首页 > 代码库 > POJ -1573 Robot Motion

POJ -1573 Robot Motion


题目链接: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;
}