杭电ACM OJ 1035 Robot Motion 继续水

来源:互联网 发布:登录界面html源码下载 编辑:程序博客网 时间:2024/05/18 02:45

Robot Motion

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11550    Accepted Submission(s): 5468


Problem Description


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.
 

Input
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.
 

Output
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.
 

Sample Input
3 6 5NEESWEWWWESSSNWWWW4 5 1SESWEEESNWNWEENEWSEN0 0
 

Sample Output
10 step(s) to exit3 step(s) before a loop of 8 step(s)
翻译:
就看这个图
NEESWE
WWWESS
SNWWWW
5代表从第一行第5个进去,在数组中就是4了。
然后你当前是W就是西边,往西走。到了S。就是往南走。以此类推。
直到走出去了或者走到你走过的路了,就停止。
思路:
很简单,就是有个问题是,如果你碰到了死循环,就要写你到达死循环的步数而不是当前步数,这就很烦了。所以我们采用走过的路都标记上当前的步数,如果你遇到的不是nwes,而是遇到的是数字,那就直接输出这个数字就可以了。
代码:
package ACM1000_1099;public class RobotMotion1035 {    // TODO: 2017/12/4 ctrl + R 批量替换字符串    void calculate() {//        char[][] a = {{'N', 'E', 'E', 'S', 'W', 'E'},//                      {'W', 'W', 'W', 'E', 'S', 'S'},//                      {'S', 'N', 'W', 'W', 'W', 'W'}};        char[][] a = {{'S','E','S','W','E'},                      {'E','E','S','N','W'},                      {'N','W','E','E','N'},                      {'E','W','S','E','N'}};        int height = a.length;        int width = a[0].length;//        int enter = 5 - 1;        int enter = 1 - 1;        int row = 0;        int col = enter;        int times = 0;        while (true) {            //2个条件退出:1.这个位置已经走过(把走过的路赋值成X)2.走出去了            //有个问题:这个碰到死循环就输出到达死循环的步数有点烦,需要考虑下性能,最后决定把走过的路不赋值成X,而是走到那一步的时间,完美啊卧槽            if (row < 0 || col < 0 || row > height - 1 || col > width - 1) {                System.out.println("you cost " + times);                break;            }            if (a[row][col] != 'N' && a[row][col] != 'S'                    && a[row][col] != 'W' && a[row][col] != 'E') {                int formalTime = (int) a[row][col] - 1;                System.out.println("you cost " + formalTime + " ,and you meet a loop");                break;            }            times++;            char direction = a[row][col];            //把当前时间记录在这个点            a[row][col] = (char) times;            switch (direction) {                //                case 'N':                    row -= 1;                    break;                //                case 'S':                    row += 1;                    break;                //西                case 'W':                    col -= 1;                    break;                //                case 'E':                    col += 1;                    break;            }        }    }    public static void main(final String[] args) throws Exception {        RobotMotion1035 r = new RobotMotion1035();        r.calculate();    }}

 
原创粉丝点击