【hihocoder 1312】搜索三·启发式搜索(启发式搜索写法)
【题目链接】:http://hihocoder.com/problemset/problem/1312?sid=1092363
【题意】
【题解】
定义一个A*函数
f = step+val
这里的val是当前这个状态;每个点到目标状态的点的曼哈顿距离的绝对值;
(这个值肯定比真正需要花费的路程短)
step就为当前状态花费的步数;
把普通队列改成优先队列;
优先处理f值小的状态;
f值相同的,优先处理step值小的;
(也就是说f值大的不是不处理了,而是放到后面再处理)
这样就能较快地逼近目标状态了;
效果异常地棒
2s变成0.2s了!
【Number Of WA】
0
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const int pre[9][2] =
{
{2,2},
{0,0},{0,1},{0,2},
{1,0},{1,1},{1,2},
{2,0},{2,1}
};
const double pi = acos(-1.0);
const int N = 110;
struct node
{
int a[9],p,step,f;
friend bool operator < (node x,node y)
{
if (x.f==y.f)
{
if (x.step==y.step)
return true;
else
return x.step>y.step;
}
else
return x.f>y.f;
}
};
node init;
priority_queue <node> dl;
map <int,int> dic;
int a[9],goal;
int cs[9] = {1,2,3,4,5,6,7,8,0};
int has(int *a)
{
int x = 0;
rep1(i,0,8) x = x*10 + a[i];
return x;
}
int val(int *a)
{
int ret = 0;
rep1(i,0,8)
{
int x = i/3,y = i%3;
if (a[i]==0) continue;
ret+=abs(pre[a[i]][0]-x)+abs(pre[a[i]][1]-y);
}
return ret;
}
int bfs()
{
while (!dl.empty())
{
int p = dl.top().p;
int now = dl.top().step;
node temp;
rep1(i,0,8) temp.a[i] = dl.top().a[i];
dl.pop();
//上
if (p>2)
{
int tp = p-3;
swap(temp.a[tp],temp.a[p]);
int xzt = has(temp.a);
if (xzt==goal) return now+1;
if (dic.find(xzt)==dic.end())
{
dic[xzt] = now+1;
temp.step = now+1;
temp.p = tp;
temp.f = temp.step+val(temp.a);
dl.push(temp);
}
swap(temp.a[tp],temp.a[p]);
}
//下
if (p<6)
{
int tp = p+3;
swap(temp.a[tp],temp.a[p]);
int xzt = has(temp.a);
if (xzt==goal) return now+1;
if (dic.find(xzt)==dic.end())
{
dic[xzt] = now+1;
temp.step = now+1;
temp.p = tp;
temp.f = temp.step+val(temp.a);
dl.push(temp);
}
swap(temp.a[tp],temp.a[p]);
}
//左
if (p%3!=0)
{
int tp = p-1;
swap(temp.a[tp],temp.a[p]);
int xzt = has(temp.a);
if (xzt==goal) return now+1;
if (dic.find(xzt)==dic.end())
{
dic[xzt] = now+1;
temp.step = now+1;
temp.p = tp;
temp.f = temp.step+val(temp.a);
dl.push(temp);
}
swap(temp.a[tp],temp.a[p]);
}
//右
if (p%3!=2)
{
int tp = p+1;
swap(temp.a[tp],temp.a[p]);
int xzt = has(temp.a);
if (xzt==goal) return now+1;
if (dic.find(xzt)==dic.end())
{
dic[xzt] = now+1;
temp.step = now+1;
temp.p = tp;
temp.f = temp.step+val(temp.a);
dl.push(temp);
}
swap(temp.a[tp],temp.a[p]);
}
}
return -1;
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
goal = has(cs);
int t;
cin >> t;
while (t--)
{
dic.clear();
rep1(i,0,8)
{
cin >> a[i];
if (a[i]==0) init.p = i;
}
rep1(i,0,8) init.a[i] = a[i];
init.step = 0,init.f = init.step+val(init.a);
dic[has(init.a)] = 0;
if (has(init.a)==goal)
{
cout << 0 << endl;
continue;
}
while (!dl.empty()) dl.pop();
dl.push(init);
int ans = bfs();
if (ans==-1)
cout <<"No Solution!"<<endl;
else
cout << ans << endl;
}
return 0;
}
【hihocoder 1312】搜索三·启发式搜索(启发式搜索写法)的更多相关文章
- hihoCoder #1312 : 搜索三·启发式搜索(A*, 康托展开)
原题网址:http://hihocoder.com/problemset/problem/1312 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在小Ho的手机上有 ...
- js中的三种函数写法
js中的三种函数写法 <script type="text/javascript"> //普通的声明方式 function myFun(m,n){ alert(m+n) ...
- 【hihocoder 1312】搜索三·启发式搜索(普通广搜做法)
[题目链接]:http://hihocoder.com/problemset/problem/1312?sid=1092352 [题意] [题解] 从末状态的123456780开始逆向搜; 看它能到达 ...
- hihoCoder 1312:搜索三·启发式搜索(A* + 康托展开)
题目链接 题意 中文题意 思路 做这题的前置技能学习 康托展开 这个东西我认为就是在排列组合问题上的Hash算法,可以压缩空间. A*搜索. 这里我使用了像k短路一样的做法,从最终状态倒回去预处理一遍 ...
- hihoCoder 1393 网络流三·二分图多重匹配(Dinic求二分图最大多重匹配)
#1393 : 网络流三·二分图多重匹配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 学校的秋季运动会即将开始,为了决定参赛人员,各个班又开始忙碌起来. 小Hi和小H ...
- hihoCoder 1185 连通性·三(Tarjan缩点+暴力DFS)
#1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出 ...
- [Elasticsearch] 多字段搜索 (三) - multi_match查询和多数字段 <译>
multi_match查询 multi_match查询提供了一个简便的方法用来对多个字段执行相同的查询. NOTE 存在几种类型的multi_match查询,其中的3种正好和在“了解你的数据”一节中提 ...
- WPF之Binding的三种简单写法
环境 类代码 public class Person:INotifyPropertyChanged { private string name; public string Name { get { ...
- [Elasticsearch] 多字段搜索 (三) - multi_match查询和多数字段
multi_match查询 multi_match查询提供了一个简便的方法用来对多个字段执行相同的查询. NOTE 存在几种类型的multi_match查询,其中的3种正好和在"了解你的数据 ...
随机推荐
- 多人即时战斗游戏服务端系列[2]--90坦克Online游戏对象介绍以及渲染机制
先上类图,略大,点击此处放大: 1.先说下方接口 1.1 场景物品接口 ISceneObject : OpLog.IOpItem, IStackPoolObject 全部场景对象的基本接口,包含类型定 ...
- Java中static作用及使用方法具体解释
1.1概述: static是静态修饰符,什么叫静态修饰符呢?大家都知道,在程序中不论什么变量或者代码都是在编译时由系统自己主动分配内存来存储的.而所谓静态就是指在编译后所分配的内存会一直存在.直到程序 ...
- SQL SERVER读书笔记:TempDB
每次SQL SERVER启动的时候,会重新创建. 用于 0.临时表 1.排序 2.连接(merge join,hash join) 3.行版本控制 临时表与表变量的区别: 1)表变量是存储在内存中的, ...
- 逻辑运算0==x和x==0具体解释
看很多大牛写的程序经常看到if(0==x){运行体},而自己写的程序常用if(x==0){运行体}.刚開始的时候我还非常自信的觉得这样的表达方式是等价的,大牛们仅仅是为了显摆下与众不同的格调.当读到C ...
- gdb的使用(转)
gdb使用 转自清华大学操作系统实验指导书 gdb 是功能强大的调试程序,可完成如下的调试任务: 设置断点 监视程序变量的值 程序的单步(step in/step over)执行 显示/修改变量的值 ...
- <T extends Serializable>这是什么意思呢?看明白这个,你的问题就自然而然的明白了!
1.转自:https://blog.csdn.net/liwenqiang758/article/details/8131185 自己动手丰衣足食!!! 泛型是Java SE 1.5的新特性,泛型的本 ...
- MySQL实现表之间的字段更新
新功能写好之后,需要把以前表数据更新一下,字段数据从以前的表中获取,只更新两个字段 UPDATE TABLE1,TABLE2 SET TABLE1.COLUMN1 = TABLE2.COLUMN1 , ...
- 什么是递归?用十进制转二进制的Python函数示例说明
先上用Python写的十进制转二进制的函数代码: def Dec2Bin(dec): result = '' if dec: result = Dec2Bin(dec//2) return resul ...
- C#中的流_字节_字符_字符串之间的相互转换
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- 3.TinkPHP中的模型
1.配置数据库的连接设置 数据库的连接配置项可以在系统的主配置文件中 2.什么是模型? 模型是MVC 三大组成部分的M,作用是负责与数据表达额交互(CRUD) 3.模型的创建 命名规范:不带前缀的标明 ...