「程序设计思维与实践」Week2 作业:A-Maze、B-Pour Water。BFS+路径记录输出+map+set+pair

7 篇文章 0 订阅
3 篇文章 0 订阅

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开始,且以左上角为原点。
另外注意,输出中分隔坐标的逗号后面应当有一个空格。

题解
  • 题意找到最短路线,所有路径的长度都是1,用bfs遍历第一次找到终点时即为最短路径。
  • 路径记录:创建一个pair<int,int>类型的数组,在将节点推入队列时,记录从哪个节点走到了当前节点。遍历完成后从终点逆序推到起点,按正向输出路径即可。
代码
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <stack>
using namespace std;

int maps[10][10];
pair<int, int> pre[10][10];
bool vis[10][10];

void mapsInput(){
	for(int i = 0; i <= 4; ++ i) {
		for(int j = 0; j <= 4; ++ j) {
			cin >> maps[i][j];
		}
	}

}

int dx[] = {0,0,-1,0,1};
int dy[] = {0,-1,0,1,0};

queue<pair<int, int> > q;
void bfs(int s, int t) {
	q.push({s,t});
	vis[s][t] = true;
	while(q.size()) {
		pair<int, int> now = q.front();
		q.pop();
		if(now.first == 4 && now.second == 4)
			break;
		for(int i = 1; i <= 4; ++i) {
			int nx = now.first + dx[i];
			int ny = now.second + dy[i];
			if(!vis[nx][ny] && nx>=0 && nx<=4 && ny>=0 && ny<=4 && !maps[nx][ny]) {
				q.push({nx,ny});
				// cout << " success";
				pre[nx][ny] = {now.first, now.second};
				vis[nx][ny] = true;
			} else {
				// cout << "~~~" << vis[nx][ny] << " " << maps[nx][ny];
			}
		}
	}
}

int main() {
	memset(pre,0,sizeof(pre));
	memset(vis,0,sizeof(vis));
	while(q.size()) q.pop();
	mapsInput();

	bfs(0,0);
	pair<int, int> t(4,4);

	stack<pair<int, int> > s;
	while(t.first != 0 ||  t.second != 0) {
		s.push(t);
		t = pre[t.first][t.second];
	}
	s.push({0,0});
	while(s.size()) {
		cout << "(" << s.top().first << ", " << s.top().second << ")\n";
		s.pop();
	}

	return 0;
}

B - 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
Hint

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

题解
  • 进行bfs暴力枚举每种状态:从每个状态有6中方向可以转移,分别是倒满A、倒满B、倒空A、倒空B、把A往B倒、把B往A倒。
  • 使用set作为传统的vis数组,来判断某种状态是否已经到达过,注意每组数据开始时要清空。
  • 使用prestatus和preid两个map作为路径记录和操作记录,即记录到达某个状态的操作类型(1/6)和前一个状态,bfs返回最终状态,从最终状态倒推回去即可输出路径。
  • 将6种操作用函数数组来整合能够复用check操作的代码,稍微美观清晰一些。
代码
丑陋版
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <stack>
#include <map>
#include <set>
using namespace std;

map<int,string> method;

int A, B, C;
struct status {
	int a;
	int b;
	bool operator < (const status &x) const {
		if(a != x.a) return a < x.a;
		return b < x.b;
	}
};

map<status,status> prestatus;
map<status,int> preid;
set<status> vis;
queue<status> q;
void check(status now, status t, int id) {
	if(vis.find(t) == vis.end()) { //没有走过这个状态
		// cout << "*";
		q.push(t);
		vis.insert(t);
		preid[t] = id;
		prestatus[t] = now;
	}
	// cout << endl;
}

status bfs(int a, int b) {
	q.push((status){a,b});
	vis.insert((status){a,b});
	while(q.size()) {
		status now = q.front();
		q.pop();
		if(now.a == C || now.b == C) {
			// cout << now.a << " " << now.b;
			return now;
		}
		// 6种分支生成状态
		status t;
		// 1 fill A
		t.a=now.a;t.b=now.b;
		t.a = A;
		check(now,t,1);
		// 2 fill B
		t.a=now.a;t.b=now.b;
		t.b = B;
		check(now,t,2);
		// 3 empty A
		t.a=now.a;t.b=now.b;
		t.a = 0;
		check(now,t,3);
		// 4 empty B
		t.a=now.a;t.b=now.b;
		t.b = 0;
		check(now,t,4);
		// 5 pour A B
		t.a=now.a;t.b=now.b;
		int minn = min(t.a, B-t.b);
		t.a -= minn;
		t.b += minn;
		check(now,t,5);
		// 6 pour B A
		t.a=now.a;t.b=now.b;
		minn = min(t.b, A-t.a);
		t.b -= minn;
		t.a += minn;
		check(now,t,6);
	}
	return (status){0,0};
}

