Eight

it2022-05-05  162

传送门HDU1043

描述

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 1213 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:

12345 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 1213 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, andfrustrating 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 threearrangement.

输入

You will receive, several descriptions of configuration of the 8 puzzle. One 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 puzzle1 2 3x 4 67 5 8is described by this list:1 2 3 x 4 6 7 5 8

输出

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. Do not print a blank line between cases.

样例

Input2 3 4 1 5 x 7 6 8

Outputullddrurdllurdruldr

题解

题意:3*3的表格,x可以和上下左右交换,最后通过移动x使整个表格有序且x在最后一格。求移动方案,输出路径,没有则输出-1法一:从输入字符串和目标串开始进行双向bfs(3000+ms)法二:bfs预处理出目标串到其他可达串的路径,然后O(t)查询,t为答案长度(300+ms)因为内存有限,把x视为9,用康托展开存储答案。

Code(法一)

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107//双向bfs+康托#include<bits/stdc++.h>#define INIT(a,b) memset(a,b,sizeof(a))#define LL long longusing namespace std;const int inf=0x3f3f3f3f;const int N=362885;const int mod=1e9+7;string title="12345678x";string mp="111111111";string ops[2]={"udlr","durl"};map<int,string> op[2];int rec[4]={-3,3,-1,1};int vis[2][N];int fac[20];struct node{ string sta; int xsit;};queue<node> que[2];bool judge(int x,int to){ if(to<0||to>=9)return false; if((x==2&&to==3)||(x==3&&to==2))return false; if((x==5&&to==6)||(x==6&&to==5))return false; return true;}int cantor(string a){ int ans=0,c=0; for(int i=0;i<9;i++){ c=0; for(int j=i+1;j<9;j++) if(a[j]<a[i])c++; ans+=c*fac[9-i-1]; } return ans;}int solve(int t){ int sum=que[t].size(); node q; while(sum--){ q=que[t].front();que[t].pop(); int can=cantor(q.sta); if(vis[t^1][can]) return can; for(int i=0;i<4;i++){ int to=q.xsit+rec[i]; if(judge(q.xsit,to)){ swap(q.sta[q.xsit],q.sta[to]); int _can=cantor(q.sta); if(!vis[t][_can]){ vis[t][_can]=1; que[t].push((node){q.sta,to}); op[t][_can]=op[t][can]+ops[t][i]; } swap(q.sta[q.xsit],q.sta[to]); } } } return -1;}void bfs(int t){ INIT(vis,0); while(!que[0].empty())que[0].pop(); while(!que[1].empty())que[1].pop(); que[0].push((node){mp,t}); que[1].push((node){title,8}); op[0].clear(),op[1].clear(); op[0][cantor(mp)]=""; op[1][0]=""; int ans; while(que[0].size()||que[1].size()){ int ans1=solve(0); int ans2=solve(1); if(ans1!=-1||ans2!=-1){ ans=(ans1==-1)?ans2:ans1; break; } } reverse(op[1][ans].begin(),op[1][ans].end()); cout<<op[0][ans]<<op[1][ans]<<endl;}int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); fac[0]=1; for(int i=1;i<10;i++){ fac[i]=i*fac[i-1]; } while(cin>>mp[0]){ int t=0; for(int i=1;i<9;i++){ cin>>mp[i]; if(mp[i]=='x') t=i; } int sum=0; for(int i=1;i<9;i++){ if(mp[i]=='x') continue ; for(int j=i-1;j>=0;j--){ if(mp[j]=='x') continue; if(mp[j]>mp[i]) sum++; } } if(sum & 1) cout<<"unsolvable"<<endl;//逆序数为奇数则无解 else bfs(t); } return 0;}

Code(法二)

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687//预处理答案#include<bits/stdc++.h>#define INIT(a,b) memset(a,b,sizeof(a))#define LL long longusing namespace std;const int inf=0x3f3f3f3f;const int N=1e6+7;const int mod=1e9+7;string title="12345678x";string mp="111111111";string ops="durl";int rec[4]={-3,3,-1,1};int pre[N],ans[N],vis[N],fac[10];struct node{ string sta; int xsit;};queue<node> que[2];bool judge(int x,int to){ if(to<0||to>=9)return false; if((x==2&&to==3)||(x==3&&to==2))return false; if((x==5&&to==6)||(x==6&&to==5))return false; return true;}int cantor(string a){ int ans=0,c=0; for(int i=0;i<9;i++){ c=0; for(int j=i+1;j<9;j++) if(a[j]<a[i])c++; ans+=c*fac[9-i-1]; } return ans;}void bfs(){ INIT(vis,0); INIT(pre,-1); queue<node>que; que.push((node){title,8}); int can=cantor(title); vis[can]=1; while(!que.empty()){ node q=que.front();que.pop(); for(int i=0;i<4;i++){ int to=q.xsit+rec[i]; can=cantor(q.sta); if(judge(q.xsit,to)){ swap(q.sta[q.xsit],q.sta[to]); int _can=cantor(q.sta); if(!vis[_can]){ vis[_can]=1; que.push((node){q.sta,to}); ans[_can]=i; pre[_can]=can; } swap(q.sta[q.xsit],q.sta[to]); } } }}void print(int c){ if(pre[c]==-1){ cout<<endl; return; } cout<<ops[ans[c]]; print(pre[c]);}int main(){ fac[0]=1; for(int i=1;i<10;i++){ fac[i]=i*fac[i-1]; } bfs(); while(cin>>mp[0]){ int t=0; for(int i=1;i<9;i++){ cin>>mp[i]; if(mp[i]=='x') t=i; } int can=cantor(mp); if(!vis[can]) cout<<"unsolvable"<<endl; else print(can); } return 0;}

最新回复(0)