您的位置:首页 > 其它

(简单) 搜索 HOJ 1097 Robot

2014-02-08 09:51 489 查看

Robot

My Tags  (Edit)
Source : ACM ICPC Central European Regional 1996
Time limit : 3 secMemory limit : 32 M
Submitted : 239, Accepted : 107The 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.
Input[/b]

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[/b]

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[/b]9 10
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 1 0
7 2 2 7 south
0 0

Sample Output[/b]12


题意:机器人的能进行三个动作,往前走一到三步,向左转,向右转,  这三个操作都消耗一个单位的时间,求从起点到终点的最短时间思路:直接bfs就行了,边界条件是 0<r<n 0<c<m
代码:#include<iostream>#include<cstdio>#include<string.h>#include<algorithm>#include<string>#include<deque>#include<queue>#include<math.h>#include<vector>#include<map>#include<stack>#include<set>using namespace std;#define MAX 100+10#define MOD 99997const int inf = 0xfffffff;
struct State{ State(int x,int y,int z)  : r(x) , c(y) , face(z) { } int r,c,face;};int can_go[MAX][MAX];int d[MAX][MAX][10];int ver[4] = { -1,0,1,0 };int hor[4] = { 0,1,0,-1 };int turn[4][4] = { {0,1,2,1} , {1,0,1,2} ,  {2,1,0,1} , {1,2,1,0}};int b1,b2,e1,e2;int n,m;
bool in_range(int k,int face,int i,int j){ int r = k*ver[face]+i; int c = k*hor[face]+j; if (r>=n || r<=0) return false; if (c>=m || c<=0) return false; return true;}
void bfs(int f){ int i,j,face; queue<State> q; q.push(State(b1,b2,f)); while (q.size()) { i = q.front().r; j = q.front().c; face = q.front().face; q.pop(); int r , c; for (int k = 1 ; in_range(k,face,i,j) && can_go[r=i+k*ver[face]][c=j+k*hor[face]] && k <=3 ; ++k) { if (d[r][c][face] > d[i][j][face]+1) { d[r][c][face] = d[i][j][face] + 1; q.push(State(r,c,face)); } } for (int k = 0 ; k < 4 ; ++k) if (k!=face) { if (d[i][j][k] > d[i][j][face]+turn[face][k]) { d[i][j][k] = d[i][j][face] + turn[face][k]; q.push(State(i,j,k)); } } }}

int min4(int a,int b,int c,int d){ if (a<b) b = a; if (b<c) c = b; if (c<d) d = c; return d;}
int main(){ while (scanf("%d%d",&n,&m)) { if (n==0 && m==0) return 0; memset(can_go,0,sizeof(can_go)); for (int i = 0 ; i <= n ;  ++i) { for (int j = 0 ; j <= m ; ++j) { can_go[i][j] = true; for (int k = 0 ; k < 4 ; ++k) d[i][j][k] = inf; } } int obs; for (int i = 1 ; i <= n ; ++i) { for (int j = 1; j <= m ; ++j) { scanf("%d",&obs); if (obs) { can_go[i][j] = can_go[i-1][j] = can_go[i][j-1] = can_go[i-1][j-1] = false; } } } char s_face[10]; scanf("%d%d%d%d%s",&b1,&b2,&e1,&e2,s_face); if (s_face[0]=='n') { d[b1][b2][0] = 0; bfs(0); } else if (s_face[0]=='e') { d[b1][b2][1] = 0; bfs(1); } else if (s_face[0]=='s') { d[b1][b2][2] = 0; bfs(2); } else { d[b1][b2][3] = 0; bfs(3); }
int ans = min4(d[e1][e2][0],d[e1][e2][1],d[e1][e2][2],d[e1][e2][3]); if (ans==inf) printf("-1\n"); else  printf("%d\n",ans); }}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: