UVA 10085(bfs+康拓展开)八数码问题
2014-06-09 18:43
302 查看
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.
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
1
2 6 4
1 3 7
0 5 8
Sample Output
Puzzle #1
8 1 5
7 3 6
4 0 2
UURDDRULLURRDLLDRRULULDDRUULDDR
__________________________________________________________________________________________
Rezaul Alam Chowdhury
"A fool looks for happiness in the distance, those who are intelligent grow it under their own feet."
总算敲出来了,痛苦哎。。。
#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;
}
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
1
2 6 4
1 3 7
0 5 8
Sample Output
Puzzle #1
8 1 5
7 3 6
4 0 2
UURDDRULLURRDLLDRRULULDDRUULDDR
__________________________________________________________________________________________
Rezaul Alam Chowdhury
"A fool looks for happiness in the distance, those who are intelligent grow it under their own feet."
总算敲出来了,痛苦哎。。。
#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 The most distant state(八数码问题)
- UVA 716 - Commedia dell' arte(三维N数码问题)
- UVALive 6665 Dragon&#226;s Cruller --BFS,类八数码问题
- uva 10085 The most distant state(八数码)
- 康托展开求八数码问题
- poj 1077 Eight 八数码问题( 康拓展开+BFS状态压缩)
- 8数码,欺我太甚!<bfs+康拓展开>
- UVALive - 5542 Commedia dell' arte 三维n数码问题
- UVALive 6665 Dragonâs Cruller --BFS,类八数码问题
- POJ 1077八数码问题(cantor展开+BFS)
- 蓝桥杯 历届试题 九宫重排 (八数码问题--康托展开去重 + bfs搜索)
- HDOJ 1043 八数码问题(经典题) bfs+托康展开
- cdoj 414 八数码 (双向bfs+康拓展开,A*)
- UVA 1604 立体八数码问题——编码解码+哈希+dfs
- 【康托展开+状压BFS】poj1077 Eight(八数码问题)
- HDU 1043 八数码问题 A*搜索+康拓展开+逆序对判断+路径输出
- 康托展开与八数码问题
- 八数码问题的 A* 算法解答
- ASP.NET应用程序中TreeView 控件 即需填充 TreeNode 数据时,节点展开问题
- 八数码问题