uva 10085 The most distant state(BFS + HASH)
来源:互联网 发布:5号电池多少毫安 知乎 编辑:程序博客网 时间:2024/06/06 07:39
uva 10085 The most distant state
The 8-puzzle is a square tray in which eight square tiles are placed. The remaining ninth square is uncovered. Each tile has a number on it. A tile that is adjacent to the blank space can be slid into that space. A game consists of a starting state and a specified goal state. The starting state can be transformed into the goal state by sliding (moving) the tiles around. The 8-puzzle problem asks you to do the transformation in minimum number of moves.
2
8
3
1
2
3
1
6
4
=>
8
4
7
5
7
6
5
Start
Goal
However, our current problem is a bit different. In this problem, given an initial state of the puzzle your are asked to discover a goal state which is the most distant (in terms of number of moves) of all the states reachable from the given state.
Input
The first line of the input file contains an integer representing the number of test cases to follow. A blank line follows this line.
Each test case consists of 3 lines of 3 integers each representing the initial state of the puzzle. The blank space is represented by a 0 (zero). A blank line follows each test case.
Output
For each test case first output the puzzle number. The next 3 lines will contain 3 integers each representing one of the most distant states reachable from the given state. The next line will contain the shortest sequence of moves that will transform the given state to that state. The move is actually the movement of the blank space represented by four directions : U (Up), L (Left), D (Down) and R (Right). After each test case output an empty line.
Sample Input
12 6 4
1 3 7
0 5 8
Sample Output
Puzzle #18 1 5
7 3 6
4 0 2
UURDDRULLURRDLLDRRULULDDRUULDDR
题目大意:八数码问题,不同的是,这个不是求一个八数码初始状态到达目标状态的最少步数, 而是要随便走,找出走的步数最多的那个状态,不能重复。
#include<stdio.h>#include<string.h>#include<algorithm>#include<stdlib.h>using namespace std;struct knight{int g[9], x, y, rec, dir;};knight k[400000];int move[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; //上下左右int exp[8] = {40320, 5040, 720, 120, 24, 6, 2, 1}; //阶乘int vis[400000], head, tail, output[400000];int hash(int x) { //哈希判重int sum = 0;for (int i = 0; i < 9; i++) {int cnt = 0;for (int j = i + 1; j < 9; j++) {if (k[x].g[i] > k[x].g[j]) cnt++; //统计逆序数}sum += cnt*exp[i];}return sum;}void BFS() {int px, py, temp, h, p1, p2;while (head <= tail) {for (int i = 0; i < 4; i++) {px = k[head].x + move[i][0];py = k[head].y + move[i][1];if (px < 0 || py < 0 || px >= 3 || py >= 3) continue;p1 = k[head].x * 3 + k[head].y; p2 = px * 3 + py;tail++;for (int j = 0; j < 9; j++) {k[tail].g[j] = k[head].g[j];}temp = k[tail].g[p1];k[tail].g[p1] = k[tail].g[p2];k[tail].g[p2] = temp;h = hash(tail);if (vis[h])tail--;else {vis[h] = 1;k[tail].rec = head;k[tail].dir = i;k[tail].x = px;k[tail].y = py;}}head++;}}int main() {int T, Case = 1;scanf("%d", &T);while (T--) {memset(vis, 0, sizeof(vis));head = tail = 0;for (int i = 0; i < 9; i++) {scanf("%d", &k[head].g[i]);if (k[head].g[i] == 0) { //将二维数据一维存储k[head].x = i / 3;k[head].y = i % 3;}}vis[hash(head)] = 1;BFS();printf("Puzzle #%d\n", Case++);head--;for (int i = 0; i < 9; i++) {printf("%d", k[head].g[i]);if ((i + 1) % 3 == 0) printf("\n");else printf(" ");} int cnt = 0;while (head) {output[cnt++] = k[head].dir; //dir记录空格移动方向head = k[head].rec; //rec记录当前状况的原始状况}for (int i = cnt - 1; i >= 0; i--) { //逆序输出if (output[i] == 0) printf("U");else if (output[i] == 1) printf("D");else if (output[i] == 2) printf("L");else if (output[i] == 3) printf("R");}printf("\n\n");}return 0;}
- UVA - 10085 The most distant state(bfs+hash)
- uva 10085 The most distant state(BFS + HASH)
- UVA 10085 The most distant state (hash隐式图搜索)
- UVa 10085-The most distant state(BFS,Eight).cpp
- uva 10085 - The most distant state bfs+map
- uva 10085 10085 - The most distant state
- UVA 10085 - The most distant state
- uva 10085 - The most distant state
- UVa 10085 - The most distant state
- UVA 10085 - The most distant state
- uva 10085-The most distant state
- UVa:10085 The most distant state
- uva 10085 The most distant state
- uva:10085 - The most distant state
- UVa 10085 - The Most Distant State
- uva 10085 The most distant state
- uva 10085The most distant state
- UVA 10085 The most distant state
- Java练习题-输入一个字符串,输出该字符串中字符的所有组合
- 玩转DWZ (一)---项目中怎么使用dwz
- JAVA算法题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
- Oracle merge 语句
- 软件架构经典书籍
- uva 10085 The most distant state(BFS + HASH)
- Hyper-V 虚拟机制复制功能
- hdu 1043 反向bfs标号+dfs查询+stl+状态压缩
- 1-9加减得到110 匪警请拨110,即使手机欠费也可拨通!
- D语言如何在windows的cmd下正确显示中文
- 2.12学习内容 go的map、闭包,andoid的Fragment相关
- hdoj 1301 Jungle Roads 【最小生成树】
- 分布式存储hadoop
- ST7920LCD12864反白显示