poj 1573 Robot Motion

http://poj.org/problem?id=1573

简单的搜索题就不说了;因为"whether or not the number before it is 1"wrong了两次;啊!!

代码:

View Code
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
bool ok = 0;
void print1(int n)
{
printf("%d step(s) to exit\n",n);
ok = 1;
}
void print2(int a,int b)
{
printf("%d step(s) before a loop of %d step(s)\n",b - 1,a - b +1);
ok =1;
}
int main()
{
int y = 0;
int x = 0;
int sta = 0;
char s[15][15] ={NULL};
int visit[20][20] = {0};
int a = 0;
int b = 0;

while(scanf("%d%d%d",&y,&x,&sta),x&&y&&sta)
{
ok = 0;
memset(visit,0,sizeof(visit));
a = y;
b = sta;
visit[a][b] = 1;
for(int i = y; i >= 1; --i)
scanf("%s",s[i]+1);
while(1)
{
switch(s[a][b])
{
case 'E':{if(b+1 > x) print1(visit[a][b]);
else if(visit[a][b+1]) print2(visit[a][b],visit[a][b+1]);
else {visit[a][b+1] = visit[a][b] +1;b++;}}break;
case 'W':{if(b-1 < 1) print1(visit[a][b]);
else if(visit[a][b-1]) print2(visit[a][b],visit[a][b-1]);
else {visit[a][b-1] = visit[a][b] +1;b--;}}break;
case 'N':{if(a+1 > y) print1(visit[a][b]);
else if(visit[a+1][b]) print2(visit[a][b],visit[a+1][b]);
else {visit[a+1][b] = visit[a][b] +1;a++;}}break;
case 'S':{if(a-1 < 1) print1(visit[a][b]);
else if(visit[a-1][b]) print2(visit[a][b],visit[a-1][b]);
else {visit[a-1][b] = visit[a][b] +1;a--;}}break;

}
if(ok)
break;
}
}
return 0;
}



posted @ 2012-02-26 22:14  LT-blogs  阅读(143)  评论(0编辑  收藏  举报