【hihocoder 1312】搜索三·启发式搜索(普通广搜做法)
【题目链接】:http://hihocoder.com/problemset/problem/1312?sid=1092352
【题意】
【题解】
从末状态的123456780开始逆向搜;
看它能到达哪些状态;
到时候O(1)输出就可以了;
用map< int,int> dic来判重;
对于状态;
用数组表示;
然后把它转化成一个对应的十进制数;
【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 cs[9] = {1,2,3,4,5,6,7,8,0};
const double pi = acos(-1.0);
const int N = 110;
struct node
{
int a[9],p,step;
};
node init;
queue <node> dl;
map <int,int> dic;
int a[9];
int has(int a[9])
{
int x = 0;
rep1(i,0,8)
x = x*10 + a[i];
return x;
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
rep1(i,0,8) init.a[i] = cs[i];
init.p = 8,init.step = 0;
dl.push(init);
dic[has(init.a)] = 0;
while (!dl.empty())
{
int p = dl.front().p;
int now = dl.front().step;
node temp;
rep1(i,0,8) temp.a[i] = dl.front().a[i];
dl.pop();
//上
if (p>2)
{
int tp = p-3;
swap(temp.a[tp],temp.a[p]);
int xzt = has(temp.a);
if (dic.find(xzt)==dic.end())
{
dic[xzt] = now+1;
temp.step = now+1;
temp.p = tp;
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 (dic.find(xzt)==dic.end())
{
dic[xzt] = now+1;
temp.step = now+1;
temp.p = tp;
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 (dic.find(xzt)==dic.end())
{
dic[xzt] = now+1;
temp.step = now+1;
temp.p = tp;
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 (dic.find(xzt)==dic.end())
{
dic[xzt] = now+1;
temp.step = now+1;
temp.p = tp;
dl.push(temp);
}
swap(temp.a[tp],temp.a[p]);
}
}
int t;
cin >> t;
while (t--)
{
rep1(i,0,8)
cin >> a[i];
int zt = has(a);
if (dic.find(zt)!=dic.end())
cout << dic[zt] << endl;
else
cout << "No Solution!" << endl;
}
return 0;
}
【hihocoder 1312】搜索三·启发式搜索(普通广搜做法)的更多相关文章
- hihoCoder #1312 : 搜索三·启发式搜索(A*, 康托展开)
原题网址:http://hihocoder.com/problemset/problem/1312 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在小Ho的手机上有 ...
- 【hihocoder 1312】搜索三·启发式搜索(启发式搜索写法)
[题目链接]:http://hihocoder.com/problemset/problem/1312?sid=1092363 [题意] [题解] 定义一个A*函数 f = step+val 这里的v ...
- hihoCoder 1312:搜索三·启发式搜索(A* + 康托展开)
题目链接 题意 中文题意 思路 做这题的前置技能学习 康托展开 这个东西我认为就是在排列组合问题上的Hash算法,可以压缩空间. A*搜索. 这里我使用了像k短路一样的做法,从最终状态倒回去预处理一遍 ...
- Codeforces 251C Number Transformation DP, 记忆化搜索,LCM,广搜
题意及思路:https://blog.csdn.net/bossup/article/details/37076965 代码: #include <bits/stdc++.h> #defi ...
- Codeforces 1105D(双层广搜)
要点 题意:可以拐弯,即哈密顿距离 注意不可以直接一个一个搜,这过程中会把下一轮的标记上,导致同一轮的其它点没能正常完成应有的搜索 因此采用双层广搜,把同一轮先都出队列再的一起搜 #include & ...
- hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Oj 24260: Lilypad Pond (神奇广搜题,状态搜索)
题目 为了让奶牛们娱乐和锻炼,约翰建造了一个美丽的池塘.这个池塘是矩形的,可以分成M×N个方格.一些格子是坚固得令人惊讶的莲花,还有一些是岩石,其余的只是美丽,纯净,湛蓝的水.贝西正在练习芭蕾舞,她站 ...
- 双向广搜+hash+康托展开 codevs 1225 八数码难题
codevs 1225 八数码难题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Yours和zero在研究A*启 ...
随机推荐
- IOS - 查找未使用的图片
实现细节都在代码里面, 帮助 -h. # -*- coding: utf-8 -*- """ 检查IOS应用图片是否使用 1. 读取有效文件: 图片(.png, .jpg ...
- hdoj1051Wooden Sticks
/*这道题目是先要排序的,依照长度或者重量排都能够. 当长度(重量)同样时就依照重量(长度)排, 从大到小或从小到大都能够! 这里我懂的.没有问题! 排序之后,问题就能够简化,(如果依照长度不等 ...
- 【Python学习笔记】-APP图标显示未读消息数目
以小米手机系统为例,当安装的某个APP有未读消息时,就会在该APP图标的右上角显示未读消息的数目.本文主要解说怎样用Python语言实现图标显示未读消息的数目.首先,还是要用到Python中PIL库, ...
- Ubuntu安装及ubuntu系统使用菜岛教程
Ubuntu是一款广受欢迎的开源Linux发行版,和其他Linux操作系统相比,Ubuntu非常易用,和Windows相容性很好,非常适合Windows用户的迁移,在其八年的成长过程中已经获得了两千多 ...
- PHP检测输入数据是否合法常用的类(转)
<?php class Fun{ function isEmpty($val) { if (!is_string($val)) return false; //是否是字符串类型 if (empt ...
- Kaggle之泰坦尼克号幸存预测估计
上次已经讲了怎么下载数据,这次就不说废话了,直接开始.首先导入相应的模块,然后检视一下数据情况.对数据有一个大致的了解之后,开始进行下一步操作. 一.分析数据 1.Survived 的情况 train ...
- golang互斥锁和读写锁
一.互斥锁 互斥锁是传统的并发程序对共享资源进行访问控制的主要手段.它由标准库代码包sync中的Mutex结构体类型代表.sync.Mutex类型(确切地说,是*sync.Mutex类型)只有两个公开 ...
- Blender Python UV 学习
Blender Python UV 学习 1. bmesh面转换 bm = bmesh.from_edit_mesh(bpy.context.edit_object.data) bm.faces.en ...
- A - A Compatible Pair
Problem description Nian is a monster which lives deep in the oceans. Once a year, it shows up on th ...
- [原创]C++带空格字符串的输入问题
字符串一直是一个重点加难点,很多笔试面试都会涉及,带空格的字符串更是十分常见,现在对字符串的输入问题进行一下总结. C++用cin输入的时候会忽略空格以后的字符,比如 char a[100]; cin ...