UVa 10116 - Robot Motion

来源:互联网 发布:淘宝爱互动 编辑:程序博客网 时间:2024/06/06 09:54

题目:有一个地图,地图上有运行的规则(移动到东西南北四个方向),现在给你起始点,求最后的状态。

分析:模拟。直接从起始点出发,按照地图的规则移动即可。记录每个点到达时走的步数,计算环。

说明:这题目也很眼熟(⊙_⊙)。

#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;char maps[100][100];int  smap[100][100];int main(){int N,M,X,Y;while (~scanf("%d%d%d",&N,&M,&Y) && Y) {for (int i = 0 ; i < N ; ++ i)scanf("%s",maps[i]);memset(smap, 0, sizeof(smap));X = 0; Y --;int count = 1;while (!smap[X][Y] && X >= 0 && X < N && Y >= 0 && Y < M) {smap[X][Y] = count ++;switch(maps[X][Y]) {case 'N': X --;break;case 'S': X ++;break;case 'W': Y --;break;case 'E': Y ++;break;default : break;}}if (X >= 0 && X < N && Y >= 0 && Y < M) printf("%d step(s) before a loop of %d step(s)\n",smap[X][Y]-1,count-smap[X][Y]);else printf("%d step(s) to exit\n",count-1);}return 0;}



0 0