Robot Motion(机器人运动) java代码实现

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 5

NEESWE

WWWESS

SNWWWW

4 5 1

SESWE

EESNW

NWEEN

EWSEN

0 0 0

Sample Output

10 step(s) to exit

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

问题的中文介绍如下:(使用网易有道词典翻译的)

一个机器人已经被编程来按照它的路径上的指示走。机器人下一步移动方向的指令被放置在网格中。可能的说明是

North(往上)

South(往下)

East(往右)

West(往左)

例如,假设机器人从网格1的北部(顶部)开始,从南部(向下)开始。机器人的路径显示。机器人在离开网格之前要在网格中执行10条指令。

比较网格2中的情况:机器人只经过3条指令一次,然后通过8条指令开始循环,从不退出。

你要写一个程序来决定一个机器人离开网格需要多长时间或者机器人是如何循环的。

输入

将有一个或多个网格供机器人导航。每个数据的格式如下。第一行是三个用空格隔开的整数:网格中的行数、网格中的列数和机器人从北方进入的列数。可能的条目列的编号从左边的1开始。然后是指示方向的行。每个网格至少有一个和最多10行和10列指令。指令行只包含没有空格的字符N、S、E或W。

输入结束由包含0 0 0的行表示。

输出

对于输入中的每个网格,都有一行输出。机器人要么遵循一定数量的指令,从四边的任意一个网格中退出,要么在一定数量的位置上重复执行指令,然后在一定数量的位置上重复执行指令。下面的示例输入对应于上面的两个网格,并说明了两种输出形式。“step”一词后面总是紧跟着“(s)”,不管前面的数字是否为1。

我的理解:

其实问题很简单,但是我比较笨,用最笨的方法实现的,而且还不是很懂字符数组要怎么输入

先解释一下题意:

就是先输入一个行数一个列数,再输入一个数表示从第几列进入

再输入一个至少一个十行十列的字符数组,然后根据这个数组判断这个小人往哪走

(1)第一种情况就是小人花了几步走出了这个循环

(2)第二种就是小人走到第几步走进了一个几步的死循环

代码实现:(希望可以有大佬看见,能帮我修改修改我的代码,帮助我过csp)

import java.util.Scanner;

public class Robot {
	public static void main(String[] args) {
		int x, y, p;
		Scanner in = new Scanner(System.in);
		while (in.hasNext()) {//循环输入
			x = in.nextInt();//行数
			y = in.nextInt();//列数
			p = in.nextInt();//从哪进
			if (x == 0 && y == 0 && p == 0) {//如果输入的都是0就退出程序
				break;
			}
			char[][] a = new char[11][11];//定义一个字符数组
			for (int i = 0; i < x; i++) {
				String s = in.next();//先输入一个字符串
				for (int j = 0; j < y; j++) {
					a[i][j] = s.charAt(j);//再将字符串赋给字符数组的一行,这样循环就将所有的字符放在了字符数组中
				}
			}
			// for (int i = 0; i < x; i++) {
			// for (int j = 0; j < y; j++) {
			// System.out.print(a[i][j]);
			// }
			// System.out.println();
			// }

			int i = 0;
			int j = p - 1;//从0行p-1列进入
			int n = 1;//这算是踏出去的第一步
			int[][] b = new int[11][11];//定义一个辅助的整型数组,每当走过一个位置时将该位置标记为走到该位置的步数
			b[i][j] = 1;//将走过的第一个位置在辅助数组中设为1,表示第一步走到了这
			while (true) {
				switch (a[i][j]) {
				case 'N':
					i--;//向上走
					break;
				case 'S':
					i++;//向下走
					break;
				case 'W':
					j--;//向左走
					break;
				case 'E':
					j++;//向右走
					break;
				}
				if (i < 0 || i == x || j < 0 || j == y) {//当小人走出循环时,就输出走到第几步退出
					System.out.println(n + " step(s) to exit");
					break;
				} else {
					n++;//没走出循环,就步数加一
					if (b[i][j] == 0) {
						b[i][j] = n;//如果没走过该位置,就将该位置标记 ,标记为走到第几步走到了该位置,以便于记录死循环的情况
					} else {//否则就表示走过该位置,则进入了死循环
						System.out.print(b[i][j] - 1
								+ " step(s) before a loop of ");//输出之前走到该位置的步数之前的一步,是踏入死循环之前的一步
						System.out.println(n - b[i][j] + " step(s)");//输出死循环的步数
						break;
					}
				}
			}

		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值