其他分享
首页 > 其他分享> > poj 1573(水题)

poj 1573(水题)

作者:互联网

#include<iostream>
#include<cstring>
using namespace std;
char ch[15][15];
int data[15][15],hasExit,hasLoop; 
int r,c,colNum,exitStep,loopStep;
int main(){
    while(scanf("%d%d%d",&r,&c,&colNum)==3&&r){
        for(int i=0;i<r;i++){
            scanf("%s",ch[i]);
        }
        memset(data,0,sizeof data);
        exitStep = loopStep = 0;
        int p = 0,q = colNum-1;
        hasExit = hasLoop = false;
        data[p][q] = ++exitStep;
        while(true){
            exitStep++;
            if(ch[p][q]=='N'){
                if(p>0&&!data[p-1][q]){
                    data[p-1][q] = exitStep;
                    p--;
                }
                else if(p==0){
                    hasExit = true;
                    break;
                }
                else{
                    loopStep = exitStep - data[p-1][q];
                    hasLoop = true;
                    break;
                }
            }
            else if(ch[p][q]=='S'){
                if(p<r-1&&!data[p+1][q]){
                    data[p+1][q] = exitStep;
                    p++;
                }
                else if(p==r-1){
                    hasExit = true;
                    break;
                }
                else{
                    loopStep = exitStep - data[p+1][q];
                    hasLoop = true;
                    break; 
                }
            }
            else if(ch[p][q]=='W'){
                if(q>0&&!data[p][q-1]){
                    data[p][q-1] = exitStep;
                    q--;
                }
                else if(q==0){
                    hasExit = true;
                    break;
                }
                else{
                    loopStep = exitStep - data[p][q-1];
                    hasLoop = true;
                    break;
                }
            }
            else if(ch[p][q]=='E'){
                if(q<c-1&&!data[p][q+1]){
                    data[p][q+1] = exitStep;
                    q++;
                }
                else if(q==c-1){
                    hasExit = true;
                    break;
                }
                else{
                    loopStep = exitStep - data[p][q+1];
                    hasLoop = true;
                    break;
                }
            }
        }
        if(hasExit){
            printf("%d step(s) to exit\n",exitStep-1);
        }
        if(hasLoop){
            printf("%d step(s) before a loop of %d step(s)\n",exitStep-1-loopStep,loopStep);
        }
    }
    return 0;
}

 

标签:水题,int,true,exitStep,else,break,poj,1573,data
来源: https://www.cnblogs.com/stevenzrx/p/15639891.html