SPOJ-9652 Robots on a grid 解題報告

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

       題目鏈接:http://www.spoj.pl/problems/ROBOTGRI/
       解法類型:DP+BFS ||其它
       解題思路:這是今天師大比賽的一個題目,我一開始想到是用DP做的。題目要求在向右和向下走的可行路徑數,想到用從起始點s開始,分別記錄可行路徑數,即動態規劃,然後進行狀態轉移,記錄下一個。
以Sample Input 爲例:分別的狀態數爲11111
                                                                01201
                                                                01301
                  01401
                  01556
很容易得出狀態轉移方程:d[i][j]=d[i-1][j]>?=0+d[i][j-1]>?=0;
最後就是“THE GAME IS A LIE”和“INCONCEIVABLE”情況的處理了,直接用BFS從目標點d開始擴展即可,知道遇到了狀態數大於0的就找到是THE GAME IS A LIE”的情況。否則INCONCEIVABLE”
注意“so the answer should be given modulo 2^31 - 1.”!我因爲沒有注意到要取模,WA到痛不欲生。(@﹏@)~ 
       算法實現:
//STATUS:C++_AC_513MS_17344KB
#include<stdio.h>
#include<string.h>
const int MAXN=1010,mod=0x7fffffff;
void BFS(int x,int y);
char map[MAXN][MAXN];
__int64 d[MAXN][MAXN];
int q[MAXN*MAXN],n,dx[4]={-1,0,1,0},
dy[4]={0,-1,0,1},ans,vis[MAXN][MAXN];
int main()
{
//	freopen("in.txt","r",stdin);
	int i,j,ok;
	while(~scanf("%d",&n))
	{
		ans=0;
		memset(d,0,sizeof(d));
		for(i=0;i<n;i++)
			scanf("%s",map[i]);

		for(j=0;j<n;j++){
			if(map[0][j]=='.')d[0][j]=1;
			else break;
		}
		for(i=1;i<n;i++){
			for(j=0,ok=0;j<n;j++){
				if(d[i-1][j]!=0 && map[i][j]=='.')d[i][j]=(d[i][j]+d[i-1][j])%mod,ok=1;     //downwards
				if(j>0 && d[i][j-1]!=0 && map[i][j]=='.')d[i][j]=(d[i][j]+d[i][j-1])%mod,ok=1;   //righwards
			}
			if(!ok){break;}   //無法達到要求,第一種情況排除
		}
		if(!d[n-1][n-1]){  //判斷第二或第三種情況
			memset(vis,0,sizeof(vis));
			BFS(n-1,n-1);
		}
		if(d[n-1][n-1])printf("%I64d\n",d[n-1][n-1]);
		else printf("%s\n",ans==1?"THE GAME IS A LIE":"INCONCEIVABLE");
	}
	return 0;
}	

void BFS(int x,int y)    //搜索目標狀態
{
	int front=0,rear=0,i,nx,ny,u;
	u=x*n+y;
	q[rear++]=u;
	vis[x][y]=1;
	while(front<rear)
	{
		u=q[front++];
		x=u/n,y=u%n;		
		for(i=0;i<4;i++){
			nx=x+dx[i],ny=y+dy[i];
			if(!vis[nx][ny] && nx>=0&&nx<n && ny>=0&&ny<n && map[nx][ny]=='.'){
				if(d[nx][ny]){ans=1;return;}   //搜索到,"THE GAME IS A LIE"的情況
				u=nx*n+ny;
				q[rear++]=u;
				vis[nx][ny]=1;	
			}
		}
	}
	ans=2;
}

發佈了74 篇原創文章 · 獲贊 232 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章