您的位置:首页 > 其它

POJ 1573 Robot Motion

2012-11-14 09:52 423 查看
水模拟题一只。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int cnt[1000][1000],n,m,lc,s;
char map[1000][1000];
int go(int x,int y)
{
while (1)
{
if (x < 0 || y < 0 || x >= n || y >= m)
{
printf("%d step(s) to exit\n",lc-1);
break;
}
if (cnt[x][y] != 0)
{
printf("%d step(s) before a loop of %d step(s)\n",cnt[x][y]-1,lc-cnt[x][y]);
break;
}
cnt[x][y]=lc;
lc++;
switch (map[x][y])
{
case 'N':
x--;
break;
case 'S':
x++;
break;
case 'E':
y++;
break;
case 'W':
y--;
break;
}
}
}
int main()
{
int i,j,k;
while (1)
{
scanf("%d%d%d",&n,&m,&s);
getchar();
if (n == 0 && m == 0 && s == 0)
{
break;
}
s--;
memset(cnt,0,sizeof(cnt));
for (i=0; i<n; i++)
{
gets(map[i]);
}
lc=1;
go(0,s);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: