例题7-9 万圣节后的早晨(The Morning after Halloween, Japan 2007, UVa1601)

该博客介绍了如何使用双向BFS算法解决UVA1601(The Morning after Halloween, Japan 2007)问题。通过状态压缩和优化,将256^3的状态转换为144个节点的图,减少了空间需求。算法通过枚举和冲突检查找到从起点到终点的最短路径。" 8864604,1404681,SQLite C API:返回值与错误码详解,"['SQLite', '数据库', 'C语言', '错误处理', '编程接口']
摘要由CSDN通过智能技术生成
思路:同为bfs求解最少步数,问题在于状态如何更好地枚举。
无法开256^3的数组,利用题中2*2格中必有*的条件,改矩阵图为存边的图,点则变为144个,可存数组。
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cctype>
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define LL long long
#define mod 1000000007
#define INF 1000000007
#define eps 1e-5
#define PI 3.1415926535898
using namespace std;
//-------------------------CHC------------------------------//
const int maxw = 16 + 5;
const int maxn = 144 + 5;
const int dx[] = { 1, -1, 0, 0, 0 };
const int dy[] = { 0, 0, 1, -1, 0 };
char G[maxw][maxw];
int newG[maxn][5], degree[maxn];
int w, h, n;
int s[3], t[3];
struct Node {
	int a, b, c;
	Node(int a, int b, int c): a(a), b(b), c(c) { }
};
int d[maxn][maxn][maxn];

bool conflict(int a, int b, int na, int nb) { return na == nb || (a == nb) && (b == na); }

int bfs() {
	CLEAR(d, -1);
	queue<Node> q;
	q.push(Node(s[0], s[1], s[2]));
	d[s[0]][s[1]][s[2]] = 0;
	
	while (q.size()) {
		Node u = q.front(); q.pop();
		if (u.a == t[0] && u.b == t
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值