UESTC_Eight Puzzle 2015 UESTC Training for Search Algorithm & String<Problem F>
F - Eight Puzzle
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
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 a4 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. To simplify this problem, you should print the minimum steps only.
Input
There are multiple test cases.
For each test case, 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.Otherwise, output an integer which equals the minimum steps.
Sample input and output
| Sample Input | Sample Output |
|---|---|
1 2 x 4 5 3 7 8 6 |
2 |
Hint
Any violent algorithm may gain TLE. So a smart method is expected.
The data used in this problem is unofficial data prepared by hzhua. So any mistake here does not imply mistake in the offcial judge data.
解题报告:
HINT部分已经知道本题数据很强,因此暂时不考虑普通bfs,那么我们可以考虑双广和A*两种算法..
关于两种方法的结果:
1.A*超时
2.双广AC
可能的原因:首先如果没有解,都退化成普通bfs,这点并没有区别,那么只能说明在有解的时候双广比A*高效很多
当然效率还可以进一步提升,那就是奇偶性剪枝,想了解的话可以百度
双广代码(有奇偶性剪枝)
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std; const int maxhashsize = + ;
const int maxstatussize = 1e6 + ;
int vis1[maxhashsize],vis2[maxhashsize];
int fac[];
int dir[][] = {-,,,,,-,,}; typedef struct status
{
char s[] , step;
int val;
}; status q1[maxstatussize],q2[maxstatussize]; int gethashvalue(const status &x)
{
int res = ;
for(int i = ; i < ; ++ i)
{
int cot = ;
for(int j = i+ ; j < ; ++ j)
if (x.s[i] > x.s[j])
cot++;
res += fac[-i]*cot;
}
return res;
} status st,ed; int bfs()
{
int front1 = , rear1 = ;
int front2 = , rear2 = ;
q1[rear1++] = st;
q2[rear2++] = ed;
if (st.val == ed.val )
return ;
while(front1 < rear1 || front2 < rear2)
{
//
{
status ns = q1[front1++];
int x,y,step = ns.step,oripos;
for(int i = ; i < ; ++ i)
if (!ns.s[i])
{
x = i / ;
y = i % ;
oripos = i;
break;
}
for(int i = ; i < ; ++ i)
{
int newx = x + dir[i][];
int newy = y + dir[i][];
if (newx >= || newx < || newy >= || newy < )
continue;
int newpos = newx*+newy;
status ss;
memcpy(&ss,&ns,sizeof(struct status));
swap(ss.s[newpos],ss.s[oripos]);
int newhash = gethashvalue(ss);
if (vis1[newhash] != -)
continue;
ss.step ++ ;
if (vis2[newhash] != -)
return ss.step + vis2[newhash];
vis1[newhash] = ss.step;
ss.val = newhash;
q1[rear1++] = ss;
}
}
//**************************
{
status ns = q2[front2++];
int x,y,step = ns.step,oripos;
for(int i = ; i < ; ++ i)
if (!ns.s[i])
{
x = i / ;
y = i % ;
oripos = i;
break;
}
for(int i = ; i < ; ++ i)
{
int newx = x + dir[i][];
int newy = y + dir[i][];
if (newx >= || newx < || newy >= || newy < )
continue;
int newpos = newx*+newy;
status ss;
memcpy(&ss,&ns,sizeof(struct status));
swap(ss.s[newpos],ss.s[oripos]);
int newhash = gethashvalue(ss);
if (vis2[newhash] != -)
continue;
ss.step ++ ;
if (vis1[newhash] != -)
return ss.step + vis1[newhash];
vis2[newhash] = ss.step;
ss.val = newhash;
q2[rear2++] = ss;
}
}
}
return -;
} bool input()
{
char ch = getchar();
if (ch == EOF) return false;
memset(vis1,-,sizeof(vis1));
memset(vis2,-,sizeof(vis2));
if (ch == 'x')
st.s[] = ;
else
st.s[] = ch-'';
getchar();
for(int i = ; i <= ; ++ i)
{
ch = getchar();getchar();
if (ch == 'x')
st.s[i] = ;
else
st.s[i] = ch-'';
}
st.step = ;
vis1[gethashvalue(st)] = ; // Init for vis
st.val = gethashvalue(st);
vis2[gethashvalue(ed)] = ;
ed.val = gethashvalue(ed);
return true;
} int main(int argc,char *argv[])
{
fac[] = ;
for(int i = ; i <= ; ++ i)
fac[i] = i*fac[i-];
for(int i = ; i < ; ++ i)
ed.s[i] = i + ;
ed.s[] = ;
ed.step = ;
while(input())
{
int sum = ;
//奇偶性判断
for(int i = ; i < ; ++ i)
{
if (st.s[i] == )
continue;
for(int j = ; j < i ; ++ j)
if (st.s[j] > st.s[i])
sum++;
}
if ( sum % & )
{
cout << "unsolvable" << endl;
continue;
}
int ans = bfs();
if (ans == -)
cout << "unsolvable" << endl;
else
cout << ans << endl;
}
return ;
}
UESTC_Eight Puzzle 2015 UESTC Training for Search Algorithm & String<Problem F>的更多相关文章
- UESTC_韩爷的梦 2015 UESTC Training for Search Algorithm & String<Problem N>
N - 韩爷的梦 Time Limit: 200/100MS (Java/Others) Memory Limit: 1300/1300KB (Java/Others) Submit Stat ...
- UESTC_Palindromic String 2015 UESTC Training for Search Algorithm & String<Problem M>
M - Palindromic String Time Limit: 3000/1000MS (Java/Others) Memory Limit: 128000/128000KB (Java ...
- UESTC_秋实大哥の恋爱物语 2015 UESTC Training for Search Algorithm & String<Problem K>
K - 秋实大哥の恋爱物语 Time Limit: 5000/2000MS (Java/Others) Memory Limit: 32000/32000KB (Java/Others) Su ...
- UESTC_吴队长征婚 2015 UESTC Training for Search Algorithm & String<Problem E>
E - 吴队长征婚 Time Limit: 10000/4000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- UESTC_基爷的中位数 2015 UESTC Training for Search Algorithm & String<Problem D>
D - 基爷的中位数 Time Limit: 5000/3000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- UESTC_基爷与加法等式 2015 UESTC Training for Search Algorithm & String<Problem C>
C - 基爷与加法等式 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_邱老师降临小行星 2015 UESTC Training for Search Algorithm & String<Problem B>
B - 邱老师降临小行星 Time Limit: 10000/5000MS (Java/Others) Memory Limit: 65536/65535KB (Java/Others) Su ...
- UESTC_Ferris Wheel String 2015 UESTC Training for Search Algorithm & String<Problem L>
L - Ferris Wheel String Time Limit: 3000/1000MS (Java/Others) Memory Limit: 43000/43000KB (Java/ ...
- UESTC_全都是秋实大哥 2015 UESTC Training for Search Algorithm & String<Problem J>
J - 全都是秋实大哥 Time Limit: 5000/2000MS (Java/Others) Memory Limit: 32000/32000KB (Java/Others) Subm ...
随机推荐
- Android Studio代码着色插件
文章将给大家分享Studio中代码高亮插件,个人觉得换个代码着色方式还是挺有必要的,起码让视觉上有个变换,感官上爽一些.就像吃惯了大鱼大肉,偶尔也来点青菜萝卜吧.以下是个人喜欢的几款,给个效果图大家看 ...
- 【HDU1102】Constructing Roads(MST基础题)
最小生成树水题.prim一次AC #include <iostream> #include <cstring> #include <cstdlib> #includ ...
- AngularJS 2调用.net core WebAPI的几个坑
前几天,按照AngularJS2的英雄指南教程走了一遍,教程网址是http://origin.angular.live/docs/ts/latest/tutorial/. 在步骤完成后,又更进一步,在 ...
- 判断一个int 型整数 是否为回文数
leetcode 上的题目 Determine whether an integer is a palindrome. Do this without extra space. 由于不能使用额外空间, ...
- android jni (5)——Field & Method --> Accessing Mehtod
在java编程语言中有非静态成员函数和静态成员函数,JNI允许我们访问到java中的成员函数,然后再jni中调用,这里我就来举例说明在jni中是如何做到的. 我们先在java中定义2个成员函数,一个非 ...
- Linux正則表達式-反复出现的字符
星号(*)元字符表示它前面的正則表達式能够出现零次或多次.也就是说,假设它改动了单个字符.那么该字符能够在那里也能够不在那里,而且假设它在那里,那可能会不止出现一个.能够使用星号元字符匹配出如今引號中 ...
- hdu4506小明系列故事——师兄帮帮忙 (用二进制,大数高速取余)
Problem Description 小明自从告别了ACM/ICPC之后,就開始潜心研究数学问题了,一则能够为接下来的考研做准备,再者能够借此机会帮助一些同学,尤其是美丽的师妹.这不,班里唯一的女生 ...
- 常用LINUX脚本汇总(1)
1.查看磁盘使用空间 df -hl 2.查看文件或者文件夹大小 du -sh 文件(夹)名 查看文件大小 AIX系统为du -sg 3.查看当前用户下定时任务列表crontab -l 4.修改定时 ...
- C#中方法Show.和ShowDialog的使用区别
show()是非模式窗体. showDialog()是模式窗体. 如果这个时候用Show的话,则会发生的事情是,打开子窗体的同时主窗体又显示出来,而使用ShowDialog()的时候主要当子窗体关闭的 ...
- Android-----输入法的显示和隐藏
/** * 控制手机虚拟键盘的显示和隐藏 */public class InputMethodUtil { /** * 隐藏虚拟键盘 * @param v 参数v为获取焦点对象view */ pub ...