第九章 广度优先搜索

1:迷宫问题

定义一个二维数组: 

int maze[5][5] = {

0, 1, 0, 0, 0,

0, 1, 0, 1, 0,

0, 0, 0, 0, 0,

0, 1, 1, 1, 0,

0, 0, 0, 1, 0,

};


它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。


输入
一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。
输出
左上角到右下角的最短路径,格式如样例所示。
样例输入
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
样例输出
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
#include "iostream"
#include "cstdio"
#include "cstring"
#include "queue"
using namespace std;
struct Node {
	int x, y;
	Node(int xx, int yy) : x(xx), y(yy) {};
};
int dir[4][2] = { {0,1} ,{0,-1}, {1,0}, {-1,0} };

bool color[5][5];
int pre[26];
void Print(int n) {
	if (n == -1)
		return;
	Print(pre[n]);
	printf("(%d, %d)\n", n/5,n%5);
}
void bfs(bool map[][5]) {
	fill(pre, pre + 25,-1);
	memset(color, 0, sizeof(color));
	color[0][0] = 1;
	queue<Node>q;
	q.push(Node(0, 0));
	while (!q.empty()) {
		Node node = q.front();
		if (node.x == 4 && node.y == 4) {
			Print(24);
			break;
		}
		for (int i = 0; i < 4; i++) {
			int x = node.x + dir[i][0];
			int y = node.y + dir[i][1];
			if (x >= 0 && x < 5 && y >= 0 && y < 5) {
				if (!color[x][y] && map[x][y]==0) {
					color[x][y] = 1;
					pre[x * 5 + y] = node.x * 5 + node.y;
					q.push(Node(x, y));
				}
			}
		}
		q.pop();
	}
}
int main() {
	bool map[5][5];
	for (int i = 0; i < 5; i++)
		for (int j = 0; j < 5; j++)
			cin >> map[i][j];
	bfs(map);
	return 0;
}

2:Pots

总时间限制: 
1000ms 
内存限制: 
65536kB
描述

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i)        fill the pot i (1 ≤ ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. POUR(i,j)    pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).

Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.

输入

On the first and only line are the numbers AB, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

输出

The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

#include "iostream"
#include "cstdio"
#include "cstring"
#include "queue"
using namespace std;
int a, b, c;
struct Node {
	int x, y;
	int step; /* 记录步数 */
	Node(int xx, int yy,int steps) : x(xx), y(yy),step(steps) {};
};
struct PreNode {
	int val = 0;
	int oper = 0;
};
bool color[101][101];
PreNode pre[1000001];
void Print(int n) {
	if (n == 0)
		return;
	Print(pre[n].val);
	switch (pre[n].oper) {
	case 1:cout << "DROP(1)" << endl; break;
	case 2:cout << "DROP(2)" << endl; break;
	case 3:cout << "FILL(1)" << endl; break;
	case 4:cout << "FILL(2)" << endl; break;
	case 5:cout << "POUR(1,2)" << endl; break;
	case 6:cout << "POUR(2,1)" << endl; break;
	default:cout << "?" << endl; break;
	}
}
void bfs() {
	bool flag = 0;
	memset(color, 0, sizeof(color));
	color[0][0] = 1;
	queue<Node>q;
	q.push(Node(0, 0, 0));
	while (!q.empty()) {
		Node node = q.front();
		if (node.x == c || node.y == c) {
			flag = 1;
			cout << node.step << endl;
			Print(node.x * 100 + node.y);
			break;
		}
		if (!color[0][node.y]) { /* drop(1)*/
			color[0][node.y] = 1;
			q.push(Node(0, node.y, node.step+1));
			pre[node.y].val = node.x*100+node.y;
			pre[node.y].oper = 1;
		}
		if (!color[node.x][0]) { /* drop(2) */
			color[node.x][0] = 1;
			q.push(Node(node.x, 0, node.step+1));
			pre[node.x*100].val = node.x * 100 + node.y;
			pre[node.x * 100].oper = 2;
		}
		if (!color[a][node.y]) { /* fill(1) */
			color[a][node.y] = 1;
			q.push(Node(a, node.y, node.step+1));
			pre[a * 100 + node.y].val = node.x * 100 + node.y;
			pre[a * 100 + node.y].oper = 3;
		}
		if (!color[node.x][b]) { /* fill(2) */
			color[node.x][b] = 1;
			q.push(Node(node.x, b, node.step + 1));
			pre[node.x * 100 + b].val = node.x * 100 + node.y;
			pre[node.x * 100 + b].oper = 4;
		}
		if (node.x + node.y <= b) { /* a能全倒进b里面 */
			if (!color[0][node.x+node.y]) { /* pour(1,2)*/
				color[0][node.x + node.y] = 1;
				q.push(Node(0,node.x+node.y,node.step+1));
				pre[node.x + node.y].val = node.x * 100 + node.y;
				pre[node.x + node.y].oper = 5;
			}
		}
		else {
			if (!color[node.x+node.y-b][b]) { /* pour(1,2)*/
				color[node.x + node.y - b][b] = 1;
				q.push(Node(node.x + node.y - b, b, node.step + 1));
				pre[(node.x +node.y-b)*100+b].val = node.x * 100 + node.y;
				pre[(node.x + node.y - b) * 100 + b].oper = 5;
			}
		}
		if (node.x + node.y <= a) { /* b能全倒进a里面 */
			if (!color[node.x + node.y][0]) { /* pour(2,1)*/
				color[node.x + node.y][0] = 1;
				q.push(Node(node.x + node.y, 0, node.step + 1));
				pre[(node.x +node.y)*100].val = node.x * 100 + node.y;
				pre[(node.x + node.y) * 100].oper = 6;
			}
		}
		else {
			if (!color[a][node.x + node.y - a]) { /* pour(2,1)*/
				color[a][node.x + node.y - a] = 1;
				q.push(Node(a, node.x + node.y - a, node.step + 1));
				pre[a*100 + node.x+node.y - a].val = node.x * 100 + node.y;
				pre[a * 100 + node.x + node.y - a].oper = 6;
			}
		}
		q.pop();
	}
	if (!flag)
		cout << "impossible" << endl;
}

