POJ 1376 Robot(BFS)
来源:互联网 发布:知乎三国和战国 编辑:程序博客网 时间:2024/05/29 19:11
用A*写了一天,没写出来,没办法只能写BFS了。
#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;struct node{ int x,y,t,dir;};const int maxn=100;int vis[maxn][maxn][4];int dx[4]={-1,0,1,0};int dy[4]={0,1,0,-1};int n,m;int map[maxn][maxn];int s1,s2,e1,e2;int getdir(char ch){ if(ch=='n') return 0; else if(ch=='e') return 1; else if(ch=='s') return 2; else return 3;}void bfs(node s){ if(s.x==e1&&s.y==e2) {printf("0\n"); return ;} memset(vis,0,sizeof(vis)); vis[s.x][s.y][s.dir]=1; queue<node> q; q.push(s); while(!q.empty()) { node p=q.front(); q.pop(); if(p.x==e1&&p.y==e2) {printf("%d\n",p.t); return ;} for(int i=1;i<=3;i++) { int x=p.x+dx[p.dir]*i; int y=p.y+dy[p.dir]*i; if(map[x][y]||x<=0||y<=0||x>=n||y>=m) break; if(vis[x][y][p.dir]) continue; vis[x][y][p.dir]=1; node k; k.x=x;k.y=y; k.t=p.t+1; k.dir=p.dir; q.push(k); } node k; k.x=p.x;k.y=p.y; k.dir=(p.dir+4-1)%4; k.t=p.t+1; if(!vis[k.x][k.y][k.dir]){ vis[k.x][k.y][k.dir]=1; q.push(k); } k.dir=(p.dir+1)%4; if(!vis[k.x][k.y][k.dir]){ vis[k.x][k.y][k.dir]=1; q.push(k); } } printf("-1\n");}int main(){ while(scanf("%d%d",&n,&m)&&(n+m)) { for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { scanf("%d",&map[i][j]); if(map[i][j]==1) { map[i-1][j]=map[i][j-1]=map[i-1][j-1]=1; } } } char cmd[10]; scanf("%d%d%d%d",&s1,&s2,&e1,&e2); scanf("%s",cmd); node s; s.x=s1,s.y=s2; s.t=0; s.dir=getdir(cmd[0]); bfs(s); } return 0;}
0 0
- poj 1376 Robot (BFS)
- POJ 1376 Robot(BFS)
- poj 1376 Robot BFS
- POJ 1376 Robot (BFS)
- poj 1376 Robot(bfs)
- poj 1376 Robot BFS 块状的
- POJ 1573 Robot Motion(BFS)
- POJ 1573 Robot Motion(BFS)
- poj 1573 - Robot Motion(BFS递推)
- C. Robot(BFS)
- Patrol Robot(BFS)
- Robot Motion(BFS)
- POJ 2688 Cleaning Robot(状压dp+bfs)
- POJ-2688:Cleaning Robot(bfs预处理+dfs)
- 总结自己应用广度优先搜索(BFS)中的错误(记POJ 1376 Robot结题过程)
- (BFS+DFS)Robot Navigation
- 1600 - Patrol Robot(BFS)
- CF Cleaner Robot (BFS)
- HDU 5319 Painter(模拟 + 规律)——2015 Multi-University Training Contest 3
- 怎样用conda安装opencv
- python从零开始学习(三)--os模块
- HTTP缓存
- Hibernate中用到联合主键的使用方法,为何要序列化,为何要重写hashcode 和 equals 方法
- POJ 1376 Robot(BFS)
- AC自动机水题
- NSIndexSet基本用法
- 二维数组中的查找
- 瀑布流php+ajax+html+js
- 随记(二)
- 安卓入门第二篇
- poj3083(dfs/bfs混合)
- iOS UIAppearance使用详解