标签:
题目:给你一个n*m的格子,现在有很多机器人(给定初始坐标和面向)依次在上面行走,问最后的位置和面向,
如果走到边界就会掉落,并在掉落前的点标记,让后面的机器人不走这个点。
分析:模拟。直接模拟即可,利用循环取余计算相邻方向,利用地图标记不走的点即可。
说明:注意跳过的点是地图上的点,之前有的机器人走过这里后掉下去了,他走这里后也会掉下去就跳过。
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int maps[55][55];
int dxy[4][2] = {0,1,1,0,0,-1,-1,0};
int main()
{
int n,m,x,xx,y,yy,towards[128];
char start,steps[105],face[5] = "NESW";
towards['N'] = 0;
towards['E'] = 1;
towards['S'] = 2;
towards['W'] = 3;
memset(maps, 0, sizeof(maps));
scanf("%d %d",&n,&m);
while (~scanf("%d %d %c",&x,&y,&start)) {
scanf("%s",steps);
int face_now = towards[start],flag = 0;
for (int i = 0; steps[i]; ++ i) {
if (steps[i] == 'L')
face_now = (face_now+3)%4;
if (steps[i] == 'R')
face_now = (face_now+1)%4;
if (steps[i] == 'F') {
xx = x+dxy[face_now][0];
yy = y+dxy[face_now][1];
if (xx < 0 || xx > n || yy < 0 || yy > m) {
if (maps[x][y])
continue;
maps[x][y] = 1;
flag = 1;
break;
}
x = xx;
y = yy;
}
}
printf("%d %d %c",x,y,face[face_now]);
if (flag)
printf(" LOST");
printf("\n");
}
return 0;
}
UVa 118 - Mutant Flatworld Explorers
标签:
原文地址:http://blog.csdn.net/mobius_strip/article/details/45049733