作业一:1.最短路线 2.取水问题。

1、最短路线:
5x5矩阵,0为可通过,1为障碍,从(0,0)出发,到达(4,4),寻找最短路线并输出。
如:
输入:
输入
输出:
在这里插入图片描述
此题主要用队列来解决(使用到的库:queue):当从某一点(x,y)出发时,考虑这四个点:(x-1,y)(x+1,y)(x,y-1)(x,y+1),如果这些点未超出边界(此题中0<=x<=4,0<=y<=4)或未遇到“障碍”时,就将这个点加入队列中,并将这个点的父节点设置为(x,y),然后将(x,y)标记为已经过(一般这一步通过数组实现,也可调用map)并踢出队列。此时从队首取出下一个点(x’,y’),继续考虑(x’,y’)周围四个点…重复上面步骤直到出现目标点为止,可以思考,这时的路径就是(x,y)到目标点的最短路径。
对于此题,为了输出方便,我们将(4,4)视作起点,将(0,0)视为终点 ,执行以上步骤,输出时只要输出(0,0)然后不断输出父节点直到(4,4)就可以得到最短路径了。

2.取水问题
“fill A” 表示倒满A杯,"empty A"表示倒空A杯,“pour A B” 表示把A的水倒到B杯并且把B杯倒满或A倒空。输入A,B,C的值,输出通过A,B操作最后取得C的过程。
如:
输入:在这里插入图片描述
输出:
在这里插入图片描述
与第1题相似,此题中我们可以将A,B桶的状态视为一个“点”,则初始状态为(0,0),从这个点开始向外辐射直到A,B某一桶中盛的水为C。此题的关键在于点的构建:
1、假设A桶中有水:将桶中水倒空,B保持不变,记做empty A,B同理记做emptyB,以下对A操作对B同理。
2、假设A桶中有水且不满:
①、加满A,记做fill A。
②、将B桶中水倒入A,记做pour B A(注意这里有两种情况,一种是A,B桶中水之和大于A桶,一种是小于A桶,要注意)。
每当做出一个操作时将操作保存,并将下一状态设为当前状态的父节点,当到达C时保存此时状态并退出,然后从保存状态开始向上寻找子节点,倒序输出操作即可。

#include <iostream>     //最短路线代码
#include <queue>
#include <stdio.h>
using namespace std;
struct point{    //点
	int x;
	int y;
};
struct fa{     //点的父节点,可用map
	int x;
	int y;
};
queue<point> Q;
fa father[5][5];
int vis[5][5];   //判断点是否已经经过,0为未经过,1为经过
int a[]={0,0,-1,1},
b[]={1,-1,0,0};     //a与b完成某一点向四周扩散的操作
int main(){
	int i,l;
	for(i=0;i<5;i++)           //输入
	for(l=0;l<5;l++){
	  	scanf("%d",&vis[i][l]);
	}
	Q.push({4,4});        //从点(4,4)开始寻找路径
	vis[4][4]=1;          //表示(4,4)已经到达
	point now;           //标记此时的位置
	int bl=1;            
	while(!Q.empty()){
		point now=Q.front();
		Q.pop();
		for(i=0;i<4;i++){       //完成周围四个点的扩散
			int x=now.x+a[i];
			int y=now.y+b[i];
			if(x>=0&&y>=0&&x<=4&&y<=4&&vis[x][y]!=1){   //可到达,加入队列
				Q.push({x,y});
				vis[x][y]=1;
				father[x][y].x=now.x;
				father[x][y].y=now.y;
				if(x==0&&y==0)bl=0;       //到达(0,0),跳出循环
			}
		}
		if(bl==0)break;
	}
	i=0;
	l=0;
	int ii;
	printf("(0, 0)\n");
    while(i!=4){      //输出路径
    	ii=i;
    	i=father[i][l].x;
    	l=father[ii][l].y;
    	printf("(%d, %d)\n",i,l);
	}
	return 0;
}
#include <iostream>     //取水问题代码
#include <stdio.h>
#include <queue>
#include <map>
#include <string>
using namespace std;
struct zt{          //A,B当前状态以及进行的操作
	int a;
	int b;
	int opr;
	bool operator<(const zt &s) const       //重载<符号
	{
		return a!=s.a ? a<s.a : b<s.b;
	}

};
//0,1...5对应操作
string pu[6]={"fill A","fill B","empty A","empty B","pour A B","pour B A"};
queue<zt> Q;
map<zt,zt> fa;    //map用来储存父节点
void found(zt a,zt b){             //判断b是否能加入
	if(fa.find(b)==fa.end()){     //queue中find函数,若在fa中未找到b则返回fa.end(),表示b不在fa中,可加入。
		fa[b]=a;
		Q.push(b);
	}
}
void bfs(int a,int b,int c){    //取水过程
	zt now,next,com;
	zt j;
	now.a=0;
	now.b=0;
	Q.push(now);         //将状态(0,0)放入队列
	while(!Q.empty()){
		now=Q.front();
		Q.pop();
		if(now.a==c||now.b==c){    //取得C,存于com并跳出
			com=now;
			break;
		}
		if(now.a>0){             //将A倒空
			next.a=0;
			next.b=now.b;
			next.opr=2;
			found(now,next);
		}
		if(now.b>0){           //将B倒空
			next.b=0;
			next.a=now.a;
			next.opr=3;
			found(now,next);
		}
		if(now.a<a){
			next.a=a;              //将A加满
			next.b=now.b;
			next.opr=0;
			found(now,next);
			if(now.b!=0){             //将B倒入A
				if(now.a+now.b<=a){
					next.a=now.a+now.b;
					next.b=0;
					next.opr=5;
					found(now,next);
				}
				else {
					next.a=a;
					next.b=now.a+now.b-a;
					next.opr=5;
					found(now,next);
				}
			}
		}
	    if(now.b<b){                 //与A同理
			next.b=b;
			next.a=now.a;
			next.opr=1;
			found(now,next);
			if(now.a!=0){
				if(now.a+now.b<=b){
					next.b=now.a+now.b;
					next.a=0;
					next.opr=4;
					found(now,next);
				}
				else {
					next.b=b;
					next.a=now.a+now.b-b;
					next.opr=4;
					found(now,next);
				}
			}
		}
	}
	zt test;
	test=com;
    vector<int> g;            //建立vector g储存操作
    g.push_back(com.opr);     //将com对应操作放入g中
    while(fa[test].a!=-1){    //从com向上寻找子节点并把对应操作放入g中
    	g.push_back(fa[test].opr);
    	test=fa[test];
	}
	for(int i=g.size()-2;i>=0;i--){  //将g对应pu中字符串并倒序输出
		cout<<pu[g[i]]<<endl;
	}
	cout << "success" << endl;
	fa.clear();
	while(!Q.empty())Q.pop();
} 
int main(){
	int a,b,c;
	while(cin>>a>>b>>c){
		bfs(a,b,c);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值