您的位置:首页 > 其它

POJ -1573 Robot Motion

2014-06-19 20:59 337 查看
题目链接:POJ 1573 Robot Motion



一个小模拟,很简单,按照提示一步步走就是了

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
using namespace std;

int n = 0,m = 0,st = 0;
int map[10][20],dis[10][20];;
bool vis[10][20];
int mv[4][2] = {{0,-1},{-1,0},{1,0},{0,1}};
void Xiao_MoNi(int w)
{
memset(vis,0,sizeof(vis));
int x = 0,y = w, x0 , y0;
int step = 0;
while(x>=0 && x<n && y>=0 && y<m && !vis[x][y])
{
vis[x][y] = true;
dis[x][y] = step;
step++;
x0 = x + mv[map[x][y]][0]; // 注意一下,x0 当时敲成了x,x变化了,彻底乱了
y0 = y + mv[map[x][y]][1];  //手残
x = x0;
y = y0;
}
if(x>=0 && x<n && y>=0 && y<m)
cout<<dis[x][y]<<" step(s) before a loop of "<<step - dis[x][y]<<" step(s)\n";
else
cout<<step<<" step(s) to exit\n";
}

int main()
{
char a[15];
while(scanf("%d%d%d",&n,&m,&st),n,m,st)
{
memset(dis,0,sizeof(dis));
for(int i = 0;i<n;i++)
{
scanf("%*c%s",a);
for(int j = 0;j<m;j++)
{
if(a[j]=='E')
map[i][j] = 3;
else if(a[j]=='W')
map[i][j] = 0;
else if(a[j]=='N')
map[i][j] = 1;
else if(a[j]=='S')
map[i][j] = 2;
}
}
st--;;
Xiao_MoNi(st);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: