(bfs与状态转移)程序设计思维与实践 Week 2 作业:A Maze、B PourWater

标题 程序设计思维与实践 Week 2 作业:A Maze、B PourWater

A 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)

Hint

坐标(x, y)表示第x行第y列,行、列的编号从0开始,且以左上角为原点。
另外注意,输出中分隔坐标的逗号后面应当有一个空格。

思路:

地图中的每个位置都可以理解为二维坐标系中的一点,人物在地图中移动即为图的遍历,这里用bfs宽搜进行模拟。构造两个长度为4的数组模拟任一点上、下、左、右四种移动方向。二维结构体数组用于记录路径中每个点的前序节点。

二维数组map为点的状态数组,每个点有“0”和“1”两种状态,只有“0”点可以被访问,在样例输入时即把为墙的点状态置为“1”。bfs遍历地图,每次取队列首元素进行四种移动状态遍历,到达“0”点则把新到达的点加入队列,并在map对应位置中赋值“1”。

队列空或达到目标点则结束,递归输出路径。

代码:

#include<iostream>
#include<string.h>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<iomanip>
#include<algorithm>
/*
地图问题; 
*/ 
using namespace std;
struct Point
{//二维坐标点
    int x,y; 
}; 
//人物可以移动的四种方向;
int dx[]={0, 1, 0,-1}; 
int dy[]={-1,0, 1, 0}; 
Point farpoint[5][5];//记录路径上每个坐标点的前序坐标; 

void outputPath(Point a[][5],int x,int y)
{//递归输出路径;
    if(x==0 && y==0) return;
	else{
		outputPath(a, a[x][y].x, a[x][y].y);
		cout<<'('<<a[x][y].x<<", "<<a[x][y].y<<')'<<endl;
	}		
}

int main()
{
	int map[5][5];//地图
	queue<Point> q;//节点队列;
	int vis[5][5];//访问过的节点//0表示没有到过,1表示不能访问; 
	memset(vis,0,sizeof(vis));//初始化
	 
	for(int i=0; i<5; i++)
	    for(int j=0; j<5; j++){//输入地图数据; 
	    	cin>>map[i][j];
	    	if(map[i][j] == 1) vis[i][j]=1;//标记墙,墙不能访问; 
		}
	//原点初始化并加入队列; 
	vis[0][0]=1;
	Point point={0,0};
	q.push(point);
	while(! q.empty()){
		Point sp=q.front();
		q.pop();
		for(int i=0; i<4; i++)
		{//人物上下左右四个方向移动 
		    int newx=sp.x+dx[i],
		        newy=sp.y+dy[i];
		    if(newx>=0&&newx<=4 && newy>=0&&newy<=4 &&vis[newx][newy]!=1 )
		    {//找到新到达的点; 
		    		vis[newx][newy]=1;//标记访问过;
					
					farpoint[newx][newy]=sp;//记录前序坐标; 
		            if(newx==4 && newy==4){//已到达目标点; 
		                while(! q.empty()) q.pop();//清空队列; 
						break;//退出循环 
					}
					point={newx,newy};
					q.push(point);//加入堆栈;
			}
		}
	}
	outputPath(farpoint,4,4);//输出路径; 
	cout<<'('<<4<<", "<<4<<')'<<endl;
	return 0;
 } 

———————————————————————————————————

B PourWater:

题目描述:

倒水问题 “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

Notes:

如果你的输出与Sample Output不同,那没关系。对于某个"A B C"本题的答案是多解的,不能通过标准的文本对比来判定你程序的正确与否。 所以本题由 SPJ(Special Judge)程序来判定你写的代码是否正确。

思路:

A,B两个杯子,有限的转移种类,有限步数到达C的状态,隐式图问题!构造状态结构体status,mp将每一种状态与bool值对应,true代表该状态之前已到达,反之未到达。为了知道每种状态的前序状态,map结构fromstatus记录每一种状态的前序状态。为了最后输出操作过程,outputmap记录到达每一种状态的操作。

