本文主要是介绍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;
}
这篇关于UVa 118 Mutant Flatworld Explorers (模拟机器人移动)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!