Robots on a grid bfs() dp

本文介绍了一个寻找从网格左上角到右下角路径的问题。通过分析障碍物布局,使用动态规划计算可行路径数量,并考虑机器人能否逆向移动来判断是否可达。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 Robots on a grid

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 272   Solved: 43
[ Submit][ Status][ Web Board]

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 dierent 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 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 dierent 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
这题wa了好久,原因就是自己建立的队列的数组开的太小了,各种提交,各种wa
View Code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;
const int maxn=1100;
char g[maxn][maxn];
long long dp[maxn][maxn],n;
const long long mod=2147483647;
bool vis[maxn][maxn];
int dir[4][2]={{1,0},{-1,0},{0,-1},{0,1}};
struct node
{
int x,y;
node(int a,int b) {x=a,y=b;}
};
queue<node> q;

bool bfs()
{
memset(vis,0,sizeof(vis));
vis[1][1]=1;
while(!q.empty()) q.pop();
q.push(node(1,1));
while(!q.empty())
{
int curx=q.front().x;
int cury=q.front().y;
q.pop();
for(int i=0;i<4;i++)
{
int nextx=curx+dir[i][0];
int nexty=cury+dir[i][1];
if(nextx<1||nextx>n||nexty<1||nexty>n||vis[nextx][nexty]||g[nextx][nexty]=='#') continue;
if(nextx==n&&nexty==n) return 1;
vis[nextx][nexty]=1;
q.push(node(nextx,nexty));
}
}
return 0;
}
int main()
{

while(cin>>n)
{
//getchar();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cin>>g[i][j];
//getchar();
}
memset(dp,-1,sizeof(dp));
dp[1][1]=1;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(g[i][j]=='#') continue;
if(dp[i-1][j]!=-1)
{
if(dp[i][j]==-1) dp[i][j]=0;
dp[i][j]+=dp[i-1][j];
dp[i][j]%=mod;
}
if(dp[i][j-1]!=-1)
{
if(dp[i][j]==-1) dp[i][j]=0;
dp[i][j]+=dp[i][j-1];
dp[i][j]%=mod;
}
}
if(dp[n][n]!=-1) printf("%lld\n",dp[n][n]%mod);
else if(bfs()) printf("THE GAME IS A LIE\n");
else printf("INCONCEIVABLE\n");
}
return 0;
}


转载于:https://www.cnblogs.com/one--world--one--dream/archive/2012/04/07/2435718.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值