您的位置:首页 > 其它

HOJ---12363 Robots on a grid [DP+BFS()]

2012-07-29 10:58 225 查看
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 :

 /*
5 ..... #..#. #..#. ...#. ..... 7 ......# ####... .#..... .#...#. .#..... .#..### .#.....
7
......#
####...
.#.....
.#...#.
.#.....
.#..###
.#...#.
5
.....
#..#.
#..#.
...#.
...#.
5
.....
#..#.
#..#.
...##
...#.
5
.....
#..#.
#..#.
.....
...#.
*/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <string>
#include <set>
#include <utility>
#include <queue>
#include <stack>
#include <list>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <ctype.h>
using namespace std;

char map[1010][1010];
__int64 dp[1010][1010];
int vst[1010][1010];
bool flag;
int n;

struct node
{
int x,y;
}Node;

int dir_bfs[4][2]={
0,1,
1,0,
0,-1,
-1,0
};

bool check_bfs(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<n&&map[x][y]!='#'&&!vst[x][y])
return true;
else
return false;
}

void bfs()
{
int i;
node pre,last;
pre.x=0;
pre.y=0;
queue<node>Que;
Que.push(pre);
while(!Que.empty())
{
pre=Que.front();
Que.pop();
if(pre.x==n-1&&pre.y==n-1)
{
flag=true;
return;
}
for(i=0;i<4;i++)
{
last.x=pre.x+dir_bfs[i][0];
last.y=pre.y+dir_bfs[i][1];
if(check_bfs(last.x,last.y))
{
Que.push(last);
vst[last.x][last.y]=1;
}
}
}
}

int main()
{
int i,j;
while(~scanf("%d",&n))
{
getchar();
memset(dp,0,sizeof(dp));
for(i=0;i<n;i++)
gets(map[i]);
for(i=0;i<n;i++)
{
if(map[i][0]=='#')
break;
dp[i][0]=1;
}
for(i=0;i<n;i++)
{
if(map[0][i]=='#')
break;
dp[0][i]=1;
}
for(i=1;i<n;i++)
{
for(j=1;j<n;j++)
{
if(map[i][j]!='#')
dp[i][j]=(dp[i][j-1]+dp[i-1][j])%INT_MAX;
}
}
if(dp[n-1][n-1])
printf("%I64d\n",dp[n-1][n-1]%INT_MAX);
else
{
flag=false;
memset(vst,0,sizeof(vst));
bfs();
if(flag)
printf("THE GAME IS A LIE\n");
else
printf("INCONCEIVABLE\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: