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) orTHE 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, orINCONCEIVABLE 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
DP+BFS
吗的今天WA MLE TLE了一下午,重写了一次,才发现bfs写错了
吗的 两个代码都贴上吧
代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
char grid[1005][1005];
long long dp[1005][1005];
int stacks[1000005];
//int stacksy[1000005];
int sign[1005][1005];
bool dpsign[1005][1005];
int dx[5]={0,-1,0,1};
int dy[5]={1,0,-1,0};
int mod=0x7fffffff;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(dp,0,sizeof(dp));
memset(sign,0,sizeof(sign));
memset(dpsign,0,sizeof(dpsign));
int len,front;
len=front=-1;
for(int i=1;i<=n;i++)
scanf("%s",(grid[i])+1);
dp[1][1]=dpsign[1][1]=1;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(grid[i][j]=='.'&&!(i==1&&j==1))
{
dp[i][j]=(dp[i-1][j]%mod+dp[i][j-1]%mod)%mod;
}
if((dpsign[i-1][j]!=0||dpsign[i][j-1]!=0)&&grid[i][j]!='#')
{
dpsign[i][j]=1;
}
}
stacks[++len]=1002;
int ok=0;
sign[1][1]=1;
while(front<len)
{
front++;
int nx=stacks[front]/1001;
int ny=stacks[front]%1001;
for(int i=0;i<4;i++)
{
int zx=nx+dx[i];
int zy=ny+dy[i];
if(zx==n&&zy==n)
{
ok=1;
break;
}
if(zx>=1&&zx<=n&&zy>=1&&zy<=n&&!sign[zx][zy]&&grid[zx][zy]!='#')
{
stacks[++len]=zx*1001+zy;
sign[zx][zy]=1;
}
}
if(ok)
break;
}
if(dpsign[n][n]!=0)
printf("%I64d\n",dp[n][n]%mod);
else if(ok)
printf("THE GAME IS A LIE\n");
else
printf("INCONCEIVABLE\n");
}
}
另:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
char grid[1005][1005];
long long dp[1005][1005];
int sign[1005][1005];
bool dpsign[1005][1005];
int dx[5]={0,-1,0,1};
int dy[5]={1,0,-1,0};
int mod=0x7fffffff;
int n;
struct node
{
int x;
int y;
};
queue<node> stacks;
int bfs()
{
int nx,ny,zx,zy;
node jy;
while(!stacks.empty())
stacks.pop();
jy.x=1;
jy.y=1;
stacks.push(jy);
sign[1][1]=1;
while(!stacks.empty())
{
nx=stacks.front().x;
ny=stacks.front().y;
stacks.pop();
for(int i=0;i<4;i++)
{
zx=nx+dx[i];
zy=ny+dy[i];
if(zx==n&&zy==n)
{
return 1;
}
if(zx>=1&&zx<=n&&zy>=1&&zy<=n&&!sign[zx][zy]&&grid[zx][zy]!='#')
{
node ks;
ks.x=zx;
ks.y=zy;
stacks.push(ks);
sign[zx][zy]=1;
}
}
}
return 0;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(dp,0,sizeof(dp));
memset(sign,0,sizeof(sign));
memset(dpsign,0,sizeof(dpsign));
for(int i=1;i<=n;i++)
scanf("%s",(grid[i])+1);
dp[1][1]=dpsign[1][1]=1;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(grid[i][j]=='.'&&!(i==1&&j==1))
{
dp[i][j]=(dp[i-1][j]%mod+dp[i][j-1]%mod)%mod;
}
if((dpsign[i-1][j]!=0||dpsign[i][j-1]!=0)&&grid[i][j]!='#')
{
dpsign[i][j]=1;
}
}
if(!bfs())
printf("INCONCEIVABLE\n");
else if(dpsign[n][n]!=0)
printf("%I64d\n",dp[n][n]%mod);
else
printf("THE GAME IS A LIE\n");
}
}