POJ 1573 Robot Motion 水模拟

来源:互联网 发布:mac怎么移动文件 编辑:程序博客网 时间:2024/06/09 23:24

step后面的s不用讨论,~~~~不然wa.......


#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>#include <cmath>using namespace std;//0,1,2,3,N,W,S,Eint map[15][15];char a[15][15];int dx[] = {-1,0,1,0};int dy[] = {0,-1,0,1};int main(){    int n, m, k;    int i, j;    while(cin >> n >> m >> k){        memset(map,0,sizeof(map));        memset(a,0,sizeof(a));        if(n==0&&m==0&&k==0) break;        for(i = 0;i < n;i++){            cin >> a[i];        }        for(i = 1;i <= n;i++){            for(j = 1;j <= m;j++){                if(a[i-1][j-1] == 'N'){                    map[i][j] = 0;                }else if(a[i-1][j-1] == 'W'){                    map[i][j] = 1;                }else if(a[i-1][j-1] == 'S'){                    map[i][j] = 2;                }else if(a[i-1][j-1] == 'E'){                    map[i][j] = 3;                }            }        }        int x = 1, y = k;        int cnt[15][15];        memset(cnt,0,sizeof(cnt));        cnt[x][y] = 1;        int xx =1, yy = k;        while(1){            x = xx;            y = yy;            xx = x+dx[map[x][y]];            yy = y+dy[map[x][y]];            if(xx>=1&&xx<=n&&yy>=1&&yy<=m){                if(cnt[xx][yy] == 0){                    cnt[xx][yy] = cnt[x][y] + 1;                }else {                    int tt = cnt[x][y] - cnt[xx][yy] +1;                    printf("%d step(s) before a loop of %d step(s)\n",  cnt[xx][yy]-1, tt);                    break;                }            }else {                printf("%d step(s) to exit\n", cnt[x][y]);                break;            }        }    }    return 0;}

0 0