Robots on a grid
来源:互联网 发布:黑色沙漠手动优化 编辑:程序博客网 时间:2024/04/30 05:25
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 n×n 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 begiven 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 Output
#include<iostream>#include<stdio.h>#include<cstring>using namespace std;long long num[1001][1001],num1[1005][1005],temp[1001][1001];const int mod=0x7fffffff;//2^32-1int i,j,n,count;const int dx[]={1,-1,0,0};const int dy[]={0,0,1,-1};void get_in()//输入数据{ char a; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { cin>>a; num[i][j]=(a=='.'?1:0); num1[i][j]=num[i][j]; } }}bool search()//计算第一种情况{ num[1][1]=1; for(j=2;j<=n;j++) { num[1][j]=((num[1][j-1]>0&&num[1][j]!=0)?num[1][j-1]:0); num[j][1]=((num[j-1][1]>0&&num[j][1]!=0)?num[j-1][1]:0); } for(i=2;i<=n;i++) for(j=2;j<=n;j++) num[i][j]=(((num[i][j-1]||num[i-1][j])&&num[i][j]!=0)?(num[i][j-1]+num[i-1][j])%mod:0); if(num[n][n]) { printf("%d\n",num[n][n]); return true; } return false;}bool bfs(int x,int y)//计算第二种情况,搜索模板{ if(temp[x][y]!=-1) return temp[x][y]; count++; temp[x][y]=0; for (int r=0;r<=3;r++) { int xx=x+dx[r]; int yy=y+dy[r]; if(num1[xx][yy]&&temp[xx][yy]!=0&&xx>0&&xx<=n&&yy>0&&yy<=n) { if(bfs(xx,yy)) { temp[x][y]=1; return true; } } } return false;}int main(){ while(scanf("%d",&n)!=EOF) { memset(temp,-1,sizeof(temp)); count=0;temp[1][1]=1; get_in(); if(search()) continue; if(bfs(n,n)) printf("THE GAME IS A LIE\n"); else printf("INCONCEIVABLE\n"); } return 0;}
- Robots on a grid
- Robots on a grid
- Robots on a grid
- 【DP+BFS】Robots on a grid
- SPOJ-9652 Robots on a grid 解题报告
- hrbust/哈理工oj 1495 Robots on a grid【BFS+BFS+dp】
- Paths on a Grid
- POJ1942_Paths on a Grid
- Paths on a Grid
- Paths on a Grid
- poj1942Paths on a Grid
- pku1942 Paths on a Grid
- 10913 - Walking on a Grid
- 10913 - Walking on a Grid
- pku1942-Paths on a Grid
- poj1942 Paths on a Grid
- UVA10913 - Walking on a Grid
- Walking on a Grid uva10913
- 在cdsn潜水好久了,工作一个月来博客写写
- 微信公众帐号开发教程第8篇-文本消息中使用网页超链接
- HDU 1174 爆头 几何
- 在uClinux上安装madwifi驱动
- 微信公众帐号开发教程第9篇-QQ表情的发送与接收
- Robots on a grid
- 微信公众帐号开发教程第10篇-解析接口中的消息创建时间CreateTime
- 两个大数相乘
- iOS 点击图片放大Demo
- 使用ramdisk和cramfs
- 在centos编译gcc4.7
- 微信公众帐号开发教程第11篇-符号表情的发送(上)
- 基于状态机的LCD多级菜单设计
- ActionScript 3.0寻找