(简单) 搜索 HOJ 1097 Robot
2014-02-08 09:51
489 查看
Robot
My Tags | (Edit) |
---|
Source : ACM ICPC Central European Regional 1996 | |||
Time limit : 3 sec | Memory limit : 32 M |
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.
![](http://acm.hit.edu.cn/hoj/static/img/pic/1097_a.gif)
"""
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); }}
相关文章推荐
- (简单) 搜索 HOJ 1044 Transportation
- (简单) 搜索 HOJ 1048 Cipher
- (简单) 搜索 HOJ 1066 Substrings
- (简单)搜索 HOJ 1070 Word
- (简单)搜索 HOJ 1075 The Castle
- (简单)搜索 HOJ 1692 Choose Your Own Adventure
- Go hdu 4158 hoj 简单搜索
- (简单) 搜索 HOJ 1085 Finding Rectangles
- (简单) 搜索 HOJ 1105 Egyptian Multiplication
- (简单) 搜索 最短路 HOJ 1121 Erdos Numbers
- HOJ 1440 Knight Moves -------简单搜索 BFS 求l两点之间最小的到达步数
- poj1111 hoj 1639 Image Perimeters 简单搜索
- (简单)搜索 HOJ 1022 Spacecraft Malfunction
- (简单)搜索 HOJ 1030 Labyrinth
- hdu 4158 hoj 2581 Go 简单搜索
- 搜索引擎----以lucene为例进行简单的搜索过程描述
- Android特效专辑(九)——仿微信雷达搜索好友特效,逻辑清晰实现简单
- 仿QQ邮箱收件人选择与搜索之简单js示例
- HOJ 2091 Chess(三维简单DP)
- hdu 2616 Kill the monster(简单回溯搜索)