poj 1376 Robot
2011-01-12 16:39
387 查看
题目意思:就是一个机器人从起点到终点找一条用时最少的路径,有两种命令,一种是走,只能走1,2,3步,还有是转向,只能向左或向右转,这个两种操作都会耗时1s。
这个还有个地方就是机器人有一定的体积
其实就是一个简单的bfs只不过状态要多一些,因为,起点是在左下角,终点在右上角,所以在处理障碍物时只用将障碍物的打小往有下方扩展1
这个还有个地方就是机器人有一定的体积
其实就是一个简单的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; }
相关文章推荐
- POJ - 1376 Robot
- poj 1376 Robot
- POJ 1376 Robot (BFS)
- poj 1376 Robot 广搜
- POJ 1376-Robot(bfs广度优先搜索)
- 1130 POJ#1376 Robot
- POJ 1376 Robot
- poj 1729 Jack and Jill 1376 Robot 1324 Holedox Moving 1475 Pushing Boxes bfs + a*
- POJ 1376 Robot
- 1.12 POJ 1376 Robot
- poj 1376 Robot (BFS)
- POJ1376-Robot(广度优先搜索)
- 总结自己应用广度优先搜索(BFS)中的错误(记POJ 1376 Robot结题过程)
- POJ-1376-Robot
- POJ 1376 Robot(BFS)
- poj 1376 Robot BFS 块状的
- poj 1376 uva 314 Robot
- POJ 1376 Robot A* -
- poj 1376 Robot(bfs)
- POJ - 1376 - Robot