【DP+BFS】Robots on a grid

Robots on a grid

Time Limit: 1 Sec   Memory Limit: 128 MB
http://acdreamoj.sinaapp.com/problem.php?id=1124

Description

You have recently made a grid traversing robot that can finnd 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 x n with some obstacles marked on it where the robot cannot walk, counts the di erent 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 be given modulo 2^31 - 1.

Input

On the fi rst 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 di erent paths starting in s and ending in t (modulo 2^31 - 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.....#..#.#..#....#......

Sample Output

6

很好的题目,考查了两个基础的算法。


#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define N 1100
#define M 0x7FFFFFFF
int n,dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
long long len[N][N];
bool flag[N][N];
char map[N][N];
struct point
{
       int x,y;
}temp;
bool bfs()
{
     queue<struct point> q;
     memset(flag,false,sizeof(flag));
     temp.x=1;temp.y=1;
     flag[1][1]=true;
     q.push(temp);
     for(;!q.empty();)
     {
           for(int i=0;i<4;++i)
           {
                temp.x=q.front().x+dir[i][0];
                temp.y=q.front().y+dir[i][1];
                if(temp.x>=1&&temp.y>=1&&temp.x<=n&&temp.y<=n&&map[temp.x][temp.y]=='.'&&(!flag[temp.x][temp.y]))
                {
                      if(temp.x==n&&temp.y==n)
                          return true;
                      flag[temp.x][temp.y]=true;
                      q.push(temp);
                }
           }
           q.pop();
     }
     return false;
}                 
int main()
{
    for(;scanf("%d",&n)!=EOF;)
    {
         memset(len,0,sizeof(len));
         len[1][1]=1;
         for(int i=1;i<=n;++i)
         {
                 getchar();
                 for(int j=1;j<=n;++j)
                 {
                         scanf("%c",&map[i][j]);
                         if((map[i][j]=='.')&&(i!=1||j!=1))
                           len[i][j]=(len[i-1][j]%M+len[i][j-1]%M)%M;
                 }
         } 
         if(!bfs())
                 printf("INCONCEIVABLE\n");
         else if(len[n][n]!=0)
                 printf("%lld\n",len[n][n]%M);
         else
                 printf("THE GAME IS A LIE\n");
    }
    return 0;
}

你可能感兴趣的:(【DP+BFS】Robots on a grid)