Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

1A水过

Posted by xd560 at 2015-02-09 19:35:21 on Problem 1573
/*
560
*/
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>

using namespace std;

const int maxn=500;
char map[maxn][maxn];
int vis[maxn][maxn];
int stp[maxn][maxn];
int n,m,st;

void go(int i,int j,int step)
{
    if(vis[i][j]==0){
        cout<<step-1<<" step(s) to exit"<<endl;
        return;
    }
    if(vis[i][j]==1){
        int pre=stp[i][j]-1;
        int loop=step-stp[i][j];
        cout<<pre<<" step(s) before a loop of "<<loop<<" step(s)"<<endl;
        return;
    }
    vis[i][j]=1;
    stp[i][j]=step;
    switch(map[i][j]){
        case 'N':i--;break;
        case 'S':i++;break;
        case 'E':j++;break;
        case 'W':j--;break;
    }
    go(i,j,step+1);
}

int main()
{
    while(cin>>n>>m>>st,n||m||st){
        memset(vis,0,sizeof(vis));//0 wall
        memset(stp,0,sizeof(stp));
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>map[i][j];
                vis[i][j]=-1;
            }
        }
        go(1,st,1);
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator