Pascal's Travels

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. 

 

在做ACM题时,经常都会遇到一些比较大的整数。而常用的内置整数类型常常显得太小了:其中long 和 int 范围是[-2^31,2^31),即-2147483648~2147483647。而unsigned范围是[0,2^32),即0~4294967295。也就是说,常规的32位整数只能够处理40亿以下的数。
  那遇到比40亿要大的数怎么办呢?这时就要用到C++的64位扩展了。不同的编译器对64位整数的扩展有所不同。基于ACM的需要,下面仅介绍VC6.0与g++编译器的扩展。
  VCVC6.0的64位整数分别叫做__int64与unsigned __int64,其范围分别是[-2^63, 2^63)与[0,2^64),即-9223372036854775808~9223372036854775807与0~18446744073709551615(约1800亿亿)。对64位整数的运算与32位整数基本相同,都支持四则运算与位运算等。当进行64位与32位的混合运算时,32位整数会被隐式转换成64位整数。但是,VC的输入输出与__int64的兼容就不是很好了,如果你写下这样一段代码:

1  __int64 a;
2 cin >> a;
3 cout << a;


那么,在第2行会收到“error C2679: binary '>>' : no operator defined which takes a right-hand operand of type '__int64' (or there is no acceptable conversion)”的错误;在第3行会收到“error C2593: 'operator <<' is ambiguous”的错误。那是不是就不能进行输入输出呢?当然不是,你可以使用C的写法:

scanf("%I64d",&a);
printf(
"%I64d",a);

就可以正确输入输出了。当使用unsigned __int64时,把"I64d"改为"I64u"就可以了。
  OJ通常使用g++编译器。其64位扩展方式与VC有所不同,它们分别叫做long long 与 unsigned long long。处理规模与除输入输出外的使用方法同上。对于输入输出,它的扩展比VC好。既可以使用

1long long a;
2cin>>a;
3cout<<a;

也可以使用

scanf("%lld",&a);
printf(
"%lld",a);

使用无符号数时,将"%lld"改成"%llu"即可。
  最后我补充一点:作为一个特例,如果你使用的是Dev-C++的g++编译器,它使用的是"%I64d"而非"%lld"。     
 

转载自:http://www.cnitblog.com/cockerel/archive/2006/08/16/15356.html

 

搜索和动规的结合,输出结果要用__int64来定义,一开始没有用这个,wa了两发

能走的方向只有右边和下边,动规思路:用dp记录,如果说在走这一点的时候dp不为0,就返回dp的值。

题中有说如果棋盘上面数字为0的话不能行走,那么当走到0的时候也可以通过返回dp来实现(此时dp为0)。

#include<stdio.h>
#include<time.h>
#include<string.h>
#define M(a,b) memset(a,b,sizeof(a))
int n,a[35][35];
char b[35][35];
__int64 dp[35][35];
__int64 dfs(int x,int y)
{
    if(x==n&&y==n)
        return 1;
    else if(a[x][y]==0||dp[x][y]>0)
        return dp[x][y];
    int number=a[x][y];
    int dx,dy;
    for(int i=1; i<=2; ++i)
    {
        if(i==1) dx=x+a[x][y],dy=y;
        else dx=x,dy=a[x][y]+y;
        if(dx<=n&&dy<=n) dp[x][y]+=dfs(dx,dy);
    }
    return dp[x][y];
}
int main()
{
    while(~scanf("%d",&n)&&n!=-1)
    {
        M(a,0);
        M(dp,0);
        for(int i=0; i<n; ++i)
        {
            scanf("%s",b[i]);
            for(int j=1; j<=n; ++j)
            {
                a[i+1][j]=b[i][j-1]-'0';
            }
        }
        printf("%I64u\n",dfs(1,1));
//        printf("Time used = %.2lf\n",(double)clock()/CLOCKS_PER_SEC);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值