HDU 1035 Robot Motion 机器人运动

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/h84121599/article/details/50818453

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 5
NEESWE
WWWESS
SNWWWW
4 5 1
SESWE
EESNW
NWEEN
EWSEN
0 0 

SAMPLE OUTPUT

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

题目要求输入三个数字,分别表示地图长宽和机器人从正上方进入的位置。然后输入地图。
机器人进入地图后根据地图的标记进行运动,W左,S下,E右,N上。问最终机器人运动状态,n步后离开地图,还是n步后开始进入m步的循环。
#include<stdio.h>  
#include<string.h>  
#include<iostream>  
using namespace std;
#define MAX 1005
char mpt[MAX][MAX];
int vis[MAX][MAX];
int a,b,in,out;
void DFS(int x,int y,int step)
{
	if (out)return;//out=1表示已结束
	if (x < 0 || y < 0 || x >= a || y >= b)//第一种结束情况,走出去
	{
		cout << step-1<<" step(s) to exit" << endl;
		out = 1;
		return;
	}
	if (vis[x][y])//第二种结束情况,走到已经走过的地点,绕圈
	{
		cout << vis[x][y] - 1 << " step(s) before a loop of " << step - vis[x][y] << " step(s)"<<endl;
		out = 1;
		return;
	}
	vis[x][y] = step;//vis数值表示当前位置是第几步走到的
	int tx, ty;
	switch (mpt[x][y])
	{
	case 'W':tx = x; ty = y-1; break;
	case 'N':tx = x-1; ty = y; break;
	case 'S':tx = x+1; ty = y ; break;
	case 'E':tx = x; ty = y + 1; break;
	}
	DFS(tx, ty, step + 1);
}
int main()
{
	while (cin >> a>>b>>in){
		if (!a&&!b&&!in)return 0;
		int i;
		memset(mpt, 0, sizeof(mpt));
		memset(vis, 0, sizeof(vis));
		out = 0;
		for (i = 0; i < a; i++)cin >> mpt[i];
		DFS(0, in - 1, 1);	
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/h84121599/article/details/50818453
今日推荐