利用队列宽搜,取队列首状态结构体进行六种状态的操作,对新到达的每一种状态进行判断,若某种操作后到达之前未到达的状态,记录该操作,记录新状态的前序状态,map中置true,并将新状态加入队列。若新到达状态为目标状态,循环终止。

递归输出操作流程。

代码:

#include<iostream>
#include<string.h>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
/*
倒水问题,两个杯子容量 A<=B  将一个杯子倒入量为 C<=B 的水; 
隐藏图问题 每一瞬间两个杯子构成一种状态;
倒水即为状态转移,即图中节点到节点的遍历,共有六种状态变化; 
*/
//needed struct: 
int A,B,C;//A and B capacity// C target;
struct sta
{//状态结构体;
	int a,b;//the current volumn 
	sta(){};
	sta(int x,int y){
		a=x;b=y;
	 } 
	bool operator < (const sta &x) const{
	    if(a != x.a)return a<x.a;
	    else return b<x.b;
	}
	//six types of sta transformations:
	sta fillA()
	{
	    return sta(A,b); 
	} 
	sta fillB()
	{
		return sta(a,B);
	}
	sta emptyA()
	{
		return sta(0,b);
	}
	sta emptyB()
	{
		return sta(a,0);
	}
	sta AtoB(){//pour A B
		int newa=max(a+b-B,0);
		int newb=min(a+b,B);
		sta result(newa,newb);
		return result;
	}
	sta BtoA(){//pour B A 
		int newa=max(A,a+b);
		int newb=min(a+b-A,0);
		sta result(newa,newb);
		return result;
	}
}; 
queue<sta> q;//sta queue 
map<sta, bool> mp;// if one sta once visited; 
map<sta, sta> fromstatus;//the pre-sta of one sta
map<sta, string> outputmap;//process map; 

void output(sta a){//recursively output the process to sta a 
	 if (outputmap.find(a) != outputmap.end()) {
        output(fromstatus[a]);
        cout<<outputmap[a]<<endl;
     }
}

void bfsProcessing(int a,int b){//pouring water,sta transformation; 
sta t(a,b);q.push(t);
mp[t]=true;
while(!q.empty()){
	sta curStatus=q.front();q.pop();
	if(curStatus.a == C||curStatus.b == C){//reach target sta 
	    output(curStatus);
		return;	
	} 
	//else pour water 
	sta New=curStatus.fillA();
	if(mp[New] == false){
		mp[New]=true;
		q.push(New);
		fromstatus[New]=curStatus;
		outputmap[New]="fill A"; 
	} 
	New=curStatus.fillB();
	if(mp[New] == false){
		mp[New]=true;
		q.push(New);
		fromstatus[New]=curStatus;
		outputmap[New]="fill B"; 
	} 
	New=curStatus.emptyA();
	if(mp[New] == false){
		mp[New]=true;
		q.push(New);
		fromstatus[New]=curStatus;
		outputmap[New]="empty A"; 
	} 
	New=curStatus.emptyB();
	if(mp[New] == false){
		mp[New]=true;
		q.push(New);
		fromstatus[New]=curStatus;
		outputmap[New]="empty B"; 
	}
	New=curStatus.AtoB();
	if(mp[New] == false){
		mp[New]=true;
		q.push(New);
		fromstatus[New]=curStatus;
		outputmap[New]="pour A B"; 
	} 
	New=curStatus.BtoA();
	if(mp[New] == false){
		mp[New]=true;
		q.push(New);
		fromstatus[New]=curStatus;
		outputmap[New]="pour B A"; 
	} 
 }	
}


int main()
{
    while(cin>>A>>B>>C){//initialize
	    mp.clear();
	    fromstatus.clear();
        outputmap.clear();
	    while(!q.empty())
	        q.pop();
	    bfsProcessing(0,0);
		cout<<"success"<<endl;	
	}
	return 0;
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值