void init() {
	method[1] = "fill A";
	method[2] = "fill B";
	method[3] = "empty A";
	method[4] = "empty B";
	method[5] = "pour A B";
	method[6] = "pour B A";
}

stack<string> haha;

int main() {
	init();
	while(cin >> A >> B >> C) {
		prestatus.clear();
		preid.clear();
		vis.clear();;
		while(q.size() > 0) {q.pop();}
		status ans = bfs(0,0);	
		while(ans.a != 0 || ans.b != 0) {
			// cout << method[preid[ans]] << endl;
			haha.push(method[preid[ans]]);
			ans = prestatus[ans];
		}
		while(haha.size()) {
			cout << haha.top() << endl;
			haha.pop();
		}
		puts("success");
	}
		
	return 0;
}
重构版加入函数指针数组
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <stack>
#include <map>
#include <set>
using namespace std;

map<int,string> method;

int A, B, C;
struct status {
	int a;
	int b;
	bool operator < (const status &x) const {
		if(a != x.a) return a < x.a;
		return b < x.b;
	}
	status& operator = (const status &x) {
		a = x.a;
		b = x.b;
		return *this;
	}
};

map<status,status> prestatus;
map<status,int> preid;
set<status> vis;
queue<status> q;
void check(status now, status t, int id) {
	if(vis.find(t) == vis.end()) { //没有走过这个状态
		// cout << "*";
		q.push(t);
		vis.insert(t);
		preid[t] = id;
		prestatus[t] = now;
	}
	// cout << endl;
}

// 1 fill A
void one(status now, status &t, int id) {
	t.a = A;
}
// 2 fill B
void two(status now, status &t, int id) {
	t.b = B;
}
// 3 empty A
void three(status now, status &t, int id) {
	t.a = 0;
}
// 4 empty B
void four(status now, status &t, int id) {
	t.b = 0;
	check(now,t,4);
}
// 5 pour A B
void five(status now, status &t, int id) {
	t.a=now.a;t.b=now.b;
	int minn = min(t.a, B-t.b);
	t.a -= minn;
	t.b += minn;
	check(now,t,5);
}
// 6 pour B A
void six(status now, status &t, int id) {
	t.a=now.a;t.b=now.b;
	int minn = min(t.b, A-t.a);
	t.b -= minn;
	t.a += minn;
}

void (*p[6])(status now, status &t, int id);
status bfs(int a, int b) {
	q.push((status){a,b});
	vis.insert((status){a,b});
	while(q.size()) {
		status now = q.front();
		q.pop();
		if(now.a == C || now.b == C) {
			return now;
		}
		// 6种分支生成状态
		for(int i = 1; i <= 6; ++ i) {
			status t = now;
			p[i](now,t,i);
			check(now,t,i);
		}
	}
	return (status){0,0};
}

void init() {
	method[1] = "fill A";
	method[2] = "fill B";
	method[3] = "empty A";
	method[4] = "empty B";
	method[5] = "pour A B";
	method[6] = "pour B A";
	p[1] = one; p[2] = two; p[3] = three; 
	p[4] = four; p[5] = five; p[6] = six;
}

stack<string> haha;
int main() {
	init();
	while(cin >> A >> B >> C) {
		prestatus.clear();
		preid.clear();
		vis.clear();;
		while(q.size() > 0) {q.pop();}
		status ans = bfs(0,0);	
		while(ans.a != 0 || ans.b != 0) {
			// cout << method[preid[ans]] << endl;
			haha.push(method[preid[ans]]);
			ans = prestatus[ans];
		}
		while(haha.size()) {
			cout << haha.top() << endl;
			haha.pop();
		}
		puts("success");
	}
		
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值