当前位置: 代码迷 >> 综合 >> HDOJ1035 Robot Motion(模拟题,字符串)
  详细解决方案

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

热度:80   发布时间:2023-11-08 17:44:04.0

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'&&!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);elseprintf("%d step(s) to exit\n",step);}return 0;
}