Description
Problem A
The Most Distant State
Input: standard input
Output: standard output
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
1Sample Output
Puzzle #1
总算敲出来了,痛苦哎。。。
#include<iostream> #include<algorithm> #include<stdio.h> #include<queue> #include<vector> #include<string.h> #include<stack> #include<map> using namespace std; vector<int>mat; int dx[]={1,-1,0,0}; int dy[]={0,0,1,-1}; int A[]={1,1,2,6,24,120,720,5040,40320}; int hash_k(vector<int>T) { int ans=0; for(int i=0;i<9;i++){ int t=T[i]; for(int j=0;j<i;j++){ if(T[j]<T[i])t--; } ans+=t*A[8-i]; } return ans; } int vis[362890]; queue<vector<int> >q; map<vector<int>,vector<int> >p; stack<char>FF; char get_(vector<int>p,vector<int>q){ int z1,z2; for(z1=0;z1<9;z1++)if(!p[z1])break; int x1=z1/3,y1=z1%3; for(z2=0;z2<9;z2++)if(!q[z2])break; int x2=z2/3,y2=z2%3; if(x1!=x2){ if(x1-x2==1)return 'U'; else return 'D'; } if(y1-y2==1)return 'L'; else return 'R'; } int main() { //freopen("in.txt","r",stdin); int T; cin>>T; int cas=1; while(T--){ memset(vis,0,sizeof(vis)); mat.clear(); while(!q.empty())q.pop(); while(!FF.empty())FF.pop(); p.clear(); vector<int>ans; int num; for(int i=0;i<9;i++){ scanf("%d",&num); mat.push_back(num); } q.push(mat); vis[hash_k(mat)]=1; vector<int>u; vector<int>t; while(!q.empty()) { u=q.front(); q.pop(); int z=0; for(z=0;z<9;z++)if(!u[z])break; int x=z/3,y=z%3; for(int i=0;i<4;i++) { int nx=x+dx[i],ny=y+dy[i]; if(nx>=0&&nx<3&&ny>=0&&ny<3) { int nz=nx*3+ny; t=u; //cout<<z<<' '<<nz<<endl; t[nz]=u[z]; t[z]=u[nz]; int ok=hash_k(t); if(!vis[ok]){ vis[ok]=1; q.push(t); p[t]=u; ans=t; } } } } cout<<"Puzzle #"<<cas++<<endl; for(int i=0;i<9;i++) printf("%d%c",ans[i],i%3==2?'\n':' '); FF.push('\n'); while(1){ if(ans==mat)break; FF.push(get_(p[ans],ans)); ans=p[ans]; } while(!FF.empty()){ putchar(FF.top()); FF.pop(); } putchar('\n'); } return 0; }
UVA 10085(bfs+康拓展开)八数码问题,布布扣,bubuko.com
原文地址:http://blog.csdn.net/acvcla/article/details/29604619