UVa 118 Mutant Flatworld Explorers (模拟机器人移动)

118 - Mutant Flatworld Explorers

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=54


旋转时+1或+3再取模即可。


完整代码:

/*0.019s*/

#include<cstdio>
#include<cstring>
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}, dr[4] = {'N', 'E', 'S', 'W'};

int main()
{
	bool lostpos[51][51] = {0};
	int x, y, w, h, len, dir, i;
	bool ok;
	char s[101], c;
	scanf("%d%d", &w, &h);
	while (~scanf("%d%d %c\n", &x, &y, &c))
	{
		ok = true;
		switch (c)// 初始化机器人方向
		{
			case 'N':
				dir = 0;
				break;
			case 'E':
				dir = 1;
				break;
			case 'S':
				dir = 2;
				break;
			case 'W':
				dir = 3;
				break;
		}
		gets(s);
		len = strlen(s);
		for (i = 0; i < len && ok; ++i)
		{
			switch (s[i])
			{
				case 'R'://右转
					dir = (dir + 1) % 4;
					break;
				case 'L'://左转
					dir = (dir + 3) % 4;
					break;
				case 'F'://向前走
					if ((x + dx[dir] > w) || (y + dy[dir] > h) || (x + dx[dir] < 0) || (y + dy[dir] < 0))
					{
						if (!lostpos[x][y])//出界
						{
							printf("%d %d %c LOST\n", x, y, dr[dir]);
							ok = false, lostpos[x][y] = true;
						}
					}
					else x += dx[dir], y += dy[dir];
					break;
			}
		}
		if (ok) printf("%d %d %c\n", x, y, dr[dir]);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值