swust OJ 1319: Robot in Maze
2016-04-28 09:12
381 查看
地图里面有个机器人,初始面向北方也就是上方,现在你有三个指令,向面朝的地方走一步,向左转,向右转,求到达终点的最短步数,简单BFS既可,注意状态的标记,每到一个格子就有四个方向,标记既可,代码有点冗杂。
#include<stdio.h> #include<string.h> #include<queue> using namespace std; char map[200][200]; int use[200][200][4],n,m; int dir[5][2]={0,0,-1,0,1,0,0,-1,0,1}; struct node { int x; int y; int count; int di; }; int BFS(int ix,int iy) { node tem; tem.x=ix; tem.y=iy; tem.count=0; tem.di=1; use[ix][iy][1]=1; queue<node> que; que.push(tem); while(!que.empty()) { node now=que.front(); que.pop(); node next=now; // printf("%d %d %d ",next.x,next.y,next.di); if(map[now.x][now.y]=='T') return now.count; next.x=now.x+dir[now.di][0]; next.y=now.y+dir[now.di][1]; // printf(" %c \n",map[next.x][next.y]); // printf("%d %d\n",next.x,next.y); if(next.x>=0&&next.y>=0&&next.x<n&&next.y<m&&!use[next.x][next.y][next.di]&&map[next.x][next.y]!='#') { next.count++; use[next.x][next.y][next.di]=1; que.push(next); } next=now; if(now.di==1) { next.di=3; if(!use[next.x][next.y][next.di]) { next.count++; use[next.x][next.y][next.di]=1; que.push(next); } next=now; next.di=4; if(!use[next.x][next.y][next.di]) { next.count++; use[next.x][next.y][next.di]=1; que.push(next); } } next=now; if(now.di==2) { next.di=3; if(!use[next.x][next.y][next.di]) { next.count++; use[next.x][next.y][next.di]=1; que.push(next); } next=now; next.di=4; if(!use[next.x][next.y][next.di]) { next.count++; use[next.x][next.y][next.di]=1; que.push(next); } } next=now; if(now.di==3) { next.di=2; if(!use[next.x][next.y][next.di]) { next.count++; use[next.x][next.y][next.di]=1; que.push(next); } next=now; next.di=1; if(!use[next.x][next.y][next.di]) { next.count++; use[next.x][next.y][next.di]=1; que.push(next); } } next=now; if(now.di==4) { next.di=1; if(!use[next.x][next.y][next.di]) { next.count++; use[next.x][next.y][next.di]=1; que.push(next); } next=now; next.di=2; if(!use[next.x][next.y][next.di]) { next.count++; use[next.x][next.y][next.di]=1; que.push(next); } } } return -1; } int main() { int T; scanf("%d",&T); while(T--) { memset(use,0,sizeof(use)); int ix,iy; scanf("%d%d",&n,&m); for(int i=0;i<n;i++) scanf("%s",map[i]); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(map[i][j]=='S') { ix=i; iy=j; break; } } } int ans; ans=BFS(ix,iy); printf("%d\n",ans); } }
相关文章推荐
- 项目26-人数不定的工资类
- XML 入门学习笔记(一)
- 第10、11周项目— 点-圆-圆柱类族的设计(1)
- SpringMvc_@RequestMapping设置Router Url大小写不敏感
- Angularjs基础(十)
- OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波
- 如何查看Linux操作系统版本
- hdu1166 敌兵布阵(树状数组 && 线段树单点更新)
- struts2与hibernate的优缺点
- 第九周-深复制体验
- <html>a
- Web测试要点(功能、性能、可用性、兼容、安全)
- 第九周项目3——人数不定的工资类
- 如何通过aix通过端口号查到进程号
- Linux中的命令 make -f 是什么意思
- UML类图与类的关系详解
- 第九周阅读项目-(2)
- 链串的基本操作
- 110.Balanced Binary Tree从8ms到2ms到1ms
- js实现99乘法表