bzoj1054: [HAOI2008]移动玩具 状压+爆搜即可
题意:在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩具,请你用最少的移动次数将初的玩具状态移动到某人心中的目标状态。
题解:状压状态,然后bfs时枚举每一位向四个方向转移即可,记得加vis,表示已经访问过的状态
/**************************************************************
Problem: 1054
User: walfy
Language: C++
Result: Accepted
Time:28 ms
Memory:6176 kb
****************************************************************/
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0)
using namespace std;
const double eps=1e-6;
const int N=1000000+10,maxn=1000000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f;
bool vis[N];
int dis[N];
bool left(int u,int i)
{
if(i!=0&&i!=4&&i!=8&&i!=12&&(!((u>>(i-1))&1)))return 1;
return 0;
}
bool right(int u,int i)
{
if(i!=3&&i!=7&&i!=11&&i!=15&&(!((u>>(i+1))&1)))return 1;
return 0;
}
bool up(int u,int i)
{
if(i!=0&&i!=1&&i!=2&&i!=3&&(!((u>>(i-4))&1)))return 1;
return 0;
}
bool down(int u,int i)
{
if(i!=12&&i!=13&&i!=14&&i!=15&&(!((u>>(i+4))&1)))return 1;
return 0;
}
void bfs(int x,int y)
{
vis[x]=1;
queue<int>q;
q.push(x);
while(!q.empty())
{
int u=q.front();
if(u==y)
{
printf("%d\n",dis[u]);
return ;
}
q.pop();
for(int i=0;i<16;i++)
{
if((u>>i)&1)
{
if(left(u,i))
{
int te=u^(1<<i)^(1<<(i-1));
if(!vis[te])
{
q.push(te);
dis[te]=dis[u]+1;
vis[te]=1;
}
}
if(right(u,i))
{
int te=u^(1<<i)^(1<<(i+1));
if(!vis[te])
{
q.push(te);
dis[te]=dis[u]+1;
vis[te]=1;
}
}
if(up(u,i))
{
int te=u^(1<<i)^(1<<(i-4));
if(!vis[te])
{
q.push(te);
dis[te]=dis[u]+1;
vis[te]=1;
}
}
if(down(u,i))
{
int te=u^(1<<i)^(1<<(i+4));
if(!vis[te])
{
q.push(te);
dis[te]=dis[u]+1;
vis[te]=1;
}
}
}
}
}
}
char s[5];
int main()
{
int x=0,y=0;
for(int i=0;i<4;i++)
{
scanf("%s",s);
for(int j=0;j<4;j++)
if(s[j]=='1')
x+=(1<<(i*4+j));
}
for(int i=0;i<4;i++)
{
scanf("%s",s);
for(int j=0;j<4;j++)
if(s[j]=='1')
y+=(1<<(i*4+j));
}
bfs(x,y);
return 0;
}
/********************
********************/
bzoj1054: [HAOI2008]移动玩具 状压+爆搜即可的更多相关文章
- [HAOI2008]移动玩具 状压
发现自己只会打状压了. 233333 不需要考虑是否会被挡,所以直接dp #include<cstdio> #include<cstring> #include<iost ...
- 【Luogu】P4363一双木棋(状压爆搜)
题目链接 唉,只有AC了这道题才会感叹考场上没有想出解法的我是多么智障. 我甚至连任何想法都没有. 天啊我当时到底在想些什么. AC这道题我就能进前15了诶. 我们发现只要确定了轮廓线那么此时的状态就 ...
- bzoj1054: [HAOI2008]移动玩具
hash+bfs:要注意特殊情况.(似乎连sort.lower_bound都不用数据小直接判重了... #include<cstdio> #include<cstring> # ...
- [BZOJ1054][HAOI2008]移动玩具 bfs+hash
1054: [HAOI2008]移动玩具 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2432 Solved: 1355[Submit][Stat ...
- 【BFS】bzoj1054 [HAOI2008]移动玩具
暴搜吧,可以哈希一下,但是懒得写哈希了,所以慢得要死. Code: #include<cstdio> #include<queue> #include<set> # ...
- 【洛谷P4289】移动玩具 状压bfs
代码如下 #include <bits/stdc++.h> using namespace std; const int dx[]={0,0,1,-1}; const int dy[]={ ...
- UVA 818 Cutting Chains(状压 + 暴搜)题解
题意:有1~n个小环,他们中的有些互相扣在一起,问你至少切开几个能把这写小环串成一条链 思路:还是太菜了,题目给的n<=15,显然可以暴力解决. 用二进制表示每个环切还是不切,然后搜索所有情况. ...
- CF1042B 【Vitamins】(去重,状压搜索)
由题意,我们其实会发现 对于每一种果汁,其对应的状态只有可能有7种 VA VB VC VA+VB VA+VC VB+VC VA+VB+VC 这道题就大大简化了 我们把所有果汁都读进来 每种 ...
- hdu4536-XCOM Enemy Unknown(爆搜)
XCOM-Enemy Unknown是一款很好玩很经典的策略游戏. 在游戏中,由于未知的敌人--外星人入侵,你团结了世界各大国家进行抵抗.随着游戏进展,会有很多的外星人进攻事件.每次进攻外星人会选择3 ...
随机推荐
- DetailView内匿名函数不可用
DetailView yii\widgets\DetailView 小部件显示的是单一 yii\widgets\DetailView::$model 数据的详情. 它非常适合用常规格式显示一个模型(例 ...
- Python开发【模块】:re正则
re模块 序言: re模块用于对python的正则表达式的操作 '.' 默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行 '^' 匹配字符开头,若指定flags ...
- 使用celery之了解celery(转)
原文 http://www.dongwm.com/archives/shi-yong-celeryzhi-liao-jie-celery/ 前言 我想很多做开发和运维的都会涉及一件事:cront ...
- linux 安装libevent
今天再ubuntu下安装libevent,下载源码 tar -xzvf libevent-1.4.15.tar.gz cd libevent-1.4.15 ./configure make make ...
- 001-ant design安装及快速入门【基于纯antd的基本项目搭建】
一.安装使用 1.1.安装 推荐使用 npm 或 yarn 的方式进行开发 npm install antd --save yarn add antd 1.2.浏览器引入 在浏览器中使用 script ...
- 国内比特币bitcoin交易平台
火币网: www.huobi.com 比特币中国: www.btcchina.com okcoin: www.okcoin.cn 中国比特币: www.chbtc.com 比特币交易 ...
- in 和 or 的效率问题
select * from table where col in (2,3,4,5,6) select * from table where col=2 or col=3 or col=4 or co ...
- PAT 1060 Are They Equal[难][科学记数法]
1060 Are They Equal(25 分) If a machine can save only 3 significant digits, the float numbers 12300 a ...
- PAT 1110 Complete Binary Tree[判断完全二叉树]
1110 Complete Binary Tree(25 分) Given a tree, you are supposed to tell if it is a complete binary tr ...
- Winform 下使用WebBrowser的HTML编辑控件—WinHtmlControl 在win7 IE9下的问题
问题是这样的,有一个需要用到富文本的地方,由于是winform的程序,而且程序是上一代老员工留下的,错误百出,现在要尽量修复,至少保证能正常使用,于是就开始一点点问题修复. 在win7 64位系统下出 ...