POJ1573 Robot Motion

一、思路分析

通过二维的字符数组来模拟路径图,同时在表示路径图的数组外增加一圈边界。记录机器人走到某位置时是第几步,以及走过的位置做好标记。当机器人走到边界的时候输出第一种结果。如果机器人走到原来标记过的位置,则输出第二种结果。


二、  方法设计

1、定义3个变量:high:路径图的宽   width:路径图的长   start:起点

2、用malloc动态申请3个二维数组:direction[][ ]数组用来输入NESW四个字母表示方向,用x表示边界;arrive[ ][ ]数组用0标记没有走过的路,用1标记走过的路;haspast[ ][ ]数组用来记录走过的地方是第几步,每一个位置都初始化为值1,边界为0。

3、用starthigh、startwidth记录当前位置,通过循环,如果走到边界x,则输出走过的步数;如果走到haspast[ ][ ]标记到1的位置,则说明是循环路径,输出进入循环前的步数值,并与总步数值相减,得到循环的步数值,并输出;除此之外,每一步都用switch判断下一步位置,下一步的步数值都与前一步的值(sum表示前一步的值)相加,并将haspast[ ][ ]的值赋为1。


三、代码实现

#include<iostream>
#include<malloc.h>
using namespace std;

int main()
{
	int high,width,start;
	while(cin>>high>>width>>start&&(high||width||start))
	{
		char **direction=(char **)malloc(sizeof(char)*(high+2)*(width+2));
		for (int i=0; i<high+2;i++)
			direction[i]=(char *)malloc(sizeof(char)*(width+2));
		int **haspast=(int **)malloc(sizeof(int)*(high+2)*(width+2));
		for(int i=0;i<high+2;i++)
			haspast[i]=(int *)malloc(sizeof(int)*(width+2));
		int **arrive= (int **) malloc(sizeof(int)*(high+2) *(width+2));
		for (int i=0; i<high+2; i++)
			arrive[i]= (int *) malloc(sizeof(int)*(width+2));
		//通过malloc动态定义三个数组,具体功能见上	
	
		for (int i=1; i<=high; i++)
			for(int j=1; j<=width; j++)
			{
				cin>>direction[i][j];
				haspast[i][j]=1;
				arrive[i][j]=0;
			}
		
		for(int i=0;i<width+2;i++)
		{
			direction[0][i]='x';
			direction[high+1][i]='x'; 
			haspast[0][i]=0;
			haspast[high+1][i]=0; 
		}
		
		for(int i=0;i<high+2;i++)
		{
			direction[i][0]='x';
			direction[i][width+1]='x';
			haspast[i][0]=0;
			haspast[i][width+1]=0; 
		}
		//初始化三个数组,direction[][]数组用WESN表示方位,x表示边界;arrive[][]数组用0表示未经过,1表示已经经过;haspast[][]数组用1初始化各个位置,0初始化边界。

		int starthigh=1,startwidth=start,sum=0;
		while(1)
		{
			if(direction[starthigh][startwidth]=='x')
			{
				if(direction[starthigh][startwidth]==1)
					cout<<haspast[starthigh][startwidth]<<" step to exit"<<endl;
				else
					cout<<haspast[starthigh][startwidth]<<" step(s) to exit"<<endl; 
				break;
			}
//如果机器人走到边界,则输出第一种结果,此时输出haspast[][]的值通过前面每一步依次加1,得到的结果就是所求步数
			else if(arrive[starthigh][startwidth]==1)
			{
				if(haspast[starthigh][startwidth]-1-sum==1)
					cout<<"1 step before a loop of ";
				else
					cout<<haspast[starthigh][startwidth]-1-sum<<" step(s) before a loop of ";
				if(2*sum+1-haspast[starthigh][startwidth]==1)
					cout<<"1 step"<<endl; 
				else
					cout<<2*sum+1-haspast[starthigh][startwidth]<<" step(s)"<<endl; 
				break;
			}
//如果机器人走到了arrive[][]标记到1的地方,说明走到了原本已经到达过的位置,就输出第二种结果。变量sum用来标记踏上重复路的前一步所走步数,假设下一步是之前走过的第a步,那么haspast[][]=a+sum,所以在重复前走的步数是a-1步,即haspast[][]-sum-1。走的重复的步数就是sum-(haspast[][]-sum-1)=2*sum+1-haspast[][]。

			else
			{
				arrive[starthigh][startwidth]=1;//走到的地方标注1
				switch(direction[starthigh][startwidth])
				{
					case 'N':
					{
						haspast[starthigh-1][startwidth]+=haspast[starthigh][startwidth];
						sum=haspast[starthigh][startwidth];
						starthigh--;
						break;
					} 
					case 'W':
					{
						haspast[starthigh][startwidth-1]+=haspast[starthigh][startwidth];
						sum=haspast[starthigh][startwidth];
						startwidth--;
						break;	
					} 
					case 'S':
					{
						haspast[starthigh+1][startwidth]+=haspast[starthigh][startwidth];
						sum=haspast[starthigh][startwidth];
						starthigh++;
						break;	
					} 
					case 'E':
					{
						haspast[starthigh][startwidth+1]+=haspast[starthigh][startwidth];
						sum=haspast[starthigh][startwidth];
						startwidth++;
						break;
					} 
				}
			}
//每一步通过switch判断走向,下一步的haspast[][]值加上前一步的值。
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值