bzoj1054: [HAOI2008]移动玩具
hash+bfs;要注意特殊情况。(似乎连sort。lower_bound都不用数据小直接判重了。。。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define rep(i,n) for(int i=1;i<=n;i++)
#define clr(x,c) memset(x,c,sizeof(x))
#define REP(i,s,t) for(int i=s;i<=t;i++)
struct node{
int a[5][5];int dist;
};
node A,B,q[65540];
char tmp[5];
int xx[5]={0,0,0,1,-1};
int yy[5]={0,1,-1,0,0};
bool vis[65540];
int get(node x){
int k=1,ans=0;
rep(i,4) rep(j,4) {
if(x.a[i][j]) ans+=k;
k<<=1;
}
return ans;
}
void init(){
rep(i,4){
scanf("%s",tmp);
rep(j,4) A.a[i][j]=tmp[j-1]-'0';
}
rep(i,4){
scanf("%s",tmp);
rep(j,4) B.a[i][j]=tmp[j-1]-'0';
B.dist=0;
}
}
void work(){
clr(vis,false);
int s=get(A),t=get(B);
if(s==t){
printf("0\n");return ;
}
int l=1,r=1;q[1]=B;
while(l<=r){
node x=q[l];
rep(i,4) rep(j,4) if(x.a[i][j]) rep(k,4){
int tx=i+xx[k],ty=j+yy[k];
if(!tx||!ty||tx>4||ty>4||x.a[tx][ty]) continue;
swap(x.a[i][j],x.a[tx][ty]);
x.dist++;
int temp=get(x);
if(!vis[temp]){
if(temp==s) {
printf("%d\n",x.dist);
return ;
}
vis[temp]=1;q[++r]=x;
}
swap(x.a[i][j],x.a[tx][ty]);x.dist--;
}
l++;
}
}
int main(){
init();work();return 0;
}
1054: [HAOI2008]移动玩具
Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 1764 Solved: 972
[Submit][Status][Discuss]
Description
Input
Output
一个整数,所需要的最少移动次数。
Sample Input
0000
1110
0010
1010
0101
1010
0101
Sample Output
HINT
Source
bzoj1054: [HAOI2008]移动玩具的更多相关文章
- [BZOJ1054][HAOI2008]移动玩具 bfs+hash
1054: [HAOI2008]移动玩具 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2432 Solved: 1355[Submit][Stat ...
- bzoj1054: [HAOI2008]移动玩具 状压+爆搜即可
题意:在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩具,请你用最少的移动次数将初的玩具状态 ...
- 【BFS】bzoj1054 [HAOI2008]移动玩具
暴搜吧,可以哈希一下,但是懒得写哈希了,所以慢得要死. Code: #include<cstdio> #include<queue> #include<set> # ...
- 【BZOJ1054】[HAOI2008]移动玩具
[BZOJ1054][HAOI2008]移动玩具 题面 bzoj 洛谷 题解 太\(sb\)了,不想写了,直接点开洛谷题面单击右边蓝色按钮题解即可
- 【BZOJ1054】[HAOI2008]移动玩具 BFS
[BZOJ1054][HAOI2008]移动玩具 Description 在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动 时只能将玩具向上下左右四个 ...
- bzoj 1054: [HAOI2008]移动玩具 bfs
1054: [HAOI2008]移动玩具 Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Description 在 ...
- BZOJ 1054 [HAOI2008]移动玩具
1054: [HAOI2008]移动玩具 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1388 Solved: 764[Submit][Statu ...
- 1054: [HAOI2008]移动玩具
1054: [HAOI2008]移动玩具 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1272 Solved: 690[Submit][Statu ...
- P4289 [HAOI2008]移动玩具(bfs)
P4289 [HAOI2008]移动玩具 双向bfs+状态压缩+记忆化搜索 双向bfs用于对bfs的优化,每次找到可扩展节点少的一边进行一次bfs,找到的第一个互相接触的点即为最短路径 矩阵范围仅4* ...
随机推荐
- sql2008 计划自动创建数据库分区【转】
本文转自:http://jingyan.baidu.com/article/6b97984d9a26ec1ca3b0bf77.html sql2008 计划自动创建数据库分区 固定增量的数据,自动创建 ...
- css3 怎么实现像书籍装订线的效果
.test{ width: 150px; height: 150px; margin: 20% auto; background-color: white; background-image: rep ...
- PHP之XML节点追加操作讲解
问题: 最近研究了xml,我想知道,如何用php向已有的xml文档中添加新的节点,比如,xml文档内容如下,并保存为information.xml: <?xml version="1. ...
- Building Plugins for iOS
This page describes Native Code Plugins for the iOS platform. Building an Application with a Native ...
- fedora下缺少autopoint包的解决办法
编译过程中,报错,缺少autopoint包 然而无论是yum install autopoint 还是yum search autopoint都没有理想的答案 执行yum install gettex ...
- 使用异步 I/O 大大提高应用程序的性能
使用异步 I/O 大大提高应用程序的性能 学习何时以及如何使用 POSIX AIO API Linux® 中最常用的输入/输出(I/O)模型是同步 I/O.在这个模型中,当请求发出之后,应用程序就会阻 ...
- C#中 ? 和?? 的用法
C#中 ?? 和? 的意思 1.? 如果直接定义一个 值类型,给负值null:就会提示“无法将 Null转换成‘值类型(比如:int)’,因为他是一种不可为null的值 de类型” 例如 int in ...
- Ubuntu环境下nutch2.2.1集成HBase0.94.25
nutch2.2.1集成HBase0.94.25 (详见:http://duguyiren3476.iteye.com/blog/2085973 ) 1. 修改nutch的hbase配置 //将自己的 ...
- Happy Number
https://leetcode.com/problems/happy-number/ Write an algorithm to determine if a number is "hap ...
- 【转】Wireshark:“There are no interfaces on which a capture can be done ”
linux环境下 两种解决方案: 第一种方法:使用root用户登陆 xiaoshancun@xiaoshancun-VM500:~$ sudo wireshark 第二种方法 ...