搜索算法列表

深度优先搜索(dfs)

广度优先搜索(bfs)
队列

记忆化搜索

搜索的剪枝

双向搜索

A*

IDA*(迭代加深搜索)

DLX

题目

八数码问题(P1379)

bfs

#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>


#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
const int target = 123804765, DIR[4][2] = {
    
     {
    
    1,0},{
    
    -1,0},{
    
    0,1},{
    
    0,-1}};

pair<int, int> getZero(int n,vector<vector<int>> &arr) {
    
    
pair<int, int> ans(0, 0);
for (int i = 2; i >= 0; i--) {
    
    
for (int j = 2; j >= 0; j--) {
    
    
	arr[i][j] = n % 10; n /= 10;
	if (!arr[i][j]) {
    
    
		ans.first = i;
		ans.second = j;
	}
}
}
return ans;
}
int getNum(vector<vector<int>>& arr) {
    
    
int ans = 0;
for (int i = 0; i <= 2; i++) {
    
    
for (int j = 0; j <= 2; j++) {
    
    
	ans = ans * 10 + arr[i][j];
}
}
return ans;
}

int search(int start) {
    
    
int ans = 0;
vector<vector<int>> arr(3,vector<int>(3));
unordered_set<int> mmp;
mmp.insert(start);

queue<int> q;
q.push(start);

while (!q.empty()) {
    
    
int len = q.size();
for (int i = 0; i < len; i++) {
    
    
	int top = q.front(); q.pop();
	if (top == target) return ans;
	pair<int, int> path = getZero(top, arr);

	for (int j = 0; j < 4; j++) {
    
    
		int nx = path.first + DIR[j][0];
		int ny = path.second + DIR[j][1];
		if (nx >= 0 && nx < 3 && ny >= 0 && ny < 3) {
    
    
			swap(arr[path.first][path.second], arr[nx][ny]);
			int num = getNum(arr);
			if (!mmp.count(num)) {
    
    
				mmp.insert(num);
				q.push(num);
			}
			swap(arr[path.first][path.second], arr[nx][ny]);
		}
	}
}
++ans;
}
return -1;
}

int main()
{
    
    
int start;
cin >> start;
cout << search(start) << endl;
return 0;
}

双向bfs

#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>


#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
const int target = 123804765, DIR[4][2] = {
    
     {
    
    1,0},{
    
    -1,0},{
    
    0,1},{
    
    0,-1}};

pair<int, int> getZero(int n,vector<vector<int>> &arr) {
    
    
	pair<int, int> ans(0, 0);
	for (int i = 2; i >= 0; i--) {
    
    
		for (int j = 2; j >= 0; j--) {
    
    
			arr[i][j] = n % 10; n /= 10;
			if (!arr[i][j]) {
    
    
				ans.first = i;
				ans.second = j;
			}
		}
	}
	return ans;
}
int getNum(vector<vector<int>>& arr) {
    
    
	int ans = 0;
	for (int i = 0; i <= 2; i++) {
    
    
		for (int j = 0; j <= 2; j++) {
    
    
			ans = ans * 10 + arr[i][j];
		}
	}
	return ans;
}

int search(int start) {
    
    
	int ans = 0;
	vector<vector<int>> arr(3,vector<int>(3));
	unordered_set<int> first,last;
	first.insert(start);
	last.insert(target);

	queue<int> q,q2;
	q.push(start);
	q2.push(target);

	while (!q.empty()) {
    
    
		int len = q.size();
		for (int i = 0; i < len; i++) {
    
    
			int top = q.front(); q.pop();
			if (last.count(top)) return ans;
			pair<int, int> path = getZero(top, arr);

			for (int j = 0; j < 4; j++) {
    
    
				int nx = path.first + DIR[j][0];
				int ny = path.second + DIR[j][1];
				if (nx >= 0 && nx < 3 && ny >= 0 && ny < 3) {
    
    
					swap(arr[path.first][path.second], arr[nx][ny]);
					int num = getNum(arr);
					if (!first.count(num)) {
    
    
						first.insert(num);
						q.push(num);
					}
					swap(arr[path.first][path.second], arr[nx][ny]);
				}
			}
		}
		ans++;
		len = q2.size();
		for (int i = 0; i < len; i++) {
    
    
			int top = q2.front(); q2.pop();
			if (first.count(top)) return ans;
			pair<int, int> path = getZero(top, arr);

			for (int j = 0; j < 4; j++) {
    
    
				int nx = path.first + DIR[j][0];
				int ny = path.second + DIR[j][1];
				if (nx >= 0 && nx < 3 && ny >= 0 && ny < 3) {
    
    
					swap(arr[path.first][path.second], arr[nx][ny]);
					int num = getNum(arr);
					if (!last.count(num)) {
    
    
						last.insert(num);
						q2.push(num);
					}
					swap(arr[path.first][path.second], arr[nx][ny]);
				}
			}
		}
		ans++;
	}
	return -1;
}

int main()
{
    
    
	int start;
	cin >> start;
	cout << search(start) << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/seanbill/article/details/127169555