poj 1376 Robot BFS
来源:互联网 发布:回力鞋怎么知是正品 编辑:程序博客网 时间:2024/06/05 02:33
简单的BFS:
题意:一个机器人从起点到终点有的时间,其中按几种命令走,命令有:向左或向右转,向前走时可以走{1,2,3}三种步骤走每次命令消耗1秒,其中如图,机器人在每格的左上角;
想法:这个题最关键是如何保存图按图将横竖都扩大一,保存每个方格的四个定点:若Map[i][j]==1则Map[i][j-1]=1; Map[i-1][j]=1; Map[i-1][j-1]=1;(由于机器人有直径则不可到达边界点);
代码实现:
#include <iostream>#include <cstring>#include <cstdio>#include <queue>using namespace std;const int MAX=56;int Move[4][2]={{-1,0},{0,1},{1,0},{0,-1}};//N,E,S,Wint Map[MAX][MAX],n,m;bool Visted[MAX][MAX][4];int pre[MAX*MAX*MAX];struct Node{ int x,y,dir; int time;};queue<Node> Q;void BFS( int x1, int y1, int x2, int y2, int dir ){ if( x1==x2 && y1==y2 ) { printf("0\n"); return; } while( !Q.empty() ) Q.pop(); memset(Visted,0,sizeof(Visted)); Visted[x1][y1][dir]=1; Node p1; p1.x=x1; p1.y=y1; p1.time=0; p1.dir=dir; Q.push(p1); while( !Q.empty() ) { Node Now; Now=Q.front(); if( Now.x==x2 && Now.y==y2 ) { printf("%d\n",Now.time); return; } Q.pop(); for( int i=1; i<=3; ++i ) { Node Next; Next.x=Now.x+Move[Now.dir][0]*i; Next.y=Now.y+Move[Now.dir][1]*i; Next.dir=Now.dir; Next.time=Now.time+1; if( Next.x<=0 || Next.y<=0 || Next.x>=n || Next.y>=m || Map[Next.x][Next.y] ) break; if( !Visted[Next.x][Next.y][Next.dir] && Map[Next.x][Next.y]==0 ) { Visted[Next.x][Next.y][Next.dir]=1; Q.push(Next); } } Node Next; Next.x=Now.x; Next.y=Now.y; Next.dir=(Now.dir+3)%4; Next.time=Now.time+1; if( !Visted[Next.x][Next.y][Next.dir] ) { Visted[Next.x][Next.y][Next.dir]=1; Q.push(Next); } Next.dir=(Now.dir+1)%4; if( !Visted[Next.x][Next.y][Next.dir] ) { Visted[Next.x][Next.y][Next.dir]=1; Q.push(Next); } } printf("-1\n");}int main(){ //freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int x1,y1,x2,y2; char Dir[10]; while( scanf("%d%d",&n,&m)!=EOF ) { if( !n && !m ) break; memset(Map,0,sizeof(Map)); 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][j-1]=1; Map[i-1][j]=1; Map[i-1][j-1]=1; } } scanf("%d%d%d%d",&x1,&y1,&x2,&y2); getchar(); scanf("%s",Dir); int k=-1; if( Dir[0]=='n' ) k=0; else if( Dir[0]=='e' ) k=1; else if( Dir[0]=='s' ) k=2; else k=3; BFS(x1,y1,x2,y2,k); } return 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 2688 Cleaning Robot(状压dp+bfs)
- POJ 1573 Robot Motion(BFS)
- POJ 1573 Robot Motion(BFS)
- POJ-2688:Cleaning Robot(bfs预处理+dfs)
- poj 1376 Robot
- poj 1376 Robot
- POJ-1376-Robot
- POJ - 1376 Robot
- POJ 1376 Robot
- POJ 1376 Robot A* -
- poj 1376 Robot
- 1130 POJ#1376 Robot
- ActivityGroup和TabActivity的差异
- C# static readonly const用法
- Maximo没导入视图配置信息时系统报重大异常 .
- 各种经验与教训
- Struts是如何实现MVC的
- poj 1376 Robot BFS
- 近似算法求解调度问题
- TCP与UDP的区别
- 带有ListView界面左右滑动,切换界面
- WINDOWS UPDATE 错误 80070003
- Java反射实体类属性(get ,set方法)
- 新白娘子传奇经典句子
- 枚举所有图标
- Google Guice简介