week2作业

问题一:maze

问题描述

东东有一张地图,想通过地图找到妹纸。地图显示,0表示可以走,1表示不可以走,左上角是入口,右下角是妹纸,这两个位置保证为0。既然已经知道了地图,那么东东找到妹纸就不难了,请你编一个程序,写出东东找到妹纸的最短路线。

Input
  输入是一个5 × 5的二维数组,仅由0、1两数字组成,表示法阵地图。
Output
  输出若干行,表示从左上角到右下角的最短路径依次经过的坐标,格式如样例所示。数据保证有唯一解。
Sample Input

0 1 0 0 0
0 1 0 1 0
0 1 0 1 0
0 0 0 1 0
0 1 0 1 0

Sample Output

(0, 0)
(1, 0)
(2, 0)
(3, 0)
(3, 1)
(3, 2)
(2, 2)
(1, 2)
(0, 2)
(0, 3)
(0, 4)
(1, 4)
(2, 4)
(3, 4)
(4, 4)

题意解析

很标准的求最短路径问题,可以直接使用广搜解决。但要注意在初始情况下已经又被标记为“1”的点。在初始化vis数组时要注意。
同时该题要求记录路径,记录每个点的上一个点来递归输出即可。

做法

我使用了5x5的一维数组来存储这个方阵(但事后想想这完全没有必要,甚至它还增加了一些不必要的思考量),这里比较麻烦的是要判断可以往哪个方向移动。
向右即为+1,向左即为-1,向上为-5,向下为+5,这一点比较容易考虑,但是可能会出现类似第二行的最后一个数加一变成第三行第一个数这种不符合规则的情况产生,所以要对这种情况进行特判。
特判条件为:当移动后与移动前的差的绝对值为一时,如果移动前后不在同一行,则该移动不符合规则。
加入这个判断后即可。
对于路径记录,我使用了数组解决,即s[当前状态]=上一状态,然后倒着返回到起始点输出即可。

代码

#include <iostream>
#include <queue>
#include <cmath>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
bool a[26];
int c[26];
int d[4]={1,-1,5,-5};
int main(int argc, char** argv) {
	for(int i=1;i<=25;i++)	cin>>a[i];
	queue<int> t;
	t.push(1);
	a[1]=1;
	while(!t.empty())
	{
		int tem;
		for(int i=0;i<4;i++)
		{
			tem=t.front();
			tem=tem+d[i];
			if(((tem>25)||(tem<1)||(a[tem]==1))||((abs(tem-t.front())==1)&&(((tem-1)/5)!=((t.front()-1)/5))))
				continue;
			a[tem]=1;//标记为已经走过 
			c[tem]=t.front();//记录它的上一个位置
			t.push(tem);//入队列 
			if(tem==25)
				break;
		}
		t.pop();//出队列 
		if(tem==25)
		break;
	}
	int e[25];
	int i=0;
	int tt=25;
	while(tt!=1)//倒着找 
	{
		e[i]=c[tt];//上一个点
		tt=e[i];
		i++; 
	}
	for(int j=i-1;j>=0;j--)
	{
		cout<<"("<<(e[j]-1)/5<<", "<<(e[j]-1)%5<<")"<<endl;
	}
	cout<<"(4, 4)";
	return 0;
}

问题二:pour water

问题描述

倒水问题 “fill A” 表示倒满A杯,"empty A"表示倒空A杯,“pour A B” 表示把A的水倒到B杯并且把B杯倒满或A倒空。

Input
输入包含多组数据。每组数据输入 A, B, C 数据范围 0 < A <= B 、C <= B <=1000 、A和B互质。
Output
你的程序的输出将由一系列的指令组成。这些输出行将导致任何一个罐子正好包含C单位的水。每组数据的最后一行输出应该是“success”。输出行从第1列开始,不应该有空行或任何尾随空格。

Sample Input

2 7 5
2 7 4

Sample Output

fill B
pour B A
success 
fill A
pour A B
fill A
pour A B
success

题意解析

这也是一个很标准的广搜+记录路径的题目。将(0,0)作为初始状态,(x,C)或者(C,x)作为结束状态进行广搜。同时开一个数组记录当前状态的上一状态。
但要注意的是该题目要求的是输出到达该状态所需要的“操作”,所以在记录状态时也要记录操作。

