Robots on a grid(DP+bfs())

链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=25585

Current Server Time: 2013-08-27 20:42:26

Robots on a grid

3000ms
65536KB
 
64-bit integer IO format: %lld      Java class name: Main
Font Size:  
Type:  

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 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

尼玛,这网页有问题!!!一个图竟然有这么大!!!!!!!
MD!!卡了老子一下午!!!!
就是一道简单的搜索+动态规划:
就是用DFS()竟然会爆栈!!!!!
世界上最污秽的词语也无法表达我现在的心情!!!!!!!!
没什么可说的Runtime error(就是爆栈)
 1 #include<iostream>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<stdio.h>
 5 #include<math.h>
 6 #include<queue>
 7 using namespace std;
 8 int flag,n;
 9 int c1[4][2]={1,0,-1,0,0,1,0,-1};
10 long long dp[1005][1005];
11 const long long sb=2147483647;
12 char a[1005][1005];
13 void bfs(int x,int y)
14 {
15     int c[1005][1005];
16     int i,x1,y1,x2,y2;
17     queue<int> Q;
18     memset(c,0,sizeof(c));
19     c[x][x]=1;
20     while(!Q.empty()) Q.pop();
21     Q.push(x);
22     Q.push(y);
23     while(!Q.empty())
24     {
25         x1=Q.front();
26         Q.pop();
27         y1=Q.front();
28         Q.pop();
29         if(x1==n-1&&y1==n-1)
30     {
31         flag=1;
32         return ;
33     }
34         if(flag)
35             return ;
36         for(i=0;i<4;i++)
37     {
38         x2=x1+c1[i][0];
39         y2=y1+c1[i][1];
40         if(x2>=0&&x2<n&&y2>=0&&y2<n&&a[x2][y2]!='#'&&!c[x2][y2])
41         {
42             c[x2][y2]=1;
43             Q.push(x2);
44             Q.push(y2);
45         }
46     }
47     }
48 }
49 int main()
50 {
51     int i,j;
52     while(scanf("%d",&n)!=EOF)
53     {
54         for(i=0;i<n;i++)
55             scanf("%s",a[i]);
56         memset(dp,0,sizeof(dp));
57         dp[0][0]=1;
58         for(i=0;i<n;i++)
59             for(j=0;j<n;j++)
60                     if(a[i][j]!='#')
61                 {
62                     if(i-1>=0&&a[i-1][j]!='#')
63                             dp[i][j]+=dp[i-1][j];
64                     if(j-1>=0&&a[i][j-1]!='#')
65                             dp[i][j]+=dp[i][j-1];
66                     dp[i][j]%=sb;
67                 }
68         if(dp[n-1][n-1]==0)
69         {
70             flag=0;
71             bfs(0,0);
72             if(flag)
73                 printf("THE GAME IS A LIE\n");
74             else
75                 printf("INCONCEIVABLE\n");
76         }
77         else
78             printf("%lld\n",dp[n-1][n-1]);
79     }
80     return 0;
81 }

 



posted on 2013-08-27 20:52  ~~碾压机  阅读(338)  评论(0)    收藏  举报