您的位置:首页 > 其它

POJ 1573 Robot Motion

2015-04-01 23:00 309 查看
这是一题水水的模拟题。
#include <stdio.h>
#include <string.h>
#define mem(a) memset(a, 0, sizeof(a))

char map[15][15];
int m, n, s, shu[15][15];
bool vis[15][15];

void res(int x, int y, int num)
{
	if(x < 0||x >= m||y < 0||y >= n)
	{
		printf("%d step(s) to exit\n", num - 1);
		return;
	}
	if(vis[x][y] == 1)
	{
		printf("%d step(s) before a loop of %d step(s)\n", shu[x][y] - 1, num - shu[x][y]);
		return;
	}
	shu[x][y] = num;
	if(map[x][y] == 'W'&&vis[x][y] == 0)
	{
		vis[x][y] = 1;
		res(x, y - 1, num + 1);
	}
	else if(map[x][y] == 'E'&&vis[x][y] == 0)
	{
		vis[x][y] = 1;
		res(x, y + 1, num + 1);
	}
	else if(map[x][y] == 'N'&&vis[x][y] == 0)
	{
		vis[x][y] = 1;
		res(x - 1, y, num + 1);
	}
	else if(map[x][y] == 'S'&&vis[x][y] == 0)
	{
		vis[x][y] = 1;
		res(x + 1, y, num + 1);
	}
}

int main(int argc, char *argv[])
{
	int i, j;
	for(;;)
	{
		mem(map);
		mem(vis);
		mem(shu);
		scanf("%d%d%d%*c",&m, &n, &s);
		if(m == 0&&n == 0&&s == 0)
		break;
		for(i = 0;i < m;i++)
		{
			for(j = 0;j < n;j++)
			{
				scanf("%c", &map[i][j]);
				if(j == (n - 1))
				scanf("%*c");
			}
		}
		res(0, s - 1, 1);
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: