您的位置:首页 > 其它

poj 1376 Robot

2011-01-12 16:39 387 查看
题目意思:就是一个机器人从起点到终点找一条用时最少的路径,有两种命令,一种是走,只能走1,2,3步,还有是转向,只能向左或向右转,这个两种操作都会耗时1s。
这个还有个地方就是机器人有一定的体积
其实就是一个简单的bfs只不过状态要多一些,因为,起点是在左下角,终点在右上角,所以在处理障碍物时只用将障碍物的打小往有下方扩展1

/*
* File:   main.cpp
* Author: mi
*
* Created on 2011年1月12日, 下午2:50
*/
#include <cstdlib>
#include <stdio.h>
#include <string.h>
#include <queue>
#define N 55
#define MAX 0x7fffffff
using namespace std;
/*
*
*/
struct node
{
int x,y,step,way;
}s,e;
int r,c,map

,d[4][2]={0,1,1,0,0,-1,-1,0};
int used

[4];
char str[10];
bool ok(int x,int y,node cur)
{
return x>0&&x<r&&y>0&&y<c&&used[x][y][cur.way]>cur.step+1;
}
int bfs()
{
int i;
queue<node> q;
while(!q.empty())
q.pop();
q.push(s);
node cur,next;
while(!q.empty())
{
cur=q.front();
q.pop();
if(cur.x==e.x&&cur.y==e.y)
return cur.step;
if(cur.step+1<used[cur.x][cur.y][(cur.way+1)%4])
{
next=cur;
next.step++;
next.way=(cur.way+1)%4;
q.push(next);
used[cur.x][cur.y][(cur.way+1)%4]=cur.step;
}
if(cur.step+1<used[cur.x][cur.y][(cur.way+4-1)%4])
{
next=cur;
next.step++;
next.way=(cur.way+4-1)%4;
q.push(next);
used[cur.x][cur.y][(cur.way+4-1)%4]=cur.step;
}
for(i=1;i<=3;i++)
{
int tx,ty;
tx=cur.x+i*d[cur.way][0];
ty=cur.y+i*d[cur.way][1];
if(map[tx][ty]==1)
break;
if(ok(tx,ty,cur))
{
next.x=tx;
next.y=ty;
next.step=cur.step+1;
next.way=cur.way;
q.push(next);
used[tx][ty][cur.way]=next.step;
}
}
}
return -1;
}
int main(int argc, char** argv)
{
int i,j;
while(scanf("%d%d",&r,&c)&&(r||c))
{
memset(map,0,sizeof(map));
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
int temp;
scanf("%d",&temp);
if(temp==1)
map[i][j]=map[i+1][j]=map[i][j+1]=map[i+1][j+1]=1;
used[i][j][0]=used[i][j][1]=used[i][j][2]=used[i][j][3]=MAX;
}
scanf("%d%d%d%d%s",&s.x,&s.y,&e.x,&e.y,str);
if(!strcmp(str,"east"))
s.way=0;
if(!strcmp(str,"south"))
s.way=1;
if(!strcmp(str,"west"))
s.way=2;
if(!strcmp(str,"north"))
s.way=3;
s.step=0;
printf("%d/n",bfs());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: