POJ 2046 Gap 搜索- 状态压缩
题目地址: http://poj.org/problem?id=2046
一道搜索状态压缩的题目,关键是怎样hash。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std; typedef long long LL;
const int N=2222222;
const LL II=1000000007;
const int M=1000007; struct xh
{
int maps[4][8],step;
}w,e,vv[500000]; int aim[4][8]=//目标状态
{
11,12,13,14,15,16,17,0,
21,22,23,24,25,26,27,0,
31,32,33,34,35,36,37,0,
41,42,43,44,45,46,47,0
}; int s[80];
int vis[M]; int gethash(int t[][8])
{
int i,j,k=0,hash=0;
for(i=0;i<4;i++)
for(j=0;j<8;j++)
{
s[k++]=t[i][j]/10;
s[k++]=t[i][j]%10;
//转化成数字,hash
}
for(i=0;i<k;i++)//hash
{
hash=hash*7+s[i];
}
hash=(hash&0x7fffffff)%M;
return hash;
} void chang(int num,int i,int j)
{
int a,b;
for(a=0;a<4;a++)
for(b=1;b<8;b++)
{
if(w.maps[a][b]==num)
{
swap(w.maps[a][b],w.maps[i][j]);
return ;
}
}
} bool test(xh a)
{
int i,j;
for(i=0;i<4;i++)
for(j=0;j<8;j++)
if(a.maps[i][j]!=aim[i][j])
return false;
return true;
} void bfs()
{
int i,j,hash,head=0,tail=0;
memset(vis,false,sizeof(vis));
w.maps[0][0]=11;
w.maps[1][0]=21;
w.maps[2][0]=31;
w.maps[3][0]=41;
w.step=0;
vv[tail++]=w;
hash=gethash(w.maps);
vis[hash]=true;
while(head!=tail)
{
e=vv[head++];
if(test(e))
{
printf("%d\n",e.step);
return ;
}
for(i=0;i<4;i++)
for(j=1;j<8;j++)
{
w=e;
if(w.maps[i][j]!=0) continue;
int num=w.maps[i][j-1];//前一个
if(num==0||num==17||num==27||num==37||num==47) continue;
chang(num+1,i,j);
hash=gethash(w.maps);
if(!vis[hash])
{
vis[hash]=true;
w.step++;
vv[tail++]=w;
}
}
}
printf("-1\n");
} int main()
{
int i,j,T,k;
cin>>T;
while(T--)
{
memset(w.maps,0,sizeof(w.maps));
for(i=0;i<4;i++)
for(j=1;j<8;j++)
{
scanf("%d",&k);
w.maps[i][j]=k;
if(k==11||k==21||k==31||k==41)
w.maps[i][j]=0;
}
bfs();
}
return 0;
}
POJ 2046 Gap 搜索- 状态压缩的更多相关文章
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 3691 (AC自动机+状态压缩DP)
题目链接: http://poj.org/problem?id=3691 题目大意:给定N个致病DNA片段以及一个最终DNA片段.问最终DNA片段最少修改多少个字符,使得不包含任一致病DNA. 解题 ...
- [POJ 2923] Relocation (动态规划 状态压缩)
题目链接:http://poj.org/problem?id=2923 题目的大概意思是,有两辆车a和b,a车的最大承重为A,b车的最大承重为B.有n个家具需要从一个地方搬运到另一个地方,两辆车同时开 ...
- poj 2046 Gap
题目连接 http://poj.org/problem?id=2046 Gap Description Let's play a card game called Gap. You have 28 c ...
- POJ 2923 Relocation (状态压缩,01背包)
题意:有n个(n<=10)物品,两辆车,装载量为c1和c2,每次两辆车可以运一些物品,一起走.但每辆车物品的总重量不能超过该车的容量.问最少要几次运完. 思路:由于n较小,可以用状态压缩来求解. ...
- POJ 1321 棋盘问题(状态压缩DP)
不总结的话, 同一个地方会 WA 到死 思路: 状态压缩 DP. 1. s 表示压缩状态, 若第 i 列放了棋子, 那么该列置 1, 否则该列置 0. 假如 s = 3(0x011) 那么表示棋盘的第 ...
- POJ 3254 Corn Fields 状态压缩
这题对我真的非常难.实在做不出来,就去百度了,搜到了一种状压DP的方法.这是第一种 详细见凝视 #include <cstdio> #include <cstring> #in ...
- POJ 3254 Corn Fields 状态压缩DP (C++/Java)
id=3254">http://poj.org/problem? id=3254 题目大意: 一个农民有n行m列的地方,每一个格子用1代表能够种草地,而0不能够.放牛仅仅能在有草地的. ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
随机推荐
- vuejs学习——vue+vuex+vue-router项目搭建(一)
前言 快年底了却有新公司邀请了我,所以打算把上家公司的学到一下技术做一些总结和分享. 现在vuejs都2.0了,我相信也有很多朋友和我一样实际项目还是选择vue1.0的或者给新手一些参考,不管在选择哪 ...
- html禁止浏览器默认行为,让页面更像应用。
在html或body行内写入:oncontextmenu="return false" ondragstart='return false;' onselectstart=&quo ...
- ASN.1笔记——语法规则与类型概述
转载:https://blog.csdn.net/sever2012/article/details/7672699 一.简介 ASN.1(Abstract Syntax Notation doton ...
- BNUOJ 52503 Disdain Chain
暴力,结论. 本打算写一发暴力,然后直接交答案,之后发现无论$n$等于多少,每种图都存在长度为$n$的路径,中间还一直以为自己暴力写错了. #include<bits/stdc++.h> ...
- Mybatis 源码分析之一二级缓存
一级缓存 其实关于 Mybatis 的一级缓存是比较抽象的,并没有什么特别的配置,都是在代码中体现出来的. 当调用 Configuration 的 newExecutor 方法来创建 executor ...
- Java常用工具类之压缩解压
package com.wazn.learn.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream ...
- springMVC整合freemarker遇到的问题 maven
java.lang.IllegalAccessError: tried to access method freemarker.ext.servlet.AllHttpScopesHashModel.& ...
- Am335x u-boot 代码大概流程
在_面之前的流程和u-boot-spl一样,区别在于_main中. 对于u-boot 2016.03来说 ENTRY(_main) /* * Set up initial C runtime envi ...
- 外行人都能看懂的SpringCloud
一.前言 只有光头才能变强 认识我的朋友可能都知道我这阵子去实习啦,去的公司说是用SpringCloud(但我觉得使用的力度并不大啊~~)... 所以,这篇主要来讲讲SpringCloud的一些基础的 ...
- shell 遍历
for file in $1/* do if [ -f $file ] then SUFFIX=${file#*BK} PREFIX=${SUFFIX%%_*} CURRENT=`date -d -7 ...