您的位置:首页 > 其它

HDOJ1035 Robot Motion(模拟题,字符串)

2017-08-11 22:28 465 查看
HDOJ 1035

给出地图规模n * m, 给出入口坐标(0,y),遵循以下规则,求解机器人能否走出地图。若能,输出走出地图所需要的步数,若不能,输出进入循环前走的步数和循环的步数。

规则:

若当前格子为N,则只能向上走,若为S向下走,E向右走,W向左走。

模拟题:

#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
char mp[20][20];
int mat[20][20];

int main(){
int n,m,t,i,step;
while(cin>>n>>m>>t &&(n||m)){
int flag=0;
memset(mat,0,sizeof(mat));
memset(mp,0,sizeof(mp));
for(i=0;i<n;i++)
scanf("%s",mp[i]);
step=1;
int x,y,loop=0;
x=0,y=t-1; //起点坐标
while(1){
if(mp[x][y]=='N' && !mat[x][y]){
mat[x][y]=step;
x--;
}
else if(mp[x][y]=='S'&&!mat[x][y]){
mat[x][y]=step;
x++;
}
else if(mp[x][y]=='E'
c130
&&!mat[x][y]){
mat[x][y]=step;
y++;
}
else if(mp[x][y]=='W'&&!mat[x][y]){
mat[x][y]=step;
y--;
}
else if(mat[x][y]){  //到达了重复点
step--;
loop=step-mat[x][y]+1;
flag=1;
break;
}
else if(x<0 ||y<0 ||x>=n ||y>=m){
step--;
break;
}
step++;
}
if(flag)
printf("%d step(s) before a loop of %d step(s)\n",step-loop,loop);
else
printf("%d step(s) to exit\n",step);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: