POJ 1736 Robot BFS

Source: Internet
Author: User

Robot
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7561 Accepted: 2444

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 on the store to another. The robot can move only along a straight line. All tracks form a rectangular grid. Neighbouring tracks is one meter apart. The store is a rectangle N x M meters and it's entirely covered by this grid. The distance of the closest to the side of the store are 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 is 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 is changed at each track crossing. Initially the robot stands at a track crossing. The obstacles in the store is 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 the commands. These commands is GO and turn. 
The GO command has a one integer parameter N in {. After receiving this command, the robot moves n meters in the direction it faces.

The TURN command has one parameter which are either left or right. After receiving this command the robot changes it orientation by 90o in the direction indicated by the parameter.

The execution of each command lasts one second.

Help researchers for RMI to write a program which would 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, integers M <= and N <= separated by one space. In each of the next M lines there is N numbers one or zero separated by one space. One represents obstacles and zero represents empty squares. (The tracks is between the squares.) The block is terminated by a line containing four positive integers B1 B2 E1 E2 all followed by one space and the word in Dicating the orientation of the robot at the starting point. B1, B2 is the coordinates of the square in the north-west corner of which of the robot, placed. E1, E2 is the coordinates of the square to the north-west corner of which the robot should move (destination point). The orientation of the robot when it had reached the destination point was not prescribed. We use (row, column)-type coordinates, i.e. the coordinates of the upper left (the most north-west) square in the store AR e 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 is 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 would 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 0 1 1 0 0 0 0 xx 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 2 7 South0 0

Sample Output

12

To give a diagram, and then give you the beginning and end, and the direction of the beginning, asking you to find out from the beginning to the end of the minimum number of steps, if unable to reach the output-1. There are 4 points to note about this topic:

1, it can execute 2 kinds of commands in one second, one is to walk to the direction of 1-3 steps now, the other is to the left or right 90 degrees to turn (can not go backwards).

2, the figure of 1 is an obstacle, is not allowed to pass, including the boundary can not be allowed, this needs attention.

Title Analysis: The problem is simple BFS, there are five ways to change, walk 1 steps, walk 2 steps, walk three steps, turn left, turn right, the five states are pushed into the queue BFs, remember when the step is out of the border, and then more to go beyond the boundary,

Can break off. Then is the details of the problem, that is to do this problem how fast and simple modeling is very important, will test instructions into a good program language expression, which needs to rely on a lot of problems to consolidate.

Code:

#include <iostream>#include<cstdio>#include<cstring>#include<queue>#include<string>#include<algorithm>using namespacestd;structNode {intx; inty; intTime ; intpos;};intm, N;intmap[ -][ -];intvis[ -][ -][5];intB1, B2, E1, E2;intdx[4] = { -1,0,1,0 };intdy[4] = {0,1,0, -1 };stringOriginal_position;queue<Node>Q;intGetPos (stringstr) {    if(str = =" North")return 0; Else if(str = ="East")return 1; Else if(str = =" South")return 2; return 3;}BOOLCanGo (intXinty) {if(X <1|| X >= N | | Y <1|| Y >= m)return false; if(Map[x][y] | | map[x+1][y] | | map[x][y+1] || map[x+1][y+1] ) {        return false; }    return true;}intBFs () {Node start; Start.x=B1; Start.y=B2; Start.time=0; Start.pos=GetPos (original_position);    Q.push (start); Vis[start.x][start.y][start.pos]=1;  while( !Q.empty ()) {Node vn=Q.front ();                Q.pop (); intx =vn.x; inty =VN.Y; intTime =Vn.time; intpos =Vn.pos; if(x = = E1 && y = = E2)returnTime ;  for(inti =1; I <=3; i++) {x+=Dx[vn.pos]; Y+=Dy[vn.pos]; if( !canGo (x, y)) {                 Break; }                        if( !Vis[x][y][vn.pos])                {Node Next; Next.x=x; Next.y=y; Next.pos=Vn.pos; Next.time= time +1; Vis[next.x][next.y][next.pos]=1;            Q.push (next); }        }         for(inti =0; I <4; i++ ) {            if(Max (POS, i)-min (pos, i) = =2 ) {                Continue; }            if(Vis[vn.x][vn.y][i])Continue;            Node Next; Next.x=vn.x; Next.y=VN.Y; Next.pos=i; Next.time= time +1; Vis[next.x][next.y][next.pos]=1;        Q.push (next); }            }    return-1;}intMain () { while(Cin >> n >> m && (n | |m)) { while( !Q.empty ())        Q.pop (); memset (Map,0,sizeof(map)); memset (Vis,0,sizeof(VIS));  for(inti =1; I <= N; i++ ) {             for(intj =1; J <= M; J + +) {cin>>Map[i][j]; }} cin>> B1 >> B2 >> E1 >> E2 >>original_position; cout<< BFS () <<Endl; }    return 0;}
View Code

Recently seldom write blog, pay attention to thinking more brush questions, do not slack ah, people a I ten, people ten I million!

POJ 1736 Robot BFS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.