POJ 1376 Robot
来源:互联网 发布:windows一直系统还原 编辑:程序博客网 时间:2024/06/05 05:18
Description
The Robot Moving Institute is using a robot in their local store to transport different items. Of course the robot should spend only the minimum time necessary when travelling from one place in the store to another. The robot can move only along a straight line (track). All tracks form a rectangular grid. Neighbouring tracks are one meter apart. The store is a rectangle N x M meters and it is entirely covered by this grid. The distance of the track closest to the side of the store is exactly one meter. The robot has a circular shape with diameter equal to 1.6 meter. The track goes through the center of the robot. The robot always faces north, south, west or east. The tracks are in the south-north and in the west-east directions. The robot can move only in the direction it faces. The direction in which it faces can be changed at each track crossing. Initially the robot stands at a track crossing. The obstacles in the store are formed from pieces occupying 1m x 1m on the ground. Each obstacle is within a 1 x 1 square formed by the tracks. The movement of the robot is controlled by two commands. These commands are GO and TURN.
The GO command has one integer parameter n in {1,2,3}. After receiving this command the robot moves n meters in the direction it faces.
The TURN command has one parameter which is either left or right. After receiving this command the robot changes its orientation by 90o in the direction indicated by the parameter.
The execution of each command lasts one second.
Help researchers of RMI to write a program which will determine the minimal time in which the robot can move from a given starting point to a given destination.
The GO command has one integer parameter n in {1,2,3}. After receiving this command the robot moves n meters in the direction it faces.
The TURN command has one parameter which is either left or right. After receiving this command the robot changes its orientation by 90o in the direction indicated by the parameter.
The execution of each command lasts one second.
Help researchers of RMI to write a program which will determine the minimal time in which the robot can move from a given starting point to a given destination.
Input
The input consists of blocks of lines. The first line of each block contains two integers M <= 50 and N <= 50 separated by one space. In each of the next M lines there are N numbers one or zero separated by one space. One represents obstacles and zero represents empty squares. (The tracks are between the squares.) The block is terminated by a line containing four positive integers B1 B2 E1 E2 each followed by one space and the word indicating the orientation of the robot at the starting point. B1, B2 are the coordinates of the square in the north-west corner of which the robot is placed (starting point). E1, E2 are the coordinates of square to the north-west corner of which the robot should move (destination point). The orientation of the robot when it has reached the destination point is not prescribed. We use (row, column)-type coordinates, i.e. the coordinates of the upper left (the most north-west) square in the store are 0,0 and the lower right (the most south-east) square are M - 1, N - 1. The orientation is given by the words north or west or south or east. The last block contains only one line with N = 0 and M = 0.
Output
The output contains one line for each block except the last block in the input. The lines are in the order corresponding to the blocks in the input. The line contains minimal number of seconds in which the robot can reach the destination point from the starting point. If there does not exist any path from the starting point to the destination point the line will contain -1.
Sample Input
9 100 0 0 0 0 0 1 0 0 00 0 0 0 0 0 0 0 1 00 0 0 1 0 0 0 0 0 00 0 1 0 0 0 0 0 0 00 0 0 0 0 0 1 0 0 00 0 0 0 0 1 0 0 0 00 0 0 1 1 0 0 0 0 00 0 0 0 0 0 0 0 0 01 0 0 0 0 0 0 0 1 07 2 2 7 south0 0
Sample Output
12
注意理解题意,格子和路的区别。BFS。
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<queue>using namespace std;struct Node{ int x,y; int step,dir;}node[2600];int n,m,dirr,sx,sy,ex,ey;int mp[51][51];bool vis[51][51][4];char to[11];int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};queue<Node>q;bool judge(int x,int y){ if(x>=n||x<1||y<1||y>=m) return false; if(mp[x][y]||mp[x+1][y]||mp[x][y+1]||mp[x+1][y+1]) return false; return true;}void bfs(){ int i,j; Node now,next; while(!q.empty()) { now=q.front(); q.pop(); for(i=1;i<=3;i++) { next.x=now.x+i*dir[now.dir][0]; next.y=now.y+i*dir[now.dir][1]; next.step=now.step+1; next.dir=now.dir; if(!judge(next.x,next.y)) break; if(next.x==ex&&next.y==ey) { printf("%d\n",next.step); return; } if(!vis[next.x][next.y][next.dir]) { vis[next.x][next.y][next.dir]=1; q.push(next); } } for(i=0;i<4;i++) { if((max(i,now.dir)-min(i,now.dir))==2) continue; if(vis[now.x][now.y][i]) continue; next.x=now.x;next.y=now.y;next.step=now.step+1;next.dir=i; vis[next.x][next.y][next.dir]=1; q.push(next); } } printf("-1\n"); return;}int main(){ while(~scanf("%d%d",&n,&m)) { if(n+m==0) break; int i,j; Node start; memset(vis,0,sizeof(vis)); while(!q.empty()) q.pop(); for(i=1;i<=n;i++) for(j=1;j<=m;j++) scanf("%d",&mp[i][j]); scanf("%d%d%d%d",&sx,&sy,&ex,&ey); scanf("%s",to); if(mp[ex][ey]) { printf("-1\n"); continue; } if(sx==ex&&sy==ey) { printf("0\n"); continue; } if(to[0]=='n') dirr=0; if(to[0]=='e') dirr=1; if(to[0]=='s') dirr=2; if(to[0]=='w') dirr=3; start.x=sx;start.y=sy;start.step=0;start.dir=dirr; vis[sx][sy][dirr]=1; q.push(start); bfs(); } return 0;}
阅读全文
0 0
- poj 1376 Robot
- poj 1376 Robot
- poj 1376 Robot BFS
- POJ-1376-Robot
- POJ 1376 Robot (BFS)
- POJ - 1376 Robot
- POJ 1376 Robot
- poj 1376 Robot(bfs)
- POJ 1376 Robot A* -
- poj 1376 Robot
- 1130 POJ#1376 Robot
- POJ 1376 Robot
- poj 1376 uva 314 Robot
- poj 1376 Robot (BFS)
- poj 1376 Robot 广搜
- POJ 1376 Robot(BFS)
- poj 1376 Robot BFS 块状的
- poj 1573 Robot Motion
- python连接
- 怎样让静态库(static library)中的category变得可用
- 试玩UML(User Mode Linux)
- 柱状图
- HTTP Status 400
- POJ 1376 Robot
- java分布式任务调度xxl-job
- DB2 SQL Error: SQLCODE=-668, SQLSTATE=57016 解决方案
- 软考常考知识点整理-项目机会研究
- WinX DVD Ripper Platinum(DVD转换工具)官方破解版V8.6.0.208下载 | winx dvd ripper 破解版无需winx dvd ripper 注册码
- shell 脚本,将war包从指定位置解压到tomcat/WebApps下并启动项目
- 险些“B轮死”的小猪短租,如何穿越了融资生死线
- 详解BAT盘根错节的O2O矩阵格局
- Elasticsearch配置及head插件安装