UVa 10181 - 15-Puzzle Problem

題目:15數碼求解。

分析:搜索,A*。利用A*算法求解15數碼。利用逆序數判斷是否可解。

            可解:左右移動時、逆序數不變,上下移動時、逆序數變化奇偶性、加上行變換次數,奇偶性不變;

                       (這裡計算逆序數是,不計入0,這個是空位)

            估值:哈密爾頓距離 + 當前步數(這兩項可以有個比例權值);

                       (哈密爾頓距離為每個數字的當前位置和目標位置的行數差 + 列數差,的總和);

            搜索:這裡直接利用bfs的框架,使用map判重,每次想四個方向搜索即可;

說明:跑了9秒多,( ⊙ o ⊙ )!

#include <iostream>
#include <cstdlib>
#include <string>
#include <queue>
#include <map>

using namespace std;

typedef struct _qnode{
	string state;
	string path;
	int    step;
	int    h;
	bool operator < (const _qnode &n) const
    {
        return h + step > n.h + n.step;
    }
    _qnode(string State, string Path, int Step, int H) {
		state = State;
		path  = Path;
		step  = Step;
		h     = H;
	}
}qnode;

string array_to_string(int data[])
{
	string str = "123456789abcdef0";
	for (int i = 0; i < 16; ++ i) {
		str[i] = data[i] + '0';
	}
	return str;
}

int target[16] = {15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
int get_h(string state)
{
	int count = 0, place[16];
	for (int i = 0; i < 16; ++ i) {
		place[state[i]-'0'] = i;
	}
	for (int i = 0; i < 16; ++ i) {
		count += abs(place[i]%4 - target[i]%4) + abs(place[i]/4 - target[i]/4);
	}
	return count;
}

int get_zero_index(string state)
{
	for (int i = 0; i < 16; ++ i) {
		if (state[i] == '0') {
			return i;
		}
	}
	return -1;
}

char move_step[4] = {'D', 'R', 'U', 'L'};
int  dxy[4][2] = {{1,0}, {0,1}, {-1,0}, {0,-1}};
int  A_star(int start[], int over[])
{
	string s_state = array_to_string(start);
	string e_state = array_to_string(over);
	
	map <string, int> M;
	M[s_state] = 1;
	
	priority_queue <qnode> Q;
	Q.push(qnode(s_state, "", 0, get_h(s_state))); 
	while (!Q.empty()) {
		qnode now = Q.top(); Q.pop();
		if (now.step > 50) {
			continue;
		}
		if (now.state == e_state) {
			cout << now.path << endl;
			return 0;
		}
		int index = get_zero_index(now.state);
		int x = index/4, y = index%4;
		for (int k = 0; k < 4; ++ k) {
			int xx = x + dxy[k][0];
			int yy = y + dxy[k][1];
			if (xx >= 0 && xx < 4 && yy >= 0 && yy < 4) {
				string new_state = now.state;
				swap(new_state[index], new_state[xx*4+yy]);
				if (!M[new_state]) {
					M[new_state] = 1;
					Q.push(qnode(new_state, now.path + move_step[k], now.step + 1, get_h(new_state)));
				}
			}
		}
	}
	return -1;
}

int inversion(int data[])
{
	int count = 0;
	for (int i = 0; i < 16; ++ i) {
		if (data[i]) {
			for (int j = 0; j < i; ++ j) {
				count += data[j] > data[i];
			}
		}else {
			count += i/4;
		}
	}
	return count%2;
}

int input[16];
int ans[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0};
int main()
{
	int n;
	while (cin >> n)
	while (n --) {
		for (int i = 0; i < 16; ++ i) {
			cin >> input[i];
		}
		if (!inversion(input) || A_star(input, ans) == -1) {
			cout << "This puzzle is not solvable." << endl;
		}
	}
	return 0;	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值