Eight HDU - 1043(八数码+搜索)
題意:
就是還原八數碼。輸出操作。
題目:
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 45 6 7 89 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:?
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, 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 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. Do not print a blank line between cases.?
Sample Input
2 3 4 1 5 x 7 6 8Sample Output
ullddrurdllurdruldr分析:
用廣搜打表,根據廣搜的性質,第一次出現某種狀態,即是最短路
注意,方向問題。最終結果倒著輸出
(因為此題用搜索即可做出,網上有康拓展開(利用康拓展開判重。通過康拓展開知一共有362879種狀態。用逆序數判斷可行解見八數碼可行解。然后寬搜。)和A*(A*是一種啟發式搜索,g為已花代價,h為估計的剩余代價,而A*是根據f=g+h作為估價函數進行排列,也就是優先選擇可能最優的節點進行擴展。)做法,自行去看,我不會QAQ)orz
康拓展開:https://www.xuebuyuan.com/3261380.html
A*:https://blog.csdn.net/acm_cxlove/article/details/7745323
#include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> #include<queue> #include<map> #include<string> using namespace std; map<int,int>book; map<int,string>ans; int c[4][2]= {0,1,1,0,0,-1,-1,0}; struct node {int x,y;string s;int k[3][3]; }; void dfs() {queue<node>q;node u,v;u.x=2,u.y=2;int xx=1;for(int i=0; i<3; i++)for(int j=0; j<3; j++)u.k[i][j]=xx++;u.k[2][2]=0;book[123456780]=1;q.push(u);while(!q.empty()){v=q.front();q.pop();for(int i=0; i<4; i++){int yy=1;int dx=v.x+c[i][0];int dy=v.y+c[i][1];if(dx<0||dy<0||dx>=3||dy>=3)continue;for(int o=0; o<3; o++)for(int j=0; j<3; j++){u.k[o][j]=v.k[o][j];}swap(u.k[dx][dy],u.k[v.x][v.y]);for(int o=0; o<3; o++)for(int j=0; j<3; j++){yy=yy*10+u.k[o][j];}if(book[yy])continue;book[yy]=1;u.x=dx;u.y=dy;if(i==0)u.s=v.s+'l';else if(i==1)u.s=v.s+'u';else if(i==2)u.s=v.s+'r';elseu.s=v.s+'d';q.push(u);ans[yy]=u.s;}} } int main() {char w[50];dfs();while(gets(w)){int ant=1;int len=strlen(w);for(int i=0; i<len; i++){if(w[i]>='1'&&w[i]<='8')ant=ant*10+w[i]-'0';else if(w[i]=='x')ant*=10;}if(book[ant]){string m=ans[ant];int l=m.size();for(int i=l-1; i>=0; i--)cout<<m[i];cout<<endl;}elsecout<<"unsolvable"<<endl;}return 0; }?
總結
以上是生活随笔為你收集整理的Eight HDU - 1043(八数码+搜索)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 磁盘文件系统、挂载
- 下一篇: Tree Cutting POJ - 2