1. 程式人生 > >POJ 2704 Pascal's Travels 【DFS記憶化搜索】

POJ 2704 Pascal's Travels 【DFS記憶化搜索】

val from its 技術 mem namespace sin lse +=

題目傳送門:http://poj.org/problem?id=2704

Pascal‘s Travels

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5535 Accepted: 2500

Description

An n x n game board is populated with integers, one nonnegative integer per square. The goal is to travel along any legitimate path from the upper left corner to the lower right corner of the board. The integer in any one square dictates how large a step away from that location must be. If the step size would advance travel off the game board, then a step in that particular direction is forbidden. All steps must be either to the right or toward the bottom. Note that a 0 is a dead end which prevents any further progress.


Consider the 4 x 4 board shown in Figure 1, where the solid circle identifies the start position and the dashed circle identifies the target. Figure 2 shows the three paths from the start to the target, with the irrelevant numbers in each removed.
技術分享圖片
技術分享圖片
Figure 1 Figure 2

Input

The input contains data for one to thirty boards, followed by a final line containing only the integer -1. The data for a board starts with a line containing a single positive integer n, 4 <= n <= 34, which is the number of rows in this board. This is followed by n rows of data. Each row contains n single digits, 0-9, with no spaces between them.

Output

The output consists of one line for each board, containing a single integer, which is the number of paths from the upper left corner to the lower right corner. There will be fewer than 263 paths for any board.

Sample Input

4
2331
1213
1231
3110
4
3332
1213
1232
2120
5
11101
01111
11111
11101
11101
-1

Sample Output

3
0
7

Hint

Brute force methods examining every path will likely exceed the allotted time limit. 64-bit integer values are available as long values in Java or long long values using the contest‘s C/C++ compilers.

Source

Mid-Central USA 2005

題意概括:

有一個N*N的遊戲板, 每一格的數字代表可以跳的步數,起點在左下角,每次可以選擇向下或者向右跳,問從左上角起點(固定)跳到右下角終點(固定)的路徑有幾條。

解題思路:

DFS模擬暴力跳的可能性,記憶化搜索需要記錄從當前格可以到達終點的路徑數。

!!!因為每天路徑都是獨一無二的,需要一個標記數組 vis (一開始想著每次都是向下向右應該不會重復,不過wa掉了)

AC code:

 1 ///POJ 2704 記憶化搜索
 2 #include <cmath>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <iostream>
 6 #include <algorithm>
 7 #define INF 0x3f3f3f3f
 8 #define ll long long int
 9 using namespace std;
10 const int MAXN = 35;
11 
12 ll d[MAXN][MAXN];
13 int mmp[MAXN][MAXN];
14 bool vis[MAXN][MAXN];
15 int N;
16 
17 bool ok(int x, int y)
18 {
19     if(x >= 1 && x <= N && y >= 1 && y <= N) return true;
20     else return false;
21 }
22 ll dfs(int x, int y)
23 {
24     if(d[x][y] || (x==N && y==N)) return d[x][y];
25     if(!ok(x+mmp[x][y], y) && !ok(x, y+mmp[x][y])) d[x][y] = -1;
26     if(ok(x+mmp[x][y], y) && d[x+mmp[x][y]][y]!=-1)
27     {
28         if(!vis[x+mmp[x][y]][y])
29         {
30             vis[x+mmp[x][y]][y] = 1;
31             ll len = dfs(x+mmp[x][y], y);
32             vis[x+mmp[x][y]][y] = 0;
33             if(len > 0) d[x][y] += len;
34         }
35     }
36     if(ok(x, y+mmp[x][y]) && d[x][y+mmp[x][y]]!=-1)
37     {
38         if(!vis[x][y+mmp[x][y]])
39         {
40             vis[x][y+mmp[x][y]] = 1;
41             ll len = dfs(x, y+mmp[x][y]);
42             vis[x][y+mmp[x][y]] = 0;
43             if(len > 0) d[x][y] += len;
44         }
45     }
46     return d[x][y];
47 }
48 int main()
49 {
50     int T = 30;
51     char str[MAXN][MAXN];
52     while(T--)
53     {
54         scanf("%d", &N);
55         if(N == -1) break;
56         for(int i = 1; i <= N; i++)
57                 scanf("%s", &str[i]);
58         for(int i = 1; i <= N; i++)
59             for(int j = 0; j < N; j++)
60             mmp[i][j+1] = str[i][j]-0;
61         memset(d, 0, sizeof(d));
62         memset(vis, 0, sizeof(vis));
63         d[N][N] = 1;
64         ll ans = dfs(1, 1);
65         printf("%lld\n", ans);
66     }
67     return 0;
68 }

POJ 2704 Pascal's Travels 【DFS記憶化搜索】