Robots on a grid
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?"
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
Sample Input 1 5 ..... #..#. #..#. ...#. ..... Sample Input 2 7 ......# ####... .#..... .#...#. .#..... .#..### .#.....
Sample Output
Sample Output 1 6 Sample Output 2 THE GAME IS A LIE
表示用dfs re到死 不过也表示自己太水了,re了7,8次还不知道换哎哎!
先dp记录一下,如果有路就直接输出,否者bfs判断一下
#include<stdio.h> #include<string.h> #include<queue> #define MOD 2147483647 using namespace std; int vis[1005][1005],n; int dir[4][2]={-1,0,0,1,0,-1,1,0}; long long dp[1005][1005]; char map[1005][1005]; struct node { int x,y; }info; bool bfs(int x,int y) { int i; info.x=x,info.y=y; queue<node>q; while(!q.empty()) q.pop(); q.push(info); while(!q.empty()) { node pos; pos=q.front(); q.pop(); if(dp[pos.x][pos.y]!=0) return true; for(i=0;i<4;i++) { int xx=pos.x+dir[i][0],yy=pos.y+dir[i][1]; if(xx>=0&&xx<n&&yy>=0&&yy<n&&!vis[xx][yy]&&map[xx][yy]!='#') { vis[xx][yy]=1; info.x=xx; info.y=yy; q.push(info); } } } return false; } int main() { int i,j; while(~scanf("%d",&n)) { memset(vis,0,sizeof(vis)); memset(dp,0,sizeof(dp)); dp[0][0]=1; for(i=0;i<n;i++) scanf("%s",map[i]); for(i=0;i<n;i++) if(map[i][0]!='#') dp[i][0]=1; else break; for(i=0;i<n;i++) if(map[0][i]!='#') dp[0][i]=1; else break; for(i=1;i<n;i++) for(j=1;j<n;j++) if(map[i][j]!='#') dp[i][j]=(dp[i][j-1]%MOD+dp[i-1][j]%MOD)%MOD; if(dp[n-1][n-1]==0) { vis[n-1][n-1]=1; if( bfs(n-1,n-1)) printf("THE GAME IS A LIE "); else printf("INCONCEIVABLE "); } else printf("%lld ",dp[n-1][n-1]); } return 0; }