hdu1035

来源:互联网 发布:mac层的作用 编辑:程序博客网 时间:2024/05/21 10:14
#include<cstdio>#include<algorithm>#include<iostream>using namespace std;const int N=11;int m,n,p;char map[N][N];int a[N][N];struct Point{       int x;       int y;      // int step;      // int visited;}point[N*N];int top;int judge(int x,int y){       if(x>=0&&x<n&&y>=0&&y<m&&a[x][y]==0)       return 0;       else if(a[x][y]==1)       return 1;       else        return 2;}       int main(){       char s[N];       int xx,yy,l;       while(scanf("%d%d%d",&n,&m,&p)!=EOF)       {               if(n==0&&m==0&&p==0) break;              top=-1;                                      memset(a,0,sizeof(a));              memset(map,0,sizeof(map));              memset(point,0,sizeof(point));                                         for(int i=0;i<n;i++)              {                     scanf("%s",s);//本来这跪了3边,CE,原来是不能用cin>>,只能用scanf输入                      for(int j=0;j<m;j++)                     {                             map[i][j]=s[j];                     }              }                     xx=0;              yy=p-1;                            while(!judge(xx,yy))              {                    // cout<<"("<<xx<<","<<yy<<")"<<endl;                                  top++;                                  point[top].x=xx;                     point[top].y=yy;                     a[xx][yy]=1;                                  if(map[xx][yy]=='E')                     {                             yy++;                     }                     else if(map[xx][yy]=='W')                     {                             yy--;                     }                     else if(map[xx][yy]=='S')                     {                             xx++;                     }                     else if(map[xx][yy]=='N')                     {                             xx--;                     }              }              if(judge(xx,yy)==2)              printf("%d step(s) to exit\n",top+1);              else if(judge(xx,yy)==1)              {                     int i;                     for( i=0;i<=top;i++)                     if(point[i].x==xx&&point[i].y==yy)                     break;                    // cout<<i<<endl;                     printf("%d step(s) before a loop of %d step(s)\n",i,top-i+1);              }       }       system("pause");       return 0;}