HDU - 1043 - Eight / POJ - 1077 - Eight
先上题目:
Eight
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11243 Accepted Submission(s): 3022
Special Judge
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.
1 2 3
x 4 6
7 5 8
is described by this list:
1 2 3 x 4 6 7 5 8
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define MAX 400002
using namespace std;
typedef struct Node{
int maze[][];
int y,x;
int hash;
int h,g;
bool isok(){
return (<=y && y< && <=x && x<);
} bool operator < (const Node a) const{
if(h>a.h) return ;
if(h==a.h && g>a.g) return ;
return ;
} }Node;
Node s;
const int cantor[] = {,,,,,,,,};
const int target = ;
const int cy[]={,,,-};
const int cx[]={,-,,};
int vis[MAX];
int pre[MAX];
priority_queue<Node> p;
bool check(Node c){
int a[];
int k=,sum=;
for(int i=;i<;i++) for(int j=;j<;j++) a[k++]=c.maze[i][j];
for(int i=;i<k;i++) for(int j=i+;j<k;j++) if(a[i] && a[j] && a[i]>a[j]) sum++;
return !(sum&);
} int getHash(Node c){
int a[];
int k=,h=,sum=;
for(int i=;i<;i++) for(int j=;j<;j++) a[k++]=c.maze[i][j];
for(int i=;i<k;i++){
sum=;
for(int j=;j<i;j++){
if(a[i]<a[j]) sum++;
}
h+=sum*cantor[i];
}
return h;
} int getH(Node c){
int res=;
for(int i=;i<;i++) for(int j=;j<;j++){
res+=abs(i-(c.maze[i][j]-)/)+abs(j-(c.maze[i][j]-)%);
}
return res;
} void astar(){
while(!p.empty()) p.pop();
memset(vis,-,sizeof(vis));
memset(pre,-,sizeof(pre));
Node u,v;
s.h = getH(s);
p.push(s);
vis[s.hash]=-;
while(!p.empty()){
u = p.top();
p.pop();
for(int i=;i<;i++){
v=u;v.y+=cy[i];v.x+=cx[i];
if(!v.isok()) continue;
swap(v.maze[v.y][v.x],v.maze[u.y][u.x]);
if(!check(v)) continue;
v.hash = getHash(v);
if(vis[v.hash]!=-) continue;
vis[v.hash]=i; pre[v.hash]=u.hash;
v.g++;
v.h=getH(v);
p.push(v);
if(v.hash == target) return ; }
} } bool scan(){
char ch[];
if(!gets(ch)) return ;
int k=,l=strlen(ch);
for(int i=;i<;i++) for(int j=;j<;j++){
while(k<l && ch[k]==' ') k++;
if(ch[k]=='x'){
s.maze[i][j]=;
s.y=i; s.x=j;
}
else s.maze[i][j]=ch[k]-'';
k++;
}
return ;
} void printPath(){
string ss;
ss.clear();
int next = target;
while(pre[next]!=-){
if(vis[next]==) ss+='r';
else if(vis[next]==) ss+='l';
else if(vis[next]==) ss+='d';
else ss+='u';
next = pre[next];
}
for(int i=(int)ss.length()-;i>=;i--) putchar(ss[i]);
puts("");
} int main()
{
//freopen("data.txt","r",stdin);
while(scan()){
if(!check(s)){
puts("unsolvable");
}else{
s.hash = getHash(s);
if(s.hash == target){
puts("");
}else{
astar();
printPath();
}
}
}
return ;
}
HDU 1043
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define MAX 400002
using namespace std; const int cantor[] = {,,,,,,,,};
const int target = ;
int vis[MAX],pre[MAX];
int cy[]={,,,-};
int cx[]={,,-,};
typedef struct Node{
int maze[][];
int h,g,y,x;
int hash; bool isok(){
return (<=y && y< && <=x && x<);
} bool islegal(){
int a[],k=,sum=;
for(int i=;i<;i++) for(int j=;j<;j++) a[k++]=maze[i][j];
for(int i=;i<k;i++) for(int j=i+;j<k;j++) if(a[i] && a[j] && a[i]>a[j]) sum++;
return !(sum&);
} bool operator < (const Node o)const{
return h==o.h ? g>o.g : h>o.h;
}
}Node;
Node s;
priority_queue<Node> q; int getHash(Node e){
int a[],k=,res=;
for(int i=;i<;i++) for(int j=;j<;j++) a[k++]=e.maze[i][j];
for(int i=;i<k;i++){
int t=;
for(int j=;j<i;j++) if(a[i]<a[j]) t++;
res+=t*cantor[i];
}
return res;
} int getH(Node e){
int res=;
for(int i=;i<;i++) for(int j=;j<;j++){
res+=abs(i-(e.maze[i][j]-)/)+abs(j-(e.maze[i][j]-)%);
}
return res;
} void astar(){
Node u,v;
while(!q.empty()) q.pop();
memset(vis,-,sizeof(vis));
memset(pre,-,sizeof(pre));
s.g=;
s.h=getH(s);
q.push(s);
vis[s.hash]=-;
while(!q.empty()){
u=q.top();
q.pop();
//cout<<u.hash<<endl;
for(int i=;i<;i++){
v=u;v.y+=cy[i];v.x+=cx[i];
if(v.isok()){
swap(v.maze[v.y][v.x],v.maze[u.y][u.x]);
if(v.islegal()){
v.hash = getHash(v);
if(vis[v.hash]==-){
vis[v.hash]=i;
pre[v.hash]=u.hash;
v.g++; v.h=getH(v);
q.push(v);
if(v.hash == target) return ;
}
} }
}
}
} void printPath(){
string ss="";
int next=target;
while(pre[next]!=-){
switch(vis[next]){
case :ss+='r';break;
case :ss+='d';break;
case :ss+='l';break;
case :ss+='u';break;
}
next=pre[next];
}
for(int i=(int)ss.length()-;i>=;i--) putchar(ss[i]);
putchar('\n');
} bool get(){
char ss[];
if(gets(ss)){
int k=,l=strlen(ss);
for(int i=;i<;i++) for(int j=;j<;j++){
while(k<l && ss[k]==' ') k++;
if(ss[k]=='x'){
s.maze[i][j]=;
s.y=i; s.x=j;
}else s.maze[i][j]=ss[k]-'';
k++;
}
return ;
}
return ; } int main()
{
//freopen("data.txt","r",stdin);
while(get()){
if(s.islegal()){
s.hash = getHash(s);
if(s.hash == target) puts("");
else{
astar();
printPath();
}
}else puts("unsolvable");
}
return ;
}
POJ 1077
HDU - 1043 - Eight / POJ - 1077 - Eight的更多相关文章
- hdu 1043 pku poj 1077 Eight (BFS + 康拓展开)
http://acm.hdu.edu.cn/showproblem.php?pid=1043 http://poj.org/problem?id=1077 Eight Time Limit: 1000 ...
- HDU 1403 Eight&POJ 1077(康拖,A* ,BFS,双广)
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- Eight POJ - 1077 HDU - 1043 八数码
Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...
- HDU 1043 & POJ 1077 Eight(康托展开+BFS+预处理)
Eight Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30176 Accepted: 13119 Special ...
- HDU 1043 & POJ 1077 Eight(康托展开+BFS | IDA*)
Eight Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30176 Accepted: 13119 Special ...
- HDU 1815, POJ 2749 Building roads(2-sat)
HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...
- HDU 1043 八数码(八境界)
看了这篇博客的讲解,挺不错的.http://www.cnblogs.com/goodness/archive/2010/05/04/1727141.html 判断无解的情况(写完七种境界才发现有直接判 ...
- POJ-1077 HDU 1043 HDU 3567 Eight (BFS预处理+康拓展开)
思路: 这三个题是一个比一个令人纠结呀. POJ-1077 爆搜可以过,94ms,注意不能用map就是了. #include<iostream> #include<stack> ...
- HDU 1043 Eight(八数码)
HDU 1043 Eight(八数码) 00 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Descr ...
随机推荐
- CON1023 明明的计划
比赛说明 邀请码:a08f 来源:自出 描述:无 难度:NOIP提高组day1 赛时答疑:私信询问 奖励:无 试题列表 赛题 #A:数学题赛题 #B:明明泡妹子赛题 #C:明明去酒店 U5012 数学 ...
- webService总结(一)——使用CXF公布和调用webService(不使用Spring)
CXF和Axis2是两个比較流行的webService框架,接下来我会写几篇博客简介怎样使用这两种框架. 首先,先简介一下CXF的使用. CXF公布webService有多种方法.这里我介绍三种: 1 ...
- hdoj--5619--Jam's store(最小费用最大流)
Jam's store Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...
- Watchcow(欧拉回路)
http://poj.org/problem?id=2230 题意:给出n个field及m个连接field的边,然后要求遍历每条边仅且2次,求出一条路径来. #include <stdio.h& ...
- bzoj1231[Usaco2008 Nov]mixup2 混乱的奶牛(状压dp)
1231: [Usaco2008 Nov]mixup2 混乱的奶牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1032 Solved: 588[ ...
- Java.HttpClient绕过Https证书解决方案二
方案2 import java.io.*; import java.net.URL; import java.net.URLConnection; import java.security.Secur ...
- BZOJ 4562 搜索...
思路: 统计入度&出度 每搜到一个点 in[v[i]]--,f[v[i]]+=f[t]; if(!in[v[i]])if(out[v[i]])q.push(v[i]);else ans+=f[ ...
- 树莓派-解决apt-get upgrade速度慢的方法[更换阿里云源]
执行 apt-get upgrade 遇到速度慢的原因: 使用国外软件源 解决方法也很简单,将源换为国内环境即可,我选择阿里云 步骤 1.备份为 sources.list sudo cp /etc/a ...
- 一张图说明DIV盒子距离
虚线的宽高为你实际指定的width和height 虚线外的白色区域为padding 红色区域为border的width 红色外的区域为margin
- 【Oracle】DBMS_STATS.GATHER_TABLE_STATS
月初一直在忙保监会报送的事情,苦逼的保险行业的ETL大家都懂的.今天闲来无事查看了一下前阵子的报送存储过程,发现系统隔一段时间就会调用一次DBMS_STATS.GATHER_TABLE_STATS,所 ...