NOIP 模拟 玩积木 - 迭代加深搜索 / bfs+hash+玄学剪枝
题目大意:
有一堆积木,0号节点每次可以和其上方,下方,左上,右下的其中一个交换,问至少需要多少次达到目标状态,若步数超过20,输出too difficult
目标状态:
0
1 1
2 2 2
3 3 3 3
4 4 4 4 4
5 5 5 5 5 5
题目分析:
因为前段时间做了一道转花盆刻骨铭心,所以一看到这题就开始bfs+hash,明知道过不了,但谁知道姿势正确得了85分,后来出题人告诉数据最大步数才14,我把搜索停止条件改成了18,瞬间ac。。。
正解有很多种:迭代加深,双向搜索,\(A^{*}\)……
考后写了一下迭代加深,不是一般的快,具体讲解请百度骑士.
code
bfs + hash + 玄学剪枝
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
namespace IO{
inline int read(){
int i = 0, f = 1; char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0' && ch <= '9'; ch = getchar()) i = (i << 3) + (i << 1) + (ch - '0');
return i * f;
}
inline void wr(ll x){
if(x < 0) putchar('-'), x = -x;
if(x > 9) wr(x / 10);
putchar(x % 10 + '0');
}
}using namespace IO;
const int N = 10, M = 30;
int T, bin[N], ans;
char initMap[M];
struct node{char s[M]; int cnt;};
queue<node> que; bool flag;
typedef unsigned long long ull;
ull desVal;
const int Mod = 233333, H = 31;
vector<ull> G[Mod + 5];
vector<int> used;
inline bool insert(ull x){
int key = x % Mod;
for(int i = 0; i < G[key].size(); i++)
if(G[key][i] == x) return false;
G[key].push_back(x); used.push_back(key); return true;
}
inline ull toHash(char s[M]){
ull ret = 0;
for(int i = 1; i <= 21; i++) ret = ret * H + s[i] - '0';
return ret;
}
inline void init(){
bin[0] = 0;for(int i = 1; i <= 6; i++) bin[i] = bin[i - 1] + i;
for(int i = 1; i <= 6; i++)
for(int j = 1; j <= i; j++)
desVal = desVal * H + i - 1;
}
inline pair<int, int> get0Pos(char s[M]){
pair<int, int> ret;
for(int i = 1; i <= 6; i++)
for(int j = 1; j <= i; j++)
if(s[bin[i - 1] + j] == '0') {ret.first = i, ret.second = j; return ret;}
}
inline void add(char s[M], int cnt){
node tmp;
for(int i = 1; i <= 21; i++) tmp.s[i] = s[i];
tmp.cnt = cnt;
que.push(tmp);
}
int main(){
T = read(); init();
while(T--){
for(int i = 0; i < used.size(); i++) G[used[i]].clear();
flag = false; ans = 0;
for(int i = 1; i <= 6; i++)
for(int j = 1; j <= i; j++) initMap[bin[i - 1] + j] = read() + '0';
ull hashVal = toHash(initMap);
if(hashVal == desVal){
wr(0); putchar('\n');
continue;
}
insert(hashVal);
while(!que.empty()) que.pop();
add(initMap, 0);
while(!que.empty()){
char now[M];
node tmp = que.front();
int cnt = tmp.cnt;
for(int i = 1; i <= 21; i++) now[i] = tmp.s[i];
pair<int, int> pos = get0Pos(now);
//leftup
if(pos.second != 1){
swap(now[bin[pos.first - 1] + pos.second], now[bin[pos.first - 1] + pos.second - (bin[pos.first] - bin[pos.first - 1])]);
ull hashVal = toHash(now);
if(hashVal == desVal){flag = true, ans = cnt + 1; break;}
else if(cnt + 1 == 18) break;
if(insert(hashVal)) add(now, cnt + 1);
swap(now[bin[pos.first - 1] + pos.second], now[bin[pos.first - 1] + pos.second - (bin[pos.first] - bin[pos.first - 1])]);
}
//up
if(pos.second != bin[pos.first] - bin[pos.first - 1]){
swap(now[bin[pos.first - 1] + pos.second], now[bin[pos.first - 1] + pos.second - (bin[pos.first] - bin[pos.first - 1]) + 1]);
ull hashVal = toHash(now);
if(hashVal == desVal){flag = true, ans = cnt + 1; break;}
else if(cnt + 1 == 18) break;
if(insert(hashVal)) add(now, cnt + 1);
swap(now[bin[pos.first - 1] + pos.second], now[bin[pos.first - 1] + pos.second - (bin[pos.first] - bin[pos.first - 1]) + 1]);
}
//down
if(pos.first != 6){
swap(now[bin[pos.first - 1] + pos.second], now[bin[pos.first] + pos.second]);
ull hashVal = toHash(now);
if(hashVal == desVal){flag = true, ans = cnt + 1; break;}
else if(cnt + 1 == 18) break;
if(insert(hashVal)) add(now, cnt + 1);
swap(now[bin[pos.first - 1] + pos.second], now[bin[pos.first] + pos.second]);
}
//rightdown
if(pos.first != 6){
swap(now[bin[pos.first - 1] + pos.second], now[bin[pos.first] + pos.second + 1]);
ull hashVal = toHash(now);
if(hashVal == desVal){flag = true, ans = cnt + 1; break;}
else if(cnt + 1 == 18) break;
if(insert(hashVal)) add(now, cnt + 1);
swap(now[bin[pos.first - 1] + pos.second], now[bin[pos.first] + pos.second + 1]);
}
que.pop();
}
if(flag) wr(ans), putchar('\n');
else printf("too difficult\n");
}
return 0;
}
迭代加深
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
namespace IO{
inline int read(){
int i = 0, f = 1; char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0' && ch <= '9'; ch = getchar()) i = (i << 3) + (i << 1) + (ch - '0');
return i * f;
}
inline void wr(ll x){
if(x < 0) putchar('-'), x = -x;
if(x > 9) wr(x / 10);
putchar(x % 10 + '0');
}
}using namespace IO;
const int N = 10, M = 30;
int T, ans;
int initMap[N][N];
bool flag;
typedef pair<int, int> P;
P pos;
const int des[N][N] = {{0}, {0, 0},{0,1,1},{0,2,2,2},{0,3,3,3,3},{0,4,4,4,4,4},{0,5,5,5,5,5,5}};
inline int H(){
int ret = -1;
for(int i = 1; i <= 6; i++)
for(int j = 1; j <= i; j++) if(initMap[i][j] ^ des[i][j]) ret++;
return ret;
}
inline bool dfs(int now, int dep, int x, int y){
if(now > dep){
if(H() == -1) return true;
else return false;
}
if(H() + now - 1 > dep) return false;
//leftUp
if(y != 1){
swap(initMap[x][y], initMap[x - 1][y - 1]);
if(dfs(now + 1, dep, x - 1, y - 1)) return true;
swap(initMap[x][y], initMap[x - 1][y - 1]);
}
//up
if(y != x){
swap(initMap[x][y], initMap[x - 1][y]);
if(dfs(now + 1, dep, x - 1, y)) return true;
swap(initMap[x][y], initMap[x - 1][y]);
}
//down
if(x != 6){
swap(initMap[x][y], initMap[x + 1][y]);
if(dfs(now + 1, dep, x + 1, y)) return true;
swap(initMap[x][y], initMap[x + 1][y]);
}
//rightdown
int tmp = 0;
for(int i = 1; i < x; i++) tmp += i;
if(x != 6){
swap(initMap[x][y], initMap[x + 1][y + 1]);
if(dfs(now + 1, dep, x + 1, y + 1)) return true;
swap(initMap[x][y], initMap[x + 1][y + 1]);
}
return false;
}
int main(){
T = read();
while(T--){
ans = 0, flag = false;
for(int i = 1; i <= 6; i++)
for(int j = 1; j <= i; j++) {
scanf("%d", &initMap[i][j]);
if(!initMap[i][j]) pos = P(i, j);
}
// cout<<pos.first<<" "<<pos.second<<endl;
for(int i = 0; i <= 20; i++)
if(dfs(1, i, pos.first, pos.second)){flag = true, ans = i;break;}
if(flag) wr(ans), putchar('\n');
else printf("too difficult\n");
}
return 0;
}
NOIP 模拟 玩积木 - 迭代加深搜索 / bfs+hash+玄学剪枝的更多相关文章
- HDU 1560 DNA sequence (IDA* 迭代加深 搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...
- 【bzoj1085】【 [SCOI2005]骑士精神】启发式剪枝+迭代加深搜索
(上不了p站我要死了,侵权度娘背锅) 如果这就是启发式搜索的话,那启发式搜索也不是什么高级玩意嘛..(啪啪打脸) Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且 ...
- POJ1129Channel Allocation[迭代加深搜索 四色定理]
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14601 Accepted: 74 ...
- BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1800 Solved: 984[Submit][Statu ...
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- 迭代加深搜索 codevs 2541 幂运算
codevs 2541 幂运算 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...
- UVA 529 - Addition Chains,迭代加深搜索+剪枝
Description An addition chain for n is an integer sequence with the following four properties: a0 = ...
- hdu 1560 DNA sequence(迭代加深搜索)
DNA sequence Time Limit : 15000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- 迭代加深搜索 C++解题报告 :[SCOI2005]骑士精神
题目 此题根据题目可知是迭代加深搜索. 首先应该枚举空格的位置,让空格像一个马一样移动. 但迭代加深搜索之后时间复杂度还是非常的高,根本过不了题. 感觉也想不出什么减枝,于是便要用到了乐观估计函数(O ...
随机推荐
- ORA-01665 control file is not a standby control file
ORA-01665错误处理 问题描述: 在备库启动至mount状态时,报如下错误: ORA-01665: control file is not a standby control file 解决办法 ...
- 11. Spring Boot JPA 连接数据库
转自:https://blog.csdn.net/catoop/article/details/50508397
- JS实现跑马灯效果(鼠标滑入可暂停,离开继续跑)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Java Lock Example – ReentrantLock(java锁的例子)
Welcome to Java Lock example tutorial. Usually when working with multi-threaded environment, we use ...
- [WASM] Write to WebAssembly Memory from JavaScript
We write a function that converts a string to lowercase in WebAssembly, demonstrating how to set the ...
- POJ 1745 Divisibility DP
POJ:http://poj.org/problem?id=1745 A完这题去买福鼎肉片,和舍友去买滴~舍友感慨"这一天可以卖好几百份,每份就算赚一块钱..那么一个月..一年...&quo ...
- 9、LCD驱动程序框架
linux-3.4.2\drivers\video\S3C2410fb.c(内核自带驱动程序) fbmem.c是LCD驱动程序顶层框架文件,是一个通用的文件,在初始化init函数中会注册一个字符设备, ...
- css3-11 如何改变背景图片的大小和位置
css3-11 如何改变背景图片的大小和位置 一.总结 一句话总结:css3相对css2本身就支持改变背景图片的大小和位置. 1.怎么设置背景不填充padding部分? background-orig ...
- MyCAT分表初体验
1.mycat二进制包安装 下载地址:http://dl.mycat.io/ tar -zxvf Mycat-server-1.6.5-release-20180122220033-linux.tar ...
- 【23.33%】【codeforces 557B】Pasha and Tea
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...