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 ...
随机推荐
- luogu1991 无线通讯网
题目大意 国防部计划用无线网络连接若干个边防哨所.2 种不同的通讯技术用来搭建无线网络:每个边防哨所都要配备无线电收发器:有一些哨所还可以增配卫星电话.任意两个配备了一条卫星电话线路的哨所(两边都ᤕ有 ...
- POJ2559 Largest Rectangle in a Histogram 单调栈
题目大意 有一个直方图,其所有矩形的底均是1(以后简称小矩形).给出这些矩形的高度,求这些矩形的并集中存在的面积最大的矩形(简称大矩形)的面积. 题解 大矩形的高必然一边等于一个小矩形的高,另一边小于 ...
- oc64--协议2@protocol
// // SportProtocol.h // day17 // #import <Foundation/Foundation.h> @protocol SportProtocol &l ...
- 请问在C#的Winform下如何用正则表达式限制用户只能在textBox中输入18位的身份证号码。
请问在C#的Winform下如何用正则表达式限制用户只能在textBox中输入18位的身份证号码. 2013-06-18 11:07会飞的鱼儿18 | 分类:C#/.NET | 浏览101次 不能有空 ...
- Elias-Fano编码算法——倒排索引压缩用,本质上就是桶排序数据结构思路
Elias-Fano编码过程如下:把一组整数的最低l位连接在一起,同时把高位以严格单调增的排序划分为桶. Example: 2, 3, 5, 7, 11, 13, 24 Count in unary ...
- SpringAOP使用注解实现5种通知类型
spring aop的5种通知类型都有 Before前置通知 AfterReturning后置通知 Around环绕通知 AfterThrowing异常通知 After最终通知 首先创建接口和实现类 ...
- MarkDown流程图概要
要素 流程元素定义: 名称=>类型: 显示名称 控制流程定义: 名称1([yes,no],right)->名称2 注意事项 流程元素定义在代码上部, 流程走向定义在代码下部 名称可以取中文 ...
- A - Beautiful Matrix
Problem description You've got a 5 × 5 matrix, consisting of 24 zeroes and a single number one. Let' ...
- File入门及路径名问题
package com.io.file; import java.io.File; /** * @author 王恒 * @datetime 2017年4月20日 下午2:53:29 * @descr ...
- Laravel5.1 学习笔记2, 路由
安装的说明请看文档, laravel 安装 #基本路由 你将在 app/Http/routes.php 文件定义大部分路由, 这些路由将被App\Providers\RouteServiceProvi ...