題意: 起初定28張卡牌的排列,把其中11, 21, 31, 41移動到第一列,然后就出現(xiàn)四個空白,每個空白可以用它的前面一個數(shù)的下一個數(shù)填充,例如43后面的空格可以用44填充,但是47后面即使有空白也無法填充,因為沒有48。
思路:每次有四個空格,面臨四個選擇,直接用bfs搜索,狀態(tài)保存的話就自己設(shè)計一個哈希函數(shù),如果找不到完美哈希一定要解決沖突。
我設(shè)計的哈希函數(shù):我用的是雙哈希,第一個哈希值用于查找,第二個哈希值可以看做整個卡牌狀態(tài)的壓縮,如果有兩種狀態(tài)有同一個第一哈希,我就用第二哈希來判斷它們是否一致(第二哈希也一樣的可能性幾乎沒有)。我的時間是1201ms,有的200多ms就過了,說明決定這題快不快就在于哈希函數(shù)判重快不快。
注意:空白前面可能也是空白喲。
AC代碼: 1201ms
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <queue>#include<utility>using namespace std;typedef long long LL;typedef pair<int, int> PI;typedef int state[4][8];const int maxn = 1e6 + 10;const int hash1 = 1000007, hash2 = 1007; //雙哈希解決沖突 int goal[][8] = { 11,12,13,14,15,16,17,0, 21,22,23,24,25,26,27,0, 31,32,33,34,35,36,37,0, 41,42,43,44,45,46,47,0};int st[4][8];int sp[][2] = { 0,0, 1,0, 2,0, 3,0};struct node{ int pip; int next; node() { } node(int pip, int next): pip(pip), next(next){ }}HASH[maxn];int head[maxn];struct card{ int a[4][8]; int pos[4][2]; //四個空位 int step; card() { } card(int b[][8], int p[][2], int step):step(step) { memcpy(a, b, sizeof(a)); memcpy(pos, p, sizeof(pos)); }};PI get_hash(int a[][8]) { LL h1 = 0, h2 = 0; for(int i = 0; i < 4; ++i) for(int j = 0; j < 8; ++j) { h1 = (h1 * 100 + a[i][j]) % hash1; h2 = (h2 * 100 + a[i][j]) % hash2; } return make_pair((int)h1, (int)h2);}int cur = 0;bool Insert(PI pi) { int h = pi.first, val = pi.second; int u = head[h]; while(u != -1) { if(val == HASH[u].pip) return false; u = HASH[u].next; } HASH[cur] = node(val, head[h]); head[h] = cur; cur++; return true;}int find(int a[][8], int key) { for(int i = 0; i < 4; ++i) for(int j = 0; j < 8; ++j) { if(a[i][j] == key) return i * 8 + j; }}int bfs() { memset(head, -1, sizeof(head)); queue<card>q; q.push(card(st, sp, 0)); PI pi = get_hash(st); Insert(pi); while(!q.empty()) { card cd = q.front(); q.pop(); state &a = cd.a; if(!memcmp(goal, a, sizeof(goal))) return cd.step; for(int i = 0; i < 4; ++i) { int x = cd.pos[i][0], y = cd.pos[i][1]; int val = a[x][y - 1]; if(val == 0 || val % 10 == 7) continue; int pos = find(a, val + 1); int px = pos / 8, py = pos % 8; swap(a[x][y], a[px][py]); PI pi = get_hash(a); if(!Insert(pi)) { //判重 swap(cd.a[x][y], cd.a[px][py]); continue; } cd.pos[i][0] = px, cd.pos[i][1] = py; cd.step++; q.push(cd); cd.pos[i][0] = x, cd.pos[i][1] = y; swap(a[x][y], a[px][py]); cd.step--; } } return -1;}int f[] = {11,21,31,41};int main() { int T; scanf("%d", &T); while(T--) { cur = 0; for(int i = 0; i < 4; ++i) for(int j = 0; j < 8; ++j) { if(j == 0) st[i][j] = 0; else scanf("%d", &st[i][j]); } for(int i = 0; i < 4; ++i) { for(int j = 1; j < 8; ++j) { switch(st[i][j]) { case 11: swap(st[i][j], st[0][0]); sp[0][0] = i, sp[0][1] = j; break; case 21: swap(st[i][j], st[1][0]); sp[1][0] = i, sp[1][1] = j; break; case 31: swap(st[i][j], st[2][0]); sp[2][0] = i, sp[2][1] = j; break; case 41: swap(st[i][j], st[3][0]); sp[3][0] = i, sp[3][1] = j; break; } } } PRintf("%d/n", bfs()); } return 0;} 如有不當(dāng)之處歡迎指出!
新聞熱點
疑難解答