做法

我使用了结构体来存储当前的状态,包含x,y(分别表示A和B杯子)和一个字符串(用来记录操作)。使用map作为vis数组来记录某状态是否已经到达过。
对于六种操作(fill A等),我写了六个函数,同时利用函数指针分别指向这六个函数来达成遍历操作的目的。
但是令人沮丧的是,我使用map作为vis数组使用find方法会导致超时(但事实上有些人使用map并没有超时),所以我又换用了bool数组来记录某状态是否达到过,将状态压缩为1000*x+y保证没有重复状态。

代码

#include <iostream>
#include <map>
#include <queue>
#include <cstring>
using namespace std;
struct beizi{
	int x,y;
	string s;
	
	beizi()	{x=0;y=0;s=" ";};
	bool operator < (const beizi& bb) const
	{
        return y*1000+x<bb.y*1000+bb.x;
	}
};
map<beizi,beizi> m;
bool jjudge[1001010];
int a,b,c,judge;//两个杯子 
beizi atob(beizi tem,int i)
{
	if(tem.x==0) return tem;
	else
	{
		if(tem.x>b-tem.y)//a杯子中的水能倒满b杯子
		{
			tem.x=tem.x-(b-tem.y);			
			tem.y=b;//装满
		}
		else
		{
			tem.y=tem.y+tem.x;
			tem.x=0;
		} 
		tem.s="pour A B";
		judge=1;
		return tem;
	} 
}
beizi btoa(beizi tem,int i)
{
	if(tem.y==0) return tem;
	else
	{
		if(tem.y>a-tem.x)//b杯子中的水能倒满a杯子
		{
			tem.y=tem.y-(a-tem.x);	
			tem.x=a;//装满
		}
		else
		{
			tem.x=tem.y+tem.x;
			tem.y=0;
		} 
		tem.s="pour B A";
		judge=1;
		return tem;
	} 
}
beizi atovoid(beizi tem,int i)
{
	if(tem.x==0) return tem;
	else 
	{
		tem.x=0;
		tem.s="empty A";
		judge=1;
		return tem;
	}
}
beizi btovoid(beizi tem,int i)
{
	if(tem.y==0) return tem;
	else 
	{
		tem.y=0;
		tem.s="empty B";
		judge=1;
		return tem;
	}
}
beizi fa(beizi tem,int i)
{
	if(tem.x==a) return tem;
	else
	{
		tem.x=a;
		tem.s="fill A";
		judge=1;
		return tem;
	}
}
beizi fb(beizi tem,int i)
{
	if(tem.y==b) return tem;
	else
	{
		tem.y=b;
		tem.s="fill B";
		judge=1;
		return tem;
	}
}
void output(beizi tem)
{
	if((tem.x==0)&&(tem.y==0))
	return ;
	output(m[tem]);
	cout<<tem.s<<endl;
}
beizi (*p[6])(beizi tem,int i)={atob,btoa,atovoid,btovoid,fa,fb};


int main(int argc, char** argv) {
	int tjudge=0;
	while(cin>>a>>b>>c)
	{
		memset(jjudge,false,sizeof(jjudge));
		if(c==0)
		{
			cout<<"success"<<endl;
			continue;
		}
		tjudge=0;
		m.clear();
		queue<beizi> dui;
		beizi tem;
		tem.x=0;tem.y=0;tem.s=" ";
		m[tem]=tem;
		jjudge[0]=1;
		dui.push(tem);
		while(!dui.empty())
		{
			for(int i=0;i<6;i++)
			{
				judge=0;
				beizi tem=dui.front();
				beizi ttem=tem;
				tem=p[i](tem,i);
				if(judge==0)
					continue;
				map<beizi,beizi>::iterator pp=m.find(tem);
				if(jjudge[1000*tem.y+tem.x]==0)
				{ 
					dui.push(tem);
					m[tem]=ttem;
					jjudge[1000*tem.y+tem.x]=1;	
				}
				if((tem.x==c)||(tem.y==c))
				{		
					output(tem);
					cout<<"success"<<endl;
					tjudge=1;
					break;
				}
			}
			dui.pop();
			if(tjudge)
				break;
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值