这一题还是模拟题,在oj出现错误,一直到现在才发现,是条件的判定顺序是错误的,应该判断是不是越界,再判定是不是访问过。~~~~~
传上来有乱码,这是原始代码 http://my.csdn.net/enigma_hao/code/detail/54876,有注释的,但跟跟正确代码有区别的,注释可以参考
#include <iostream>
#define MAX 13
using namespace std;
/*288K 0MS*/
typedef struct _position
{
int x;
int y;
}position;
//function
position GetPos(char pa);
int main()
{
int row,col,enter;
while(cin>>row>>col>>enter)
{
if(row==0&&col==0&&enter==0) break;
char a[MAX][MAX];
int c[MAX][MAX]; //
for(int i=0;i<row;i++)
cin>>a[i];
//?????
memset(c,0,sizeof(c));
int x,y;//?????????
x=0;y=enter-1;
bool f1,f2;
f1=f2=true;
int counter=0;
while(f1&&f2)
{
if(x<0||y<0||x>=row||y>=col)
{
f2=false; //???????
break;
}
if(c[x][y]!=0)
{
f1=false; //????????
break;
}
char now=a[x][y];
position p=GetPos(now);
counter++; //???
c[x][y]=counter;
//???????????????
x+=p.x;
y+=p.y;
}
if(!f2)
cout<<counter<<" step(s) to exit"<<endl;
else if(!f1)
cout<<c[x][y]-1<<" step(s) before a loop of "<<counter-c[x][y]+1<<" step(s)"<<endl;
}
system("pause");
return 0;
}
position GetPos(char pa)
{
position p;
switch(pa)
{
case 'N':
p.x=-1;p.y=0;
break;
case 'W':
p.x=0;p.y=-1;
break;
case 'S':
p.x=1;p.y=0;
break;
case 'E':
p.x=0;p.y=1;
break;
default:
break;
}
return p;
}
原始代码:
#include <iostream>
#define MAX 13
using namespace std;
typedef struct _position
{
int x;
int y;
}position;
//function
position GetPos(char pa);
int main()
{
int row,col,enter;
while(cin>>row>>col>>enter)
{
if(row==0&&col==0&&enter==0) break;
char a[MAX][MAX];
int c[MAX][MAX]; //指的是当前位置第一次被访问,0代表是的没被访问
for(int i=0;i<row;i++)
cin>>a[i];
//初始化程序
memset(c,0,sizeof(c));
int x,y;//当前光标所在的位置
x=0;y=enter-1;
bool f1,f2;
f1=f2=true;
int counter=0;
while(f1&&f2)
{
if(c[x][y]!=0)
{
f1=false; //代表第一种的错误
break;
}
if(x<0||y<0||x>=row||y>=col)
{
f2=false; //代表第二种错误
break;
}
char now=a[x][y];
position p=GetPos(now);
counter++; //计数器
c[x][y]=counter;
//改变当前的光标的横坐标和纵坐标
x+=p.x;
y+=p.y;
}
if(!f2)
cout<<counter<<" step(s) to exit"<<endl;
else if(!f1)
cout<<c[x][y]-1<<" step(s) before a loop of "<<counter-c[x][y]+1<<" step(s)"<<endl;
}
system("pause");
return 0;
}
position GetPos(char pa)
{
position p;
switch(pa)
{
case 'N':
p.x=-1;p.y=0;
break;
case 'W':
p.x=0;p.y=-1;
break;
case 'S':
p.x=1;p.y=0;
break;
case 'E':
p.x=0;p.y=1;
break;
default:
break;
}
return p;
}