poj 1376 uva 314 Robot
来源:互联网 发布:沈阳工业大学学校域名 编辑:程序博客网 时间:2024/09/21 09:26
Robot
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 5835 Accepted: 1873
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
Source
Central Europe 1996
多亏了这组测试数据:
9 101 1 1 1 1 1 1 1 1 11 1 1 1 1 1 0 0 1 11 1 1 1 1 0 0 0 1 11 1 1 1 0 0 0 0 1 11 1 1 0 0 0 0 0 1 11 1 0 0 1 0 0 0 1 11 0 0 0 0 0 0 0 1 11 0 0 0 0 0 0 1 1 11 1 1 1 1 1 1 1 1 17 2 2 7 south0 0答案=9
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <queue>#include <map>using namespace std;const int maxn = 90;const int dr[4] = {-1, 0,1,0};const int dl[4] = { 0,-1,0,1};int robot[maxn][maxn][10];struct node{ int x , y , dir; node(int a = 0 , int b = 0, int c = 0){ x = a , y = b , dir = c; }};int N , M;node start , end;map<string , int> mp;void initial(){ memset(robot , 0 , sizeof robot); start = node(0 , 0 , 0); end = node(0 , 0 , 0);}void readcase(){ int a; for(int i = 0;i < N;i++){ for(int j = 0;j < M;j++){ scanf("%d" , &a); if(a == 1){ for(int k = 0;k < 4;k++){ robot[i][j][k] = -1; robot[i+1][j][k] = -1; robot[i][j+1][k] = -1; robot[i+1][j+1][k] = -1; } } } } int x1 , y1 , x2 , y2; string str; cin >> x1 >> y1 >> x2 >> y2 >> str; start = node(x1 , y1 , mp[str]); end = node(x2 , y2 , 0); //cout << start.x << "+" << start.y << "+" << start.dir << endl;}void computing(){ queue<node> q; q.push(start); //cout << start.x << " " << start.y << " " << start.dir << endl; robot[start.x][start.y][start.dir] = 1; while(!q.empty()){ node n = q.front() , temp; q.pop();for(int i = 1;i <= 3;i++){temp.x = n.x+i*dr[n.dir];temp.y = n.y+i*dl[n.dir];temp.dir = n.dir;if(temp.x > 0 && temp.x < N && temp.y > 0 && temp.y < M && (robot[temp.x][temp.y][temp.dir] > robot[n.x][n.y][n.dir]+1 || robot[temp.x][temp.y][temp.dir] == 0)){robot[temp.x][temp.y][temp.dir] = robot[n.x][n.y][n.dir]+1;q.push(temp);/*if(temp.x == 2 && temp.y == 2){cout << n.x << " " << n.y << " " << n.dir << endl;}*/}else if(temp.x > 0 && temp.x < N && temp.y > 0 && temp.y < M && robot[temp.x][temp.y][temp.dir] == -1){break;}}if(robot[n.x][n.y][(n.dir+1)%4] == 0){robot[n.x][n.y][(n.dir+1)%4] = robot[n.x][n.y][n.dir]+1;q.push(node(n.x,n.y,(n.dir+1)%4));}if(robot[n.x][n.y][(n.dir+3)%4] == 0){robot[n.x][n.y][(n.dir+3)%4] = robot[n.x][n.y][n.dir]+1;q.push(node(n.x,n.y,(n.dir+3)%4));}//cout << q.size() << endl; } /*for(int i = 0;i <= N;i++){for(int j = 0;j <= M;j++){int ans = 100000000;for(int k = 0;k < 4;k++){if(robot[i][j][k] < ans){ans = robot[i][j][k];}}cout << ans-1 << " ";}cout << endl; }*/}void out(){ int ans = robot[end.x][end.y][0]; ans = min(ans, robot[end.x][end.y][1]); ans = min(ans , robot[end.x][end.y][2]); ans = min(ans , robot[end.x][end.y][3]); cout << ans-1 << endl;}int main(){mp["north"] = 0; mp["west"] = 1; mp["south"] = 2; mp["east"] = 3;//freopen("1","r" , stdin); while(cin >> N >> M && (N || M)){ initial(); readcase(); if(start.x == end.x && start.y == end.y){cout << 0 << endl; }else if(robot[end.x][end.y][0] != 0){ cout << -1 << endl; }else{computing();out(); } } return 0;}
- poj 1376 uva 314 Robot
- UVa 314 - Robot (bfs)
- 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 Robot (BFS)
- poj 1376 Robot 广搜
- POJ 1376 Robot(BFS)
- poj 1376 Robot BFS 块状的
- 【20131130】
- mysql 修改编码格式
- IOS系列——蓝牙
- git 撤销commit
- 过河问题 poj47
- poj 1376 uva 314 Robot
- Flash中用AS3做的游戏,导出apk安装到手机上滤镜效果出不来为什么?
- Java中Date的一些简单操作
- c的格式化字符串
- Servlet和Filter的url匹配以及url-pattern详解 及 filter 循环问题的解决
- LaTeX 变大“[ ”及使用\geqslant \leqslant的方法
- 比特币价格首超黄金 年涨幅超7600%
- 数据库设计三大范式
- overflow - a secret benifit