Walk through the maze 2 Time limit:1000 ms Memory limit:65535kb 64-bit integer Io format:
% LLDJava class name:
Main
Walking through the maze is a very interesting game that can train people's memory and thinking. now, HK is trapped in a maze. Please help him calculate how many different ways he can walk out of the maze. this maze is very strange. hk can only go up or down to the right, and cannot go back.
The maze is described using an N * M matrix, and '. 'indicates that space can pass,' * 'indicates the obstacle,' s 'indicates the starting point, and 'T' indicates the exit. for example, the following matrix describes an 8*8 maze.
... T ..
..*****.
......*.
*.***.*.
......*.
.****.*.
S ..*....
........
Each input file contains only one set of input data.
The first row of each data group is two positive integers, N and M (n, m <= 100 ).
Next is an N * M matrix. Output outputs the number of different methods available for HK (because the result may be very large, the remainder of the output model is 1908). sample input
8 8.....T....*****.......*.*.***.*.......*..****.*.S..*............
Sample output
1
Source's Fifth session of the Beijing Normal University Program Design Competition author [email protected] Problem Solving: I don't understand DP !!!!!!!!!!!!! Ah, the scum is not saved! Why is it wrong! Because it can only go up or to the right, and move down or to the right if it is reversed!
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long10 using namespace std;11 char table[110][110];12 int dp[110][110];13 int main() {14 int rows,cols,x1,x2,y1,y2,i,j;15 bool flag;16 while(~scanf("%d %d",&rows,&cols)){17 getchar();18 for(i = rows; i; i--){19 for(j = 1; j <= cols; j++){20 table[i][j] = getchar();21 if(table[i][j] == ‘S‘){22 x1 = i;y1 = j;23 }else if(table[i][j] == ‘T‘){24 x2 = i;y2 = j;25 }26 }27 getchar();28 }29 memset(dp,0,sizeof(dp));30 dp[x1][y1] = 1;31 table[x1][y1] = ‘*‘;32 flag = false;33 for(i = x1; i <= rows; i++){34 for(j = y1; j <= cols; j++){35 if(table[i][j] != ‘*‘){36 dp[i][j] = dp[i][j-1] + dp[i-1][j];37 if(table[i][j] == ‘T‘){38 flag = true;break;39 }40 }41 }42 if(flag) break;43 }44 printf("%d\n",flag?dp[x2][y2]%1908:0);45 }46 return 0;47 }View code