int main() {
	cin >> a >> b >> c;
	bfs();
	return 0;
}

3:鸣人和佐助

总时间限制: 
1000ms 
内存限制: 
65536kB
描述

佐助被大蛇丸诱骗走了,鸣人在多少时间内能追上他呢?


已知一张地图(以二维矩阵的形式表示)以及佐助和鸣人的位置。地图上的每个位置都可以走到,只不过有些位置上有大蛇丸的手下,需要先打败大蛇丸的手下才能到这些位置。鸣人有一定数量的查克拉,每一个单位的查克拉可以打败一个大蛇丸的手下。假设鸣人可以往上下左右四个方向移动,每移动一个距离需要花费1个单位时间,打败大蛇丸的手下不需要时间。如果鸣人查克拉消耗完了,则只可以走到没有大蛇丸手下的位置,不可以再移动到有大蛇丸手下的位置。佐助在此期间不移动,大蛇丸的手下也不移动。请问,鸣人要追上佐助最少需要花费多少时间?

输入
输入的第一行包含三个整数:M,N,T。代表M行N列的地图和鸣人初始的查克拉数量T。0 < M,N < 200,0 ≤ T < 10
后面是M行N列的地图,其中@代表鸣人,+代表佐助。*代表通路,#代表大蛇丸的手下。
输出
输出包含一个整数R,代表鸣人追上佐助最少需要花费的时间。如果鸣人无法追上佐助,则输出-1。
#include "iostream"
#include "cstdio"
#include "cstring"
#include "queue"
using namespace std;
int a, b, c;
struct Node {
	int m, n,t;
	int step;
	Node(int xx, int yy,int tt,int steps) : m(xx), n(yy),t(tt),step(steps) {};
};
char map[201][201];
bool color[201][201][11];
int dir[4][2] = { {0,-1},{0,1},{-1,0},{1,0} };
int startX, startY;
int endX, endY;
bool flag = 0;
void bfs() {
	memset(color, 0, sizeof(color));
	queue<Node>q;
	q.push(Node(startX,startY,c,0));
	while (!q.empty()) {
		Node node = q.front();
		if (node.m == endX&&node.n == endY) {
			cout << node.step << endl;
			flag = 1;
			break;
		}
		for (int i = 0; i < 4; i++) {
			int x = node.m + dir[i][0];
			int y = node.n + dir[i][1];
			int t = node.t;
			if (x>=0&&x<a&&y>=0&&y<b) {
				if (!color[x][y][t]) {
					if (map[x][y] == '*'||map[x][y]=='+'&&t >= 0) {
						q.push(Node(x, y, t, node.step + 1));
						color[x][y][t] = 1;
					}
					if (map[x][y] == '#'&&t >= 1) {
						q.push(Node(x, y, t - 1, node.step + 1));
						color[x][y][t - 1] = 1;
					}
				}
			}
		}
		q.pop();
	}
	if (!flag)
		cout << "-1" << endl;
}
int main() {
	cin >> a >> b >> c;
	for (int i = 0; i < a; i++)
		for (int j = 0; j < b; j++) {
			cin >> map[i][j];
			if (map[i][j] == '@') {
				startX = i;
				startY = j;
			}
			if (map[i][j] == '+') {
				endX = i;
				endY = j;
			}
		}
	bfs();
	return 0;
}



【从高压输电线的架空地线中汲取电能】一个25千瓦受控电源从735千伏线路的架空地线中汲取电能的SimPowerSystems模型(Simulink仿真实现)内容概要:本文介绍了一个基于SimPowerSystems的Simulink仿真模型,用于模拟从735千伏高压输电线的架空地线中汲取25千瓦电能的受控电源系统。该模型聚焦于高压输电线路中架空地线的能量回收技术,通过仿真手段实现对电能采集过程的建模与控制策略验证,体现了电力系统中新型能源获取方式的技术可行性与工程应用潜力。文中还提及该资源属于一系列电力系统仿真研究的一部分,涵盖微电网、储能优化、碳流追踪、鲁棒调度等多个前沿方向,配套提供Matlab/Simulink代码及网盘资料链接,便于科研人员复现与拓展研究。; 适合人群:具备电力系统基础知识、熟悉Matlab/Simulink仿真环境,从事电力工程、能源回收或智能电网相关研究的科研人员及研究生;有一定编程与建模仿真经验的高年级本科生或工程技术人员。; 使用场景及目标:①研究高压输电线路中架空地线的能量回收机制与建模方法;②掌握基于Simulink的电力系统仿真技术,特别是受控电源与电网交互的动态特性分析;③为开展能源 harvesting、分布式供能、电力电子变换器控制等相关课题提供参考模型与技术支撑; 阅读建议:建议结合提供的仿真模型文件进行实操演练,重点理解系统结构设计、参数设置与控制逻辑实现;同时可延伸学习文档中提到的其他电力系统优化与仿真案例,以拓宽研究视野和技术积累。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值