HDU 1035 Robot Motion 機器人運動

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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章