标签:简单模拟 c++ 机器人大法好 每日一水 不知道说什么

3 6 5 NEESWE WWWESS SNWWWW 4 5 1 SESWE EESNW NWEEN EWSEN 0 0
10 step(s) to exit 3 step(s) before a loop of 8 step(s)
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char map[20][20]; //存取地图
int step[20][20]; //记录当前走的总步数
int n,m,start;
bool is_inmap(int x,int y) //判断是否走出边界了
{
if(!(x>=0&&y>=0&&x<n&&y<m))
{
return false;
}
else
{
return true;
}
}
int main()
{
while(scanf("%d%d",&n,&m)&&n)
{
scanf("%d",&start);
memset(map,0,sizeof(map));
memset(step,-1,sizeof(step)); //这里把数组里面每一个值设置为-1
for(int i=0;i<n;i++)
scanf("%s",map[i]);
int AnsSteps = 0; //用来存取走的总步数
step[0][start-1]=0;
int x=0,y=start-1; //记录起点的坐标
while(1)
{
if(map[x][y]=='N')
{
x--;
}
else if(map[x][y]=='S')
{
x++;
}
else if(map[x][y]=='E')
{
y++;
}
else if(map[x][y]=='W')
{
y--;
}
AnsSteps++;
if(!is_inmap(x,y)) //是否还在地图当中
{
printf("%d step(s) to exit\n",AnsSteps);
break;
}
if(step[x][y]==-1) //step[x][y]等于-1说明当前点没有走过则换成AnsSteps
step[x][y]=AnsSteps;
else //否则已经访问过,有死循环,输出
{
printf("%d step(s) before a loop of %d step(s)\n",step[x][y],AnsSteps-step[x][y]);
break;
}
}
}
}
HDU-1035-Robot Motion(开两个数组简单模拟,话说最近一直再做模拟......C++)
标签:简单模拟 c++ 机器人大法好 每日一水 不知道说什么
原文地址:http://blog.csdn.net/qq_16542775/article/details/46273295