HDU1035 Robot Motion "chain-forward star"

Source: Internet
Author: User

Robot Motion

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)Total Submission (s): 7039 Accepted Submission (s): 3244
Problem Description
A robot have been programmed to follow the instructions in its path. Instructions for the next direction the robot was to move was laid down in a grid. The possible instructions is

N North (up the page)
S South (down the page)
E East (to "right" on the page)
W West (to the left on the page)

For example, suppose the robot starts on the north (top) side of grids 1 and starts south (down). The path the robot follows is shown. The robot goes through instructions in the grid before leaving the grid.

Compare What happens on Grid 2:the robot goes through 3 instructions only once, and then starts a loop through 8 instruct Ions, and never exits.

You is to write a program this determines how long it takes a robot to get out of the grid or how the robot loops around.

Input
There'll be is one or more grids for robots to navigate. The data for each are in the following form. On the first line is three integers separated by blanks:the number of rows in the grid and the number of columns in the GRI D, and the number of the column in which the robot enters from the north. The possible entry columns is numbered starting with one in the left. Then come the rows of the direction instructions. Each grid would has at least one and at the most rows and columns of instructions. The lines of instructions contain only is the characters N, S, E, or W with no blanks. The end of input is indicated by a row containing 0 0 0.

Output
For each grid, the input there is a line of output. Either the robot follows a certain number of instructions and exits the grid on any one, the four sides or else the robot F Ollows the instructions on a certain number of locations once, and then the instructions on some number of locations Repea tedly. The sample input below corresponds to the grids above and illustrates the both forms of output. The word "step" is all immediately followed by "(s)" Whether or not, the number before it is 1.

Sample Input
3 6 5
Neeswe
Wwwess
Snwwww
4 5 1
Seswe
Eesnw
Nween
Ewsen
0 0

Sample Output
Ten step (s) to exit

3 Step (s) before a loop of 8 step (s)


The main topic: give you a n*m rectangular chart, each position has ' N ', ' S ', ' W ', ' E ', respectively, to indicate the upper and lower left and right

Four directions. Give you the initial position, if you can go out, output the number of steps out of the way.

If it's a ring, how many steps will it take to go around the output ring?

Idea: Use the chain forward to the star to do, the figure as a n points, each position is an edge, the side with the chain forward to the star storage

Up, each side has a serial number, this number is the number of steps to walk. And then, every step, we start to convict.

If the fault has previously been stored in this position (this side), if it is stored, it jumps out of the loop, and the output already exists in the order

Number (that is, the number of steps before the loop), and then the total serial number-the ordinal number of the position that is already there (that is, the step in the loop). If not

Once stored, go straight down until you have a stored or out image. If the plot is output the total serial number.


#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace    std;const int MAXN = 15;const int maxm = 150;int head[maxn];struct edgenode{int to;    int W; int next;};    Edgenode Edges[maxm];char Map[15][15];int Main () {int n,m,d,k;        while (CIN >> n >> m) {if (n==0 && m==0) break;        Cin >> D;        for (int i = 0; i < n; i++) cin >> Map[i];        memset (head,-1,sizeof (head));        int i = 0;        int j = d-1;        int num = 0;            while (1) {char c = map[i][j];                if (head[i] = =-1) {edges[num].to = J;                EDGES[NUM].W = 1;                Edges[num].next = Head[i];                Head[i] = num;            num++;                    } else {for (k = head[i]; K! =-1; k = edges[k].next) {               if (edges[k].to = = j)//         Break                    } if (k = =-1) {edges[num].to = J;                    EDGES[NUM].W = 1;                    Edges[num].next = Head[i];                    Head[i] = num;                num++;            } else break;                } switch (c) {case ' N ': i--;            Break                Case ' W ': j--;            Break                Case ' E ': j + +;            Break                Case ' S ': i++;            Break        } if (I < 0 | | J < 0 | | I >= n | | J >= m) break;        } if (I < 0 | | J < 0 | | I >= n | | J >= m) printf ("%d step (s) to exit\n", num);    else printf ("%d step (s) before a loop of%d step (s) \ n", k,num-k); } return 0;}



HDU1035 Robot Motion "chain-forward star"

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.