#include <iostream>
#include <fstream>
#include <queue>
#include <algorithm>
using namespace std;
const int MAX = 50;
int pos[MAX];
int src[MAX];
int des[MAX];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1,1, 0, 0};
int op[] = {1, 0, 3, 2};
int rowsz, boardsz;
class Node
{
public:
int x, y; //空格位置
int dist; //manhatt距离
int psz; //变换次数
int *board; //状态
int *path; //变换路径
Node()
{
dist = psz = 0;
board = new int[boardsz];
path = new int[MAX];
}
Node(Node &enode)
{
dist = enode.dist;
psz = enode.psz;
x = enode.x;
y = enode.y;
board = new int[boardsz];
path = new int[MAX];
copy(enode.board, enode.board+boardsz, board);
copy(enode.path, enode.path+MAX, path);
}
//计算manhattan距离
int getdist(int v, int loc)
{
int dis = abs(pos[v]/rowsz - loc/rowsz);
dis += abs(pos[v]%rowsz - loc%rowsz);
return dis;
}
void getboard(int m[])
{
for(int i=0; i<boardsz; i++)
{
board[i] = m[i];
if(m[i] == 0)
{
x = i / rowsz;
y = i % rowsz;
}
else
dist += getdist(m[i], i);
}
}
//按三个方向移动,不移动回上一步的位置
bool move(int dir)
{
int nx = x + dx[dir];
int ny = y + dy[dir];
if((psz==0 ||op[dir] != path[psz-1]) && nx>=0 && ny>=0 && nx<rowsz && ny<rowsz) //不移动回上一个位置,不出界
{
dist = dist + 1 - getdist(board[nx*rowsz+ny], nx*rowsz+ny) + getdist(board[nx*rowsz+ny], x*rowsz+y);
swap(board[nx*rowsz+ny], board[x*rowsz+y]);
path[psz++] = dir;
x = nx;
y = ny;
return true;
}
else
return false;
}
//达到目标状态
bool reached()
{
for(int i=0; i<boardsz; i++)
if(board[i] != des[i])
return false;
return true;
}
//输出
void out()
{
cout << "变换次数为:" << psz << endl;
cout << "移动序列为:\n";
char dir[] ={'L', 'R', 'U', 'D'};
for(int i=0; i<psz; i++)
cout << dir[ path[i] ] << " ";
cout << endl;
}
};
int maxdep;
//逐步深化的A*搜索算法
bool solve(int dep, Node enode)
{
if(dep + enode.dist <= maxdep)
{
if(enode.reached())
{
enode.out();
return true;
}
for(int i=0; i<4; i++)
{
Node now(enode);
if(now.move(i))
if(solve(dep+1, now))
return true;
}
}
return false;
}
void idstar()
{
Node enode;
enode.getboard(src);
maxdep = enode.dist;
if(maxdep == 0)
cout << 0 << endl;
while(!solve(0, enode))
maxdep += 2;
}
//判断可解性
bool odd()
{
int count = 0, count1 = 0, i1 = 0, i2 = 0, j1 = 0, j2 = 0;
int *c = new int[boardsz];
for(int i=0; i<16; i++)
{
c[des[i]] = src[i];
if(src[i] == 0) //空格
{
i1 = i / rowsz + 1;
j1 = i % rowsz + 1;
}
if(des[i] == 0) //空格
{
i2 = i / rowsz + 1;
j2 = i % rowsz + 1;
}
}
int posi = ((i1 + i2) % 2 + (j1 + j2) % 2) % 2; //两个空格的manhatt距离的奇偶性
for(i=0; i<boardsz; i++) //count 为初始排列到目标排列的变换次数
{
int k = c[i];
int k1 = i;
count1 = 0;
while(k >=0)
{
k = c[k1];
k1 = k;
c[k1] = -1;
count1++;
}
if(count1 != 0)
count += count1 - 2;
}
if(count%2 == posi) //如果两者奇偶性相同
return true;
else
return false;
}
int main()
{
ifstream fin("宫.txt");
cout << "输入方格阵列行数:";
fin >> rowsz; cout << rowsz << endl;
boardsz = rowsz * rowsz;
cout << "输入初始排列:\n";
int i;
for(i=0; i<boardsz; i++)
{
fin >> src[i];
cout << src[i] << " ";
if(i % 3 == 2)
cout << endl;
}
cout << "输入最终排列:\n";
for(i=0; i<boardsz; i++)
{
fin >> des[i];
cout << des[i] << " ";
if(i % 3 == 2)
cout << endl;
pos[ des[i] ] = i;
}
if(odd())
idstar();
else
cout << "No Solution!";
cout << endl << endl;
fin.close();
return 0;
}
重排n^2宫问题
最新推荐文章于 2021-06-21 10:02:11 发布