HDU 1978 How many ways (DP)

Source: Internet
Author: User
Tags bitset cmath

Problem description This is a simple survival game where you control a robot to go from the starting point of a chessboard to the end of the chessboard (n,m). The rules of the game are described as follows: 1. The robot starts at the starting point of the chessboard and has the energy that is marked at the starting point. 2. The robot can only walk to the right or down, and consumes one unit of energy per step. 3. The robot can not stay in situ. 4. When the robot chooses a workable path, when he goes to the end of the path, he will have only the energy marked by the end point. For example, when a robot starts at (a) point and has 4 units of energy, the blue squares indicate the point at which he can reach, if he chooses the destination in this path selection (2,4)
Point, when he arrives (2,4) the point will have 1 units of energy and start the next path selection until the (6,6) point is reached. Our question is how many ways a robot is going from the beginning to the end. This can be a large number, the output of the result of 10000 modulo.

Input the first line to enter an integer t that represents the number of groups of data. For each set of data, enter two integers n,m (1 <= n,m <= 100) for the first row. Represents the size of the board. Next, enter n lines, each of M integers e (0 <= E < 20).

Output is the result of 10000 modulo for the total number of data outputs for each group.

Sample Input16 64 5 6 6 4 32 2 3 1 7 21 1 4 6 2 75 8 4 3 9 57 6 6 2 1 53 1 1 3 7 2

Sample Output3948


Method One: The current point can reach other points of the number of methods (direct 4 cycle)

1 #pragmaComment (linker, "/stack:1024000000,1024000000")2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <math.h>7#include <algorithm>8#include <queue>9#include <Set>Ten#include <bitset> One#include <map> A#include <vector> -#include <stdlib.h> - using namespacestd; the #definell Long Long - #defineEPS 1e-10 - #defineMOD 10000 - #defineINF 1e12 + #defineN 106 - intn,m; + intMp[n][n]; A intDp[n][n]; at intMain () - { -     intT; -scanf"%d",&t); -      while(t--){ -scanf"%d%d",&n,&m); in          for(intI=1; i<=n;i++){ -              for(intj=1; j<=m;j++){ toscanf"%d",&mp[i][j]); +             } -         } theMemset (DP,0,sizeof(DP)); *dp[1][1]=1; $          for(intI=1; i<=n;i++){Panax Notoginseng              for(intj=1; j<=m;j++){ -                  for(intK=i (k<=n) && (k<=mp[i][j]+i); k++){ the                      for(intW=j (w<=m) && (w<=mp[i][j]+i+j-k); w++){ +                         if((k==i) && (w==j))Continue; Adp[k][w]+=Dp[i][j]; thedp[k][w]%=MOD; +                     } -                 } $             } $         } -printf"%d\n", dp[n][m]%MOD); -     } the     return 0; -}
View Code

Method Two: Memory DP, Mark Dp[n][m]=1, and then from the go back to the memory of Dp,dfs

1 #pragmaComment (linker, "/stack:1024000000,1024000000")2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <math.h>7#include <algorithm>8#include <queue>9#include <Set>Ten#include <bitset> One#include <map> A#include <vector> -#include <stdlib.h> - using namespacestd; the #definell Long Long - #defineEPS 1e-10 - #defineMOD 10000 - #defineN 106 + #defineINF 1e12 - intn,m; + intMp[n][n]; A intDp[n][n]; at BOOLJudgeintXinty) { -     if(x<1|| X>n | | y<1|| Y&GT;M)return false; -     return true; - } - intDfsintXinty) { -     if(dp[x][y]>=0)returnDp[x][y]; indp[x][y]=0; -      for(intI=0; i<=mp[x][y];i++){ to          for(intj=0; j<=mp[x][y]-i;j++){ +             if(Judge (x+i,y+j)) { -Dp[x][y]= (Dp[x][y]+dfs (x+i,y+j))%MOD; the             } *         } $     }Panax Notoginseng     returnDp[x][y]; - } the intMain () + { A     intT; thescanf"%d",&t); +      while(t--){ -scanf"%d%d",&n,&m); $          for(intI=1; i<=n;i++){ $              for(intj=1; j<=m;j++){ -scanf"%d",&mp[i][j]); -             } the         } -memset (dp,-1,sizeof(DP));Wuyidp[n][m]=1; theprintf"%d\n", DFS (1,1)); -     } Wu     return 0; -}
View Code

HDU 1978 How many ways (DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.