Pascal's Travels(记忆化搜索dp+dfs)

 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 2^63 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


        
  
Brute force methods examining every path will likely exceed the allotted time limit. 
64-bit integer values are available as "__int64" values using the Visual C/C++ or "long long" values 
using GNU C/C++ or "int64" values using Free Pascal compilers. 

Hint

Hint
        
 记忆化搜索,挺简单的写多了就会了,代码如下:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define mm(a,n) memset(a,n,sizeof(a))
#define ll long long
#define inf 0x3f3f3f3f
#define N 100010
using namespace std;
ll s;
int nxt[2][2]= {{1,0},{0,1}},n;
ll dp[50][50];
char a[50][50];
ll  dfs(int x,int y)
{
    bool flag=0;
    ll s=0;
    if(dp[x][y]!=-1)return dp[x][y];
    if(x==n-1&&y==n-1)return 1;
    if(a[x][y]=='0')
        return dp[x][y]=0;
    for(int i=0; i<2; i++)
    {
        int X=(a[x][y]-'0')*nxt[i][0]+x,Y=(a[x][y]-'0')*nxt[i][1]+y;
        if(X>=0&&X<n&&Y>=0&&Y<n)
        {
            flag=1;
            s+=dfs(X,Y);
        }
    }
    if(flag==0)
        return dp[x][y]=0;
    return dp[x][y]=s;;
}
int main()
{
//freopen("C:\\Users\\nuoyanli\\Desktop\\acm.txt","r",stdin);
    while(cin>>n&&n!=-1)
    {
        s=0;
        for(int i=0; i<n; i++)
            cin>>a[i];
        mm(dp,-1);
        cout<<dfs(0,0)<<endl;
    }
    return 0;
}

 

全部评论

相关推荐

小鸡蛋吃布丁:上岸编制,考个偏远的四五线小县城的话那确实难度不高,工资三四千的,但是考发达地区的纯看实力和运气了
点赞 评论 收藏
分享
05-25 10:45
门头沟学院 Java
Frank_zhang:没实习一个项目肯定不够,可以再做一个轮子,技术栈再补一个mq,微服务,整体再换个简历模板,暑期尽量再找一个日常实习
点赞 评论 收藏
分享
05-12 16:04
已编辑
江西财经大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务