poj1753模拟

题目链接http://poj.org/problem?id=1573

题意:从第一行第k个出发按照已给的方向前进,问第几步走出去或第几步进入一个有多少步的循环。

就是按照题意模拟就好了。

代码写完了wa了好多次,最后找到坑点是把出发点标记成了0,导致错误。

错误数据:2 2 1

     SW

     EN

    0 step(s) before a loop of 4 step(s)
下面是我写的代码:
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
struct T
{
    int flag,node;
} map[20][20];
int sum,ans,n,m,flag;
int main()
{
    int k,i,j,d;
    char s;
    while(cin>>n>>m>>k)
    {
        sum=0;
        ans=0;
        if(n==0&&m==0&&k==0)
            break;
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=m; j++)
            {
                cin>>s;
                if(s=='E')
                    d=1;
                if(s=='S')
                    d=2;
                if(s=='W')
                    d=3;
                if(s=='N')
                    d=4;
                map[i][j].flag=d;
                map[i][j].node=0;
            }
        }
        int a=1,b=k;
        while(1)
        {
            switch(map[a][k].flag)
            {
            case 1:
                k++;
                break;
            case 2:
                a++;
                break;
            case 3:
                k--;
                break;
            case 4:
                a--;
                break;
                default :break;
            }
            sum++;
            if(a<=0||a>n||k<=0||k>m)
            {
                printf("%d step(s) to exit
",sum);
                break;
            }
            else
            {
                if(map[a][k].node||a==1&&b==k)
                {
                    ans=map[a][k].node;
                    printf("%d step(s) before a loop of %d step(s)
",ans,sum-ans);
                    break;
                }
                else
                    map[a][k].node=sum;
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/yuanbo123/p/5448308.html