POJ 1573 Robot Motion 模拟 难度:0

 

#define ONLINE_JUDGE
#include<cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int A,B,sx,sy;
char maz[101][101];
int vis[101][101];
const int dx[4]={0,1,0,-1};
const int dy[4]={-1,0,1,0};

int dir(char ch){
    if(ch=='N')return 0;
    else if(ch=='E')return 1;
    else if(ch=='S')return 2;
    return 3;
}
void print(int step){
    for(int s=1;s<step;s++){
        for(int i=0;i<A;i++){
            for(int j=0;j<B;j++){
                if(vis[j][i]==s){
                    printf("vis[%d][%d]:%d\n",j,i,vis[j][i]);
                }
            }
        }
    }
}
void solve(){
    memset(vis,0,sizeof(vis));
    sx--;sy=0;
    int step=0;
    int fx,fy;
    while(!vis[sy][sx]&&sx>=0&&sx<A&&sy>=0&&sy<B&&++step){
        fx=sx;fy=sy;
        vis[sy][sx]=step;
        sx=dx[dir(maz[fy][fx])]+fx;
        sy=dy[dir(maz[fy][fx])]+fy;
    }
    if(sx<0||sy<0||sx>=A||sy>=B)printf("%d step(s) to exit\n",step);
    else {
        printf("%d step(s) before a loop of %d step(s)\n",vis[sy][sx]-1,step+1-vis[sy][sx]);
    }
}
int main(){
    #ifndef ONLINE_JUDGE
        freopen("output.txt","w",stdout);
    #endif // ONLINE_JUDGE
    while(scanf("%d%d%d",&B,&A,&sx)==3&&A&&B){

        for(int i=0;i<B;i++)scanf("%s",maz[i]);
        solve();
    }
    return 0;
}

  

posted @ 2015-08-24 11:50  雪溯  阅读(144)  评论(0编辑  收藏  举报