【HDOJ】1208 Pascal's Travels
记忆化搜索。注意当除右下角0外,其余搜索到0则返回。
1 #include <algorithm> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 #define MAXN 35 8 char map[MAXN][MAXN]; 9 __int64 visit[MAXN][MAXN]; 10 11 typedef struct node_st { 12 int x, y; 13 node_st() {} 14 node_st(int xx, int yy) { 15 x = xx; y = yy; 16 } 17 } node_st; 18 19 int n; 20 21 __int64 dfs(int x, int y) { 22 if (map[x][y] == 0) 23 return 0; 24 if (visit[x][y]) 25 return visit[x][y]; 26 // down 27 int nx = x+map[x][y]; 28 int ny = y+map[x][y]; 29 if (nx>=0 && nx<n) 30 visit[x][y] += dfs(nx, y); 31 if (ny>=0 && ny<n) 32 visit[x][y] += dfs(x, ny); 33 return visit[x][y]; 34 } 35 36 int main() { 37 int i, j; 38 39 while (scanf("%d%*c",&n)!=EOF && n>0) { 40 for (i=0; i<n; ++i) { 41 scanf("%s", map[i]); 42 for (j=0; j<n; ++j) 43 map[i][j] -= '0'; 44 } 45 memset(visit, 0, sizeof(visit)); 46 visit[n-1][n-1] = 1; 47 map[n-1][n-1] = 1; 48 printf("%I64d\n", dfs(0,0)); 49 #ifndef ONLINE_JUDGE 50 fflush(stdout); 51 #endif 52 } 53 54 return 0; 55 }