您的位置:首页 > 其它

poj 1573 Robot Motion_模拟

2013-07-24 22:01 344 查看
又是被自己的方向搞混了

题意:走出去和遇到之前走过的就输出。

#include <cstdlib>
#include <iostream>
#include<cstdio>
using namespace std;
#define N 110
int map

,visit

,n,m,flag;//n为x轴 m为y轴
int dir[][2]={{0,1},{1,0},{0,-1},{-1,0}};//e,s,w,n
int setdire(char s){
switch(s){
case 'E':return 0;
case 'S':return 1;
case 'W':return 2;
case 'N':return 3;
}
}
void dfs(int x,int y,int num){
int i,j,tx,ty;
if(flag!=1)
return;
tx=x+dir[map[x][y]][0];
ty=y+dir[map[x][y]][1];
if(tx>=0&&tx<n&&ty>=0&&ty<m){
if(visit[tx][ty]){
printf("%d step(s) before a loop of %d step(s)\n",visit[tx][ty]-1,num-visit[tx][ty]+1);
flag=0;
}
else{
visit[x][y]=num;
dfs(tx,ty,num+1);
}

}
else{
printf("%d step(s) to exit\n",num);
flag=0;
}
}
int main(int argc, char *argv[])
{
int i,j,x;
char str
;
while(scanf("%d%d%d",&n,&m,&x)&&(n||m||x)){
for(i=0;i<n;i++){
scanf("%s",str);
for(j=0;j<m;j++){
map[i][j]=setdire(str[j]);
}
}
memset(visit,0,sizeof(visit));
flag=1;
dfs(0,x-1,1);
}
system("PAUSE");
return EXIT_SUCCESS;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: