POJ 1573 Robot Motion

来源:互联网 发布:碧凡化妆品专营店 知乎 编辑:程序博客网 时间:2024/05/16 09:42

水模拟题一只。。。

#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);    }}


原创粉丝点击