题目描述
在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字。棋盘中留有一个空格,空格用0来表示。空格周围的棋子可以移到空格中。要求解的问题是:给出一种初始布局(初始状态)和目标布局(为了使题目简单,设目标状态为123804765),找到一种最少步骤的移动方法,实现从初始布局到目标布局的转变。
输入格式
输入初始状态,一行九个数字,空格用0表示
输出格式
只有一行,该行只有一个数字,表示从初始状态到目标状态需要的最少移动次数(测试数据中无特殊无法到达目标状态数据)
样例:
输入:
283104765
输出:
4
思路分析
之前在poj上写过一次八数码。上次用的是单bfs,这次就当练习一下双bfs。
双bfs的特点就是:知道起点和终点,然后,从起点和终点同时开始搜索,从而减少总的搜索数量,达到提高算法效率的目的。
说起来很简单,但是做起来还是有点难度的。难点在于:搜索到了相遇点之后,怎么找到另一边搜索到该点时的信息。这里,我用的方法和记录路径的时候一样:将搜索过的所有点放入一个vector中,搜索到相遇点之后再在另一边的vector中寻找相遇点,再采集信息输出。总之,这个过程还是比较难写的,以后需要多加练习。后面也会根据八数码八境界继续修炼~
完整代码
#include <iostream>
#include <queue>
#include <map>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
map<int, int> vis;
bool judge(int x, int y)
{
if (x < 3 && x >= 0 && y < 3 && y >= 0) return 1;
else return 0;
}
ll factor[] = { 0,1,2,6,24,120,720,5040,40320 };
ll cantor(int* a)
{
ll ans = 0;
for (int i = 0; i < 9; i++)
{
int cnt = 0;
for (int k = i + 1; k < 9; k++)
{
if (a[i] > a[k]) cnt++;
}
ans += factor[8 - i] * cnt;
}
return ans;
}
int dir[4][2] = { 1,0,-1,0,0,1,0,-1 };
char op[] = { 'r','l','d','u' };
typedef struct {
int a[9];
int steps;
int cantor;
int p;
}condition;//需要保存的状态建立为一个结构体
int ending[] = { 1,2,3,8,0,4,7,6,5 };//结束时的排列,其实有点多余的一步骤
int main()
{
condition s1, s2;
condition da;
condition a;
condition start;
for (int i = 0; i < 9; i++)
{
char t;
cin >> t;
start.a[i] = t - '0';
if (start.a[i] == 0) start.p = i;
}
start.cantor = cantor(start.a);
start.steps = 0;//初始节点创建完毕
condition end;
memcpy(end.a, ending, sizeof(ending));
end.steps = 0;
end.cantor = cantor(end.a);
end.p = 4;
end.steps = 0;//结束节点创建完毕
if (end.cantor == start.cantor)
{
cout << 0 << endl;
return 0;
}//排除一个特殊情况:起点和终点重合
queue<condition> q1;
queue<condition> q2;
q1.push(start);
q2.push(end);
vector<condition> a1;
vector<condition> a2;
a1.push_back(start);
a2.push_back(end);
int flag1, flag2 = 0;
vis[start.cantor] = 1;
vis[end.cantor] = 2;
while (!q1.empty() || !q2.empty())
{
if (flag2 != 0)//用一个flag2表示是否相遇,flag由后面标记
{
break;
}
else
{
if (q2.size() > q1.size())
{
a = q1.front();
flag1 = 1;
q1.pop();
}
else
{
a = q2.front();
flag1 = 2;
q2.pop();
}
for (int i = 0; i < 4; i++)
{
int x = a.p % 3;
int y = a.p / 3;
int dx = x + dir[i][0];
int dy = y + dir[i][1];
int dp = dx + 3 * dy;
if (judge(dx, dy))
{
memcpy(&da, &a, sizeof(a));
swap(da.a[a.p], da.a[dp]);
da.p = dp;
da.steps++;
da.cantor = cantor(da.a);
if (vis[da.cantor] == 0)//没有搜过的点,搜索下来。
{
vis[da.cantor] = flag1;
/*if (da.cantor == 46650)
cout << "yes" << endl;*/
if (flag1 == 1)
{
a1.push_back(da);
q1.push(da);
}
else
{
a2.push_back(da);
q2.push(da);
}
}
else if (vis[da.cantor] == flag1)//这个点被同一支搜索过,剪枝。
{
continue;
}
else//被另一支搜索过,表示相遇,停止搜索。
{
s1 = da;
flag2 = flag1;
break;
}
}
}
}
}
vector<condition>::iterator it, ed;
ll cantor;
if (flag2 == 1)//找到另一支vector的begin和end
{
cantor = s1.cantor;
it = a2.begin();
ed = a2.end();
}
else
{
cantor = s1.cantor;
it = a1.begin();
ed = a1.end();
}
for (; it != ed; it++)//找出另一支的信息,以便得出结果。
{
if (it->cantor == cantor) s2 = *it;
}
cout << s1.steps + s2.steps << endl;
return 0;
}
讲实话,双bfs是真的猛。单向的用时为 6.05s,内存9.34MB,而双向用时为224ms,内存1.53MB(太牛逼了)。