Robots on a grid |
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65535KB |
Total submit users: 30, Accepted users: 24 |
Problem 12363 : No special judgement |
Problem 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 nn 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 be given 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 di erent 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 |
Problem Source |
NCPC 2011 |
要用bfs
code :
1 /* 2 5 3 ..... 4 #..#. 5 #..#. 6 ...#. 7 ..... 8 7 9 ......# 10 ####... 11 .#..... 12 .#...#. 13 .#..... 14 .#..### 15 .#..... 16 7 17 ......# 18 ####... 19 .#..... 20 .#...#. 21 .#..... 22 .#..### 23 .#...#. 24 5 25 ..... 26 #..#. 27 #..#. 28 ...#. 29 ...#. 30 5 31 ..... 32 #..#. 33 #..#. 34 ...## 35 ...#. 36 5 37 ..... 38 #..#. 39 #..#. 40 ..... 41 ...#. 42 */ 43 #include <iostream> 44 #include <iomanip> 45 #include <fstream> 46 #include <sstream> 47 #include <algorithm> 48 #include <string> 49 #include <set> 50 #include <utility> 51 #include <queue> 52 #include <stack> 53 #include <list> 54 #include <vector> 55 #include <cstdio> 56 #include <cstdlib> 57 #include <cstring> 58 #include <cmath> 59 #include <ctime> 60 #include <ctype.h> 61 using namespace std; 62 63 char map[1010][1010]; 64 __int64 dp[1010][1010]; 65 int vst[1010][1010]; 66 bool flag; 67 int n; 68 69 struct node 70 { 71 int x,y; 72 }Node; 73 74 int dir_bfs[4][2]={ 75 0,1, 76 1,0, 77 0,-1, 78 -1,0 79 }; 80 81 bool check_bfs(int x,int y) 82 { 83 if(x>=0&&x<n&&y>=0&&y<n&&map[x][y]!='#'&&!vst[x][y]) 84 return true; 85 else 86 return false; 87 } 88 89 void bfs() 90 { 91 int i; 92 node pre,last; 93 pre.x=0; 94 pre.y=0; 95 queue<node>Que; 96 Que.push(pre); 97 while(!Que.empty()) 98 { 99 pre=Que.front(); 100 Que.pop(); 101 if(pre.x==n-1&&pre.y==n-1) 102 { 103 flag=true; 104 return; 105 } 106 for(i=0;i<4;i++) 107 { 108 last.x=pre.x+dir_bfs[i][0]; 109 last.y=pre.y+dir_bfs[i][1]; 110 if(check_bfs(last.x,last.y)) 111 { 112 Que.push(last); 113 vst[last.x][last.y]=1; 114 } 115 } 116 } 117 } 118 119 int main() 120 { 121 int i,j; 122 while(~scanf("%d",&n)) 123 { 124 getchar(); 125 memset(dp,0,sizeof(dp)); 126 for(i=0;i<n;i++) 127 gets(map[i]); 128 for(i=0;i<n;i++) 129 { 130 if(map[i][0]=='#') 131 break; 132 dp[i][0]=1; 133 } 134 for(i=0;i<n;i++) 135 { 136 if(map[0][i]=='#') 137 break; 138 dp[0][i]=1; 139 } 140 for(i=1;i<n;i++) 141 { 142 for(j=1;j<n;j++) 143 { 144 if(map[i][j]!='#') 145 dp[i][j]=(dp[i][j-1]+dp[i-1][j])%INT_MAX; 146 } 147 } 148 if(dp[n-1][n-1]) 149 printf("%I64d\n",dp[n-1][n-1]%INT_MAX); 150 else 151 { 152 flag=false; 153 memset(vst,0,sizeof(vst)); 154 bfs(); 155 if(flag) 156 printf("THE GAME IS A LIE\n"); 157 else 158 printf("INCONCEIVABLE\n"); 159 } 160 } 161 return 0; 162 }