hdoj 1035 Robot Motion(简单题)

来源:互联网 发布:java 数组转字符串 编辑:程序博客网 时间:2024/05/21 07:50

我在输入字符二维数组的地方卡了一下

以后得习惯只用一个循环将每行看成一个字符串输入。而且最好不用scanf("\n"),容易卡住!

求从哪一步开始绕圈圈,设了b[11][11],每经过一个点(x,y)就将当前step赋给b[x][y]。b[x][y]也起到判断该点是否被访问过的作用。

#include<stdio.h>using namespace std;int main(){char a[11][11];int b[11][11];int r,c,s,i,j;int x,y,step;while(~scanf("%d%d%d",&r,&c,&s)&&r!=0){  step=0;   x=0;y=s-1;   for(i=0;i<r;i++)       for(j=0;j<c;j++)   b[i][j]=0;   for(i=0;i<r;i++)   scanf("%s",a[i]);         while(1)      {      step++;      if(a[x][y]=='N')      { b[x][y]=step;      x=x-1;       }       else if(a[x][y]=='S')       { b[x][y]=step;       x=x+1;       }       else if(a[x][y]=='E')       {b[x][y]=step;       y=y+1;        }        else    {b[x][y]=step;          y--;     }          if(x<0||x>=r||y<0||y>=c)          {          printf("%d step(s) to exit\n",step);break;          }          if(b[x][y]!=0)          {          printf("%d step(s) before a loop of %d step(s)\n",b[x][y]-1,step-b[x][y]+1);          break;          }      }  }return 0;}