题意不说了 八数码问题:
和上篇博客 HDU 1043 题目是一样的!
但是做法上有些出入。
HDU 时间限制比较长,而且是多组输入,所以要逆向bfs 进行打表处理。
而POJ 1077 这个题目,时间是1S ,单组输入。
因此输入一组 搜索一组即可。
9个数的排列 判重用康托展开来做。
但是这个队列要自己写,STL的queue 会超时。
自己写的队列,在输出时 能很方便的用la[]数组来记录上一个 位置。 dfs 逆向输出即可!(用String 还是会超时!= =)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
using namespace std;
char cmd[2];
int jie[10];
int q[400000];
int l = 0,r = 0;
bool vis[370000];
const int dx[] = {-1,1,0,0}; ///
const int dy[] = {0,0,-1,1};
const char *flag = "udlr";
void init(){
jie[0] = jie[1] = 1;
for (int i = 2; i < 9; ++i){
jie[i] = jie[i-1] * i;
}
}
char hs[10];
int Hash(int v){
for (int i = 0; i < 9; ++i){
hs[8-i] = v % 10 + 48;
v/=10;
}
int sum = 0;
for (int i = 0; i < 9; ++i){
int t = 0;
for (int j = i+1; j < 9; ++j){
if (hs[j] < hs[i])++t;
}
sum += jie[8-i]*t;
}
return sum;
}
int goal;
int la[370000];
char al[370000];
char s[10];
char tab[5][5];
void bfs(int v){
q[r++] = v;
vis[Hash(v)] = 1;
while(l < r){
int u = q[l++];
int idu = Hash(u);
if (idu == goal) return;
int uu = u;
for (int i = 0; i < 9; ++i){
s[8-i] = uu % 10 + 48;
uu /= 10;
}
int p;
for (int i = 0; i < 9; ++i){
if (s[i] == 48)p = i;
tab[i / 3][i % 3] = s[i];
}
int x = p/3; int y = p % 3;
for (int i = 0; i < 4; ++i){
int xx = x + dx[i];
int yy = y + dy[i];
if (xx >= 0 && xx <= 2 && yy >= 0 && yy < 3){
swap(tab[xx][yy],tab[x][y]);
int v2 = 0;
for (int j = 0; j < 9; ++j){
v2 = v2 * 10 + tab[j/3][j%3] - 48;
}
int idv2 = Hash(v2);
if (!vis[idv2]){
vis[idv2] = 1;
q[r++] = v2;
al[r-1] = flag[i];
la[r-1] = l-1;
// state[idv2] = state[idu] + flag[i];
}
swap(tab[xx][yy],tab[x][y]);
}
}
}
}
void dfs(int cc){
if (cc){
// printf("c\n");
dfs(la[cc]);
putchar(al[cc]);
}
else return;
}
int main(){
int v = 0;
// memset(la,-1,sizeof la);
init();
goal = Hash(123456780);
for (int i = 0; i < 9; ++i){
scanf("%s",cmd);
if (cmd[0] == 'x')cmd[0] = '0';
v = v * 10 + cmd[0] - 48;
}
bfs(v);
if (!vis[goal])puts("unsolvable");
else {
l--;
dfs(l);
puts("");
}
return 0;
}
Eight
Description
The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed into a 4 by 4 frame with one tile missing. Let's call the missing tile 'x'; the object of the puzzle is to arrange the tiles so that they are ordered as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 x where the only legal operation is to exchange 'x' with one of the tiles with which it shares an edge. As an example, the following sequence of moves solves a slightly scrambled puzzle: 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 5 6 7 8 5 6 7 8 5 6 7 8 5 6 7 8 9 x 10 12 9 10 x 12 9 10 11 12 9 10 11 12 13 14 11 15 13 14 11 15 13 14 x 15 13 14 15 x r-> d-> r-> The letters in the previous row indicate which neighbor of the 'x' tile is swapped with the 'x' tile at each step; legal values are 'r','l','u' and 'd', for right, left, up, and down, respectively. Not all puzzles can be solved; in 1870, a man named Sam Loyd was famous for distributing an unsolvable version of the puzzle, and frustrating many people. In fact, all you have to do to make a regular puzzle into an unsolvable one is to swap two tiles (not counting the missing 'x' tile, of course). In this problem, you will write a program for solving the less well-known 8-puzzle, composed of tiles on a three by three arrangement. Input
You will receive a description of a configuration of the 8 puzzle. The description is just a list of the tiles in their initial positions, with the rows listed from top to bottom, and the tiles listed from left to right within a row, where the tiles are represented by numbers 1 to 8, plus 'x'. For example, this puzzle
1 2 3 x 4 6 7 5 8 is described by this list: 1 2 3 x 4 6 7 5 8 Output
You will print to standard output either the word ``unsolvable'', if the puzzle has no solution, or a string consisting entirely of the letters 'r', 'l', 'u' and 'd' that describes a series of moves that produce a solution. The string should include no spaces and start at the beginning of the line.
Sample Input 2 3 4 1 5 x 7 6 8 Sample Output ullddrurdllurdruldr Source |