学习总结——第 4 章 搜索技术

本文深入讲解了搜索技术的多个方面,包括使用STL输出全排列、递归打印全排列、生成集合的子集、广度优先搜索(BFS)及其在实际问题中的应用等。通过具体的代码示例详细介绍了各种搜索技术的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

4.1 递归和排列

4.1.1 用 STL 输出全排列
// STL 升序输出全排列
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int data[] = {5, 2, 1, 4};
	sort(data, data + 4);
	do{
		for(int i = 0;i < 4;i ++){
			cout << data[i] << " ";
		}
		cout << endl;
	}while(next_permutation(data, data + 4));
	
	return 0;
}
4.1.2 用递归打印全排列
// 用递归打印全排列
#include <bits/stdc++.h>
using namespace std;
#define swap1(a, b){int temp = a; a = b; b = temp;}

int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
int num = 0;

// 函数参数位数组中的起始下标
int perm(int begin, int end){
	
	if(begin == 3){// 例如:从 10 个数中任取 3 个数的全排列
		for(int i = 0;i <= 3;i ++){
			cout << data[i] << " ";
		}
		cout << endl;
		num ++;
	}
	else{
		for(int i = begin;i <= end;i ++){
			swap1(data[begin], data[i]);
			perm(begin + 1, end);
			swap1(data[begin], data[i]);
		}
	}
}

int main(){
	perm(0, 9);
	cout << num;
}

4.2 子集生成和组合问题

4.2.1 打印一个集合的非空子集
// 打印一个集合的非空子集
#include <bits/stdc++.h>
using namespace std;

char str[] = {'a', 'b', 'c', 'd'};

void subset(int n){
	for(int i = 0;i < (1 << n);i ++){
		for(int j = 0;j < n;j ++){
			if(i & (1 << j)){
				cout << str[j];
			}
		}
		cout << endl;
	}
}

int main(){
	int n;
	cin >> n;
	subset(n);
	
	return 0;
}
4.2.2 元素总数量为 n,个数为 k 的子集
// 元素总数量为 n,个数为 k 的子集
#include <bits/stdc++.h>
using namespace std;

void subset(int n, int k){
	for(int i = 0;i < (1 << n);i ++){
		int num = 0;
		int kk = i;
		while(kk){
			kk = kk & (kk - 1);
			num ++;
		}
		if(num == k){
			for(int j = 0;j < n;j ++)
				if(i & (1 << j))
					cout << j << " ";
			cout << endl;
		}
	}
}

int main(){
	
	int n, k;
	cin >> n >> k;
	
	subset(n, k);
	return 0;
}

4.3 BFS

4.3.1 BFS 和队列

hdu 1312 Red and Black

// 深度搜索
#include <bits/stdc++.h>
using namespace std;

char room[23][23];
int dir[4][2] = {
			{1, 0},
			{0, 1},
			{-1, 0},
			{0, -1}
};

int wx,hy, num;

#define CHECK(x, y)(x < wx && x >= 0 && y < hy && y >= 0)
struct node{
	int x;
	int y;
};

void BFS(int dx,int dy){
	num = 1;
	queue<node> q;
	node start, next;
	start.x = dx;
	start.y = dy;
	
	q.push(start);
	
	while(!q.empty()){
		start = q.front();
		q.pop();
		
		for(int i = 0;i < 4;i ++){
			next.x = dir[i][0];
			next.y = dir[i][1];
			if(CHECK(next.x, next.y) && room[next.x][next.y] == '.'){
				room[next.x][next.y] = '#';
				num ++;
				q.push(next);
			}
		}
	}
}


int main()
{
	int x, y, dx, dy;
	while(cin >> wx >> hy){
		if(wx || hy) break;
		
		for(y = 0;y < hy;y ++)
			for(x = 0;x < wx;x ++){
				cin >> room[x][y];
				if(room[x][y] == '@'){
					dx = x;
					dy = y;
				}
			}
		
		num = 0;
		BFS(wx, hy);
		cout << num << endl;
	}
	
	return 0;
}
4.3.2八数码问题和状态图搜索

poj 1077 八数码管问题

// 八数码管
#include <bits/stdc++.h>
using namespace std;
const int LEN = 362880;
struct node{
	int state[9];
	int dis;
};

int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int visited[LEN];
int start[9];
int goal[9];
long int factory[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};

bool Cantor(int str[], int n){
	long result = 0;
	for(int i = 0;i < n;i ++){
		int counted = 0;
		for(int j = i + 1;j < n;j ++){
			if(str[i] > str[j]) counted ++;
		}
		result += counted * factory[n - i - 1];
	}
	if(!visited[result]){
		visited[result] = 1;
		return 1;
	}else return 0;
}

int BFS(){
	node head;
	memcpy(head.state, start, sizeof(head.state));
	head.dis = 0;
	queue<node> q;
	Cantor(head.state, 9);
	q.push(head);
	
	while(!q.empty()){
		head = q.front();
		if(memcmp(head.state, goal, sizeof(start)) == 0) return head.dis;
		q.pop();
		
		int z;
		for(z = 0;z < 9;z ++){
			if(!head.state[z]) break;
		}
		
		int x = z % 3;
		int y = z / 3;
		for(int i = 0;i < 4;i ++){
			int newx = x + dir[i][0];
			int newy = y + dir[i][1];
			int nz = x + 3 * newy;
			if(newx >= 0 && newx < 3 && newy >= 0 && newy < 3){
				node newnode;
				memcmp(&newnode, &head, sizeof(struct node));
				swap(newnode.state[nz], newnode.state[z]);
				newnode.dis ++;
				
				if(Cantor(newnode.state, 9)) q.push(newnode);
			} 
		}
	}
	return -1;
}

int main(){
	for(int i = 0;i < 9;i ++) cin >> start[i];
	for(int i = 0;i < 9;i ++) cin >> goal[i];
	int num = BFS();
	
	if(num != -1) cout << num << endl;
	else cout << "Impossible" << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值