【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种正好和在"了解你的数据 ...
随机推荐
- mysql稳定的版本号选择及下载说明(2014-11-10)
怎样选择新稳定的版本号 mysql的版本号大概能够分为Alpha.Beta.GA. GA版即mysql官方公布的稳定版本号. 怎样在官方下载Mysql 能够通过http:// ...
- 【Python学习笔记】-APP图标显示未读消息数目
以小米手机系统为例,当安装的某个APP有未读消息时,就会在该APP图标的右上角显示未读消息的数目.本文主要解说怎样用Python语言实现图标显示未读消息的数目.首先,还是要用到Python中PIL库, ...
- Android的编译环境--Build系统【转】
本文转载自:http://blog.csdn.net/kitty_landon/article/details/60764232 Android是一个庞大的系统,包含太多的模块,各种模块的类型也有10 ...
- PHP 和 Java 的主要区别有哪些?
PHP 和 Java 的主要区别有哪些? 其实Java方面我要学的真的还有很多,要是有大项目的机会和经验就好,所以提前我肯定要把基础打扎实. 我要学的还有很多,比如前段,后端,还有linux,还有肯定 ...
- 找新朋友(hdoj--1286--欧拉函数)
欢迎参加--每周六晚的BestCoder(有米!) 找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- Codeforces Round #512 (Div. 2) D.Vasya and Triangle 数学
题面 题意:给你n,m,k,在你在(0,0)到(n,m)的矩形内,选3个格点(x,y都是整数),使得三角形面积为n*m/k,不能找到则输出-1 题解:由毕克定理知道,格点多边形的面积必为1/2的整数倍 ...
- ES6 Proxy 性能之我见
ES6 Proxy 性能之我见 本文翻译自https://thecodebarbarian.com/thoughts-on-es6-proxies-performance Proxy是ES6的一个强力 ...
- EditPlus修改主题方法
在“EditPlus.exe”或"EditPlus64.exe"所在的目录下找到"editplus_u.ini"文件(如果不存在就新建一个),修改这个文件即可更 ...
- 定义maven的项目结构
创建一个Maven 的父项目 新建一个maven项目,选中create a simple project 填写以下内容: 如下内容: Group Id :edu.zipcloud.cloudstree ...
- Android AlertDialog 动态更新里面的ListView数据
1:和ListView的数据跟新是基本一样的. 2:Activity代码示例 public class MainActivity extends AppCompatActivity { AlertDi ...