Robots on a grid

来源:互联网 发布:黑色沙漠手动优化 编辑:程序博客网 时间:2024/04/30 05:25

Description

You have recently made a grid traversing robot that can find its way from the top left corner of a grid to the bottom right corner. However, you had forgotten all your AI programming skills, so you only programmed your robot to go rightwards and downwards (that’s after all where the goal is). You have placed your robot on a grid with some obstacles, and you sit and observe. However, after a while you get tired of observing it getting stuck, and ask yourself “How many paths are there from the start position to the goal position?”, and “If there are none, could the robot have made it to the goal if it could walk upwards and leftwards?”
So you decide to write a program that, given a grid of size n×n with some obstacles marked on it where the robot cannot walk, counts the different ways the robot could go from the top left corner s to the bottom right t, and if none, tests if it were possible if it could walk up and left as well.

However, your program does not handle very large numbers, so the answer should begiven modulo 231-1.

Input

On the first line is one integer, 1 ≤ n ≤ 1000. Then follows n lines, each with n characters, where each character is one of ’.’ and ’#’, where ’.’ is to be interpreted as a walkable tile and ’#’ as a non-walkable tile. There will never be a wall at s, and there will never be a wall at t.

Output

Output one line with the number of different paths starting in s and ending in t (modulo 231-1.) or THE GAME IS A LIE if you cannot go from s to t going only rightwards and downwards but you can if you are allowed to go left and up as well, or INCONCEIVABLE if there simply is no path from s to t.

Sample Input

5
.....
#..#.
#..#.
...#.
.....
7
......#
####...
.#.....
.#...#.
.#.....
.#..###
.#.....

Sample Output

6
THE GAME IS A LIE
分析:这题目做了好久,中间的当做模板记着吧o(>_<)o ~~
代码:
#include<iostream>#include<stdio.h>#include<cstring>using namespace std;long long num[1001][1001],num1[1005][1005],temp[1001][1001];const int mod=0x7fffffff;//2^32-1int i,j,n,count;const int dx[]={1,-1,0,0};const int dy[]={0,0,1,-1};void get_in()//输入数据{    char a;    for(i=1;i<=n;i++)    {        for(j=1;j<=n;j++)        {            cin>>a;            num[i][j]=(a=='.'?1:0);            num1[i][j]=num[i][j];        }    }}bool search()//计算第一种情况{    num[1][1]=1;    for(j=2;j<=n;j++)    {        num[1][j]=((num[1][j-1]>0&&num[1][j]!=0)?num[1][j-1]:0);        num[j][1]=((num[j-1][1]>0&&num[j][1]!=0)?num[j-1][1]:0);    }    for(i=2;i<=n;i++)        for(j=2;j<=n;j++)            num[i][j]=(((num[i][j-1]||num[i-1][j])&&num[i][j]!=0)?(num[i][j-1]+num[i-1][j])%mod:0);    if(num[n][n])    {        printf("%d\n",num[n][n]);        return true;    }    return false;}bool bfs(int x,int y)//计算第二种情况,搜索模板{    if(temp[x][y]!=-1)        return temp[x][y];    count++;    temp[x][y]=0;    for (int r=0;r<=3;r++)    {        int xx=x+dx[r];        int yy=y+dy[r];        if(num1[xx][yy]&&temp[xx][yy]!=0&&xx>0&&xx<=n&&yy>0&&yy<=n)        {            if(bfs(xx,yy))            {                temp[x][y]=1;                return true;            }        }    }    return false;}int main(){    while(scanf("%d",&n)!=EOF)    {        memset(temp,-1,sizeof(temp));        count=0;temp[1][1]=1;        get_in();        if(search())            continue;        if(bfs(n,n))            printf("THE GAME IS A LIE\n");        else            printf("INCONCEIVABLE\n");    }    return 0;}


原创粉丝点击