Cubic Eight-Puzzle UVA - 1604

参考了下这位博主的框架 https://blog.csdn.net/zju2016/article/details/78594948

感谢博主

9个格子,自然二维数组,每个立方体由于他的特殊性,所有只需要俩个参数即可,选择用pair

状态转移也比较简单

另外是优化,这位博主的判重比较简单,只判断这一步走的结果会不会走到上一步未走的状态,也就是会不会走回去

这样应该还是会产生重复的,但是省了很多写的时间,我也是这么做的,我也想省点事

另外,就是暴力搜索常见的剪枝了,已经走了dep,再怎么走也不可能在限制的maxd中走完,就回朔

这里也一样

这道题应该还有优化的可能,就是加入判重,再使用bfs来搜索,应该比较快

我没试过,猜的

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<assert.h>
#include<vector>
#include<list>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<bitset>
#include<algorithm>
#pragma warning(disable:4996)
#define _for(i,a,b) for(int i=(a);i<(b);++i)
#define _rep(i,a,b) for(int i=(a);i<=(b)++i)
#define Max(a,b) (a)>(b)?(a):(b);
#define Min(a,b) (a)<(b)?(a):(b);
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = { 0, -1, 0, 1, -1, -1, 1, 1 };
const int dc[] = { -1, 0, 1, 0, -1, 1, -1, 1 };
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-15;
const int N = 100000 + 10;
// 0表示空白,1表示蓝色,2表示白色,3表示红色
int aim[3][3];              //目标
pair<int, int> cube[3][3];  //只要知道了方块的top,front,这个方块的信息就全了,下标代表位置
int ans;
int cmp()
{
	int cnt = 0;
	_for(i, 0, 3)
		_for(j, 0, 3)
		if (cube[i][j].first != aim[i][j]) cnt++;
	return cnt;
}
void Move(int dx, int dy, int i)
{
	int x = dx + dr[i], y = dy + dc[i];
	if (i == 0) cube[x][y].first = 6 - cube[x][y].first - cube[x][y].second;
	if (i == 1) swap(cube[x][y].first, cube[x][y].second);
	if (i == 2) cube[x][y].first = 6 - cube[x][y].first - cube[x][y].second;
	if (i == 3) swap(cube[x][y].first, cube[x][y].second);
	cube[x][y].swap(cube[dx][dy]);
}
void dfs(int curx, int cury, int dep)
{
	int cnt = cmp();
	if (cnt == 0) {
		ans = min(ans, dep);
		return;
	}
	if (cnt + dep > ans) return;
	int tx, ty;
	_for(i, 0, 3)
		_for(j, 0, 3)
		if (cube[i][j].first == 0) {
			tx = i; ty = j; break;
		}
	_for(i, 0, 4) {
		int dx = tx + dr[i], dy = ty + dc[i];
		if (dx < 0 || dx>2 || dy < 0 || dy>2) continue;   //不能越界
		if (curx != -1 && curx == dx && cury == dy) continue; //
		Move(tx, ty, i);
		dfs(tx, ty, dep + 1);
		Move(dx, dy, (i + 2) % 4);
	}
}
int main()
{
	int x, y; char ch;
	while (scanf("%d%d", &x, &y) == 2)
	{
		if (x == 0 && y == 0) break;
		_for(i,0,3)
			_for(j, 0, 3)
		{
			cin >> ch;
			if (ch == 'E') aim[i][j] = 0;
			else if (ch == 'W') aim[i][j] = 2;
			else if (ch == 'R') aim[i][j] = 3;
			else aim[i][j] = 1;
			if (i == y - 1 && j == x - 1) cube[i][j] = make_pair(0, 0);
			else cube[i][j] = make_pair(2, 3);
		}
		ans = 31;
		dfs(-1, -1, 0);
		if (ans > 30) ans = -1;
		cout << ans << endl;
	}
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值