POJ1573

来源:互联网 发布:怎么安装cad2008软件 编辑:程序博客网 时间:2024/05/22 15:20
#include <iostream>
#include <stdio.h>
#include <string.h>


using namespace std;
char gra[12][12];
int nowx, nowy;
int loop,count;
int r, c, pos;
bool flag = false;


bool check(int x, int y)
{


    if(x >= 1 && x <= r && y >= 1 && y<= c)
    {
        if(gra[x][y] > 57)
         return true;
        else
        {
            flag  = true;
            loop = gra[x][y]-'0';
        }
    }


    return false;
}


void fun(char a)
{
    gra[nowx][nowy] = count +'0';
    switch (a)
    {
        case 'W':
        {
            nowy--;
            break;
        }


        case 'N':
        {
            nowx--;
            break;
        }


        case 'E':
        {
           nowy++;
           break;
        }


        case 'S':
        {
            nowx++;
            break;
        }


    }
 //cout<<"x"<<nowx<<"y"<<nowy<<endl;
}


int main()
{


    int i, j;
    char s;




    while(scanf("%d %d %d",&r, &c, &pos), r && c && pos)
    {
        loop = 0;
        count = 0;
         flag = false;


        for(i = 1; i <= r; i++ )
         for( j = 1; j <= c ; j++)
           cin>>gra[i][j];


         nowx = 1;
         nowy = pos;


         while(check(nowx,nowy))
         {
             fun(gra[nowx][nowy]);
             count++;
         }


         if(flag)
         cout<<loop<<" step(s) before a loop of "<<count-loop<<" step(s)"<<endl;
        else
         cout<<count<<" step(s) to exit"<<endl;


    }




}
0 0