您的位置:首页 > 其它

Sicily 1274. Pascal's Travels

2015-03-22 17:06 281 查看


1274. Pascal's Travels


Constraints

Time Limit: 1 secs, Memory Limit: 32 MB


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.

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.





NOTE: 64-bit integer values are available as long values in Java orlong
long values in C++ (g++ compiler).


Sample Input


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



Sample Output


3 
0 
70

刚开始没有考虑到用dp,用的bfs写,咋看起来时没有错的:
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
#include <iomanip>
#include <map>
#include <stack>
#include <functional>
#include <list>
using namespace std;

#define MAX_WIDTH 50

struct p {
    int ii;
    int jj;
    p() {}
    p(int i, int j) {
        ii = i;
        jj = j;
    }
};

long long int way[MAX_WIDTH][MAX_WIDTH];
int step[MAX_WIDTH][MAX_WIDTH];
bool vis[MAX_WIDTH][MAX_WIDTH];

int main() {

    std::ios::sync_with_stdio(false);

    while (1) {

        int n;

        cin >> n;

        if (n == -1) break;

        for (int i = 0; i < n; i++) {

            string s;

            cin >> s;

            for (int j = 0; j < n; j++) {

                step[i][j] = s[j] - '0';

            }

        }

        queue<p> q;

        memset(way, 0, sizeof(way));
        memset(vis, false, sizeof(vis));

        q.push(p(0, 0));
        way[0][0] = 1;
        vis[0][0] = true;

        while (!q.empty()) {

            p now = q.front();
            q.pop();

            if (step[now.ii][now.jj] == 0) continue;

            int i = now.ii + step[now.ii][now.jj];
            int j = now.jj + step[now.ii][now.jj];

            if (i < n) {

                way[i][now.jj] += way[now.ii][now.jj];
                if (!vis[i][now.jj]) {
                    q.push(p(i, now.jj));
                    vis[i][now.jj] = true;
                }

            }

            if (j < n) {

                way[now.ii][j] += way[now.ii][now.jj];
                if (!vis[now.ii][j]) {
                    q.push(p(now.ii, j));
                    vis[now.ii][j] = true;
                }

            }

        }

        cout << way[n - 1][n - 1] << endl;

    }

    //getchar();
    //getchar();
    return 0;
}

但是通过这组数据发现用bfs做会导致错误:

4
1231
8201
7271
1983
这是因为bfs会率先到达(2,3)这个位置,再到达(1,3)这个位置,导致(2,3)的步数并没有包括(1,3)的步数,因为到达过的点不会再次搜索。因此,确保搜索顺序是严格的从上往下,从左到右即可得出正确答案:

#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
#include <iomanip>
#include <map>
#include <stack>
#include <functional>
#include <list>
using namespace std;

#define MAX_WIDTH 50

struct p {
    int ii;
    int jj;
    p() {}
    p(int i, int j) {
        ii = i;
        jj = j;
    }
};

long long int way[MAX_WIDTH][MAX_WIDTH];
int step[MAX_WIDTH][MAX_WIDTH];
bool vis[MAX_WIDTH][MAX_WIDTH];

int main() {

    std::ios::sync_with_stdio(false);

    while (1) {

        int n;

        cin >> n;

        if (n == -1) break;

        for (int i = 0; i < n; i++) {

            string s;

            cin >> s;

            for (int j = 0; j < n; j++) {

                step[i][j] = s[j] - '0';

            }

        }

        memset(way, 0, sizeof(way));
        way[0][0] = 1;

        for (int i = 0; i < n; i++) {

            for (int j = 0; j < n; j++) {

                if (step[i][j] == 0) continue;

                int next_i = i + step[i][j], next_j = j + step[i][j];

                if (next_i < n) {
                    
                    way[next_i][j] += way[i][j];

                }

                if (next_j < n) {
                    
                    way[i][next_j] += way[i][j];

                }

            }

        }

        cout << way[n - 1][n - 1] << endl;

    }

    //getchar();
    //getchar();
    return 0;
}
附上帮我检测出错误的代码:

#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
#include <iomanip>
#include <map>
#include <stack>
#include <functional>
#include <list>
using namespace std;

#define MAX_WIDTH 50

struct p {
    int ii;
    int jj;
    p() {}
    p(int i, int j) {
        ii = i;
        jj = j;
    }
};

long long int way[MAX_WIDTH][MAX_WIDTH];
int step[MAX_WIDTH][MAX_WIDTH];
bool vis[MAX_WIDTH][MAX_WIDTH];
int n;

long long int test1() {

    std::ios::sync_with_stdio(false);

    while (1) {

        if (n == -1) break;

        queue<p> q;

        memset(way, 0, sizeof(way));
        memset(vis, false, sizeof(vis));

        q.push(p(0, 0));
        way[0][0] = 1;
        vis[0][0] = true;

        while (!q.empty()) {

            p now = q.front();
            q.pop();

            if (step[now.ii][now.jj] == 0) continue;

            int i = now.ii + step[now.ii][now.jj];
            int j = now.jj + step[now.ii][now.jj];

            if (i < n) {

                way[i][now.jj] += way[now.ii][now.jj];
                if (!vis[i][now.jj]) {
                    q.push(p(i, now.jj));
                    vis[i][now.jj] = true;
                }

            }

            if (j < n) {

                way[now.ii][j] += way[now.ii][now.jj];
                if (!vis[now.ii][j]) {
                    q.push(p(now.ii, j));
                    vis[now.ii][j] = true;
                }

            }

        }

        return way[n - 1][n - 1];

    }

    //getchar();
    //getchar();
    return 0;
}                 

long long int test2()
{
    long long paths[ 35 ][ 35 ];
    int i, j;
    char s[ 35 ];

    while (1) {

        memset( paths, 0, sizeof( paths ) );
        paths[ 0 ][ 0 ] = 1;
        for ( i = 0; i < n; i++ ) {
            for ( j = 0; j < n; j++ ) {
                if ( step[ i ][ j ] == 0 )
                    continue;
                if ( i + step[ i ][ j ] < n )
                    paths[ i + step[ i ][ j ] ][ j ] += paths[ i ][ j ];
                if ( j + step[ i ][ j ] < n )
                    paths[ i ][ j + step[ i ][ j ] ] += paths[ i ][ j ];
            }
        }
        return paths[n - 1][n - 1];
    }

    return 0;

}

int main() {

    while (1) {

    n = 4;

    for (int i = 0; i < n; i++) {

        for (int j = 0; j < n; j++) {

            step[i][j] = rand() % 10;

        }

    }

    int ans1 = test1(), ans2 = test2();

    if (ans1 != ans2) {

        cout << n << endl;

        for (int i = 0; i < n; i++) {

            for (int j = 0; j < n; j++) {

                cout << step[i][j];

            }

            cout << endl;

        }

        cout << ans1 << " " << ans2 << endl;

        getchar();

    }

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: