[UVA][模] 10116 - Robot Motion@Morris' Blog|PChome Online 人新台
2012-05-28 21:59:00| 人1,441| 回0 | 上一篇 | 下一篇

[UVA][模] 10116 - Robot Motion

0 收藏 0 0 站台

Problem F: Robot Motion

Source file:robot.{c, cpp, java, pas}
Input file:robot.in
Output file:robot.out

A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are

N north (up the page)
S south (down the page)
E east (to the right on the page)
W west (to the left on the page)

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

Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits.

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

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

For each grid in the input there is one 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 follows the instructions on a certain number of locations once, and then the instructions on some number of locations repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word "step" is always immediately followed by "(s)" whether or not the number before it is 1.

Example input:

3 6 5 NEESWE WWWESS SNWWWW 4 5 1 SESWE EESNW NWEEN EWSEN 0 0 0 

Example output:

10 step(s) to exit 3 step(s) before a loop of 8 step(s)

磨手感

#include <stdio.h>

#include <stdlib.h>

int main() {
    int r, c, s, i, j;
    while(scanf("%d %d %d", &r, &c, &s) == 3) {
        if(r == 0 && c == 0 && s == 0)
            break;
    &bsp;   char map[20][20];
        for(i = 0; i < r; i++)
            scanf("%s", map[i]);
        int used[20][20] = {};
        int x = 0, y = s-1, step = 0;
        for(i = 0; i < r; i++)
            for(j = 0; j < c; j++)
                used[i][j] = -1;
        used[x][y] = 0;
        while(1) {
            step++;
            if(map[x][y] == 'N')
                x--;
            else if(map[x][y] == 'S')
                x++;
            else if(map[x][y] == 'E')
                y++;
            else
                y--;
            if(x < 0 || y < 0 || x >= r || y >= c) {
                printf("%d step(s) to exit\n", step);
                break;
            }
            if(used[x][y] != -1) {
                printf("%d step(s) before a loop of %d step(s)\n", used[x][y], step-used[x][y]);
                break;
            }
            used[x][y] = step;
        }
    }
    return 0;
}

台: Morris
人(1,441) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][BFS] 352 - The Seasonal War
此分上一篇:[UVA][BFS] 10946 - You want what filled?

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86