bzoj 1054 移动玩具
题目连接
http://www.lydsy.com/JudgeOnline/problem.php?id=1054
移动玩具
Description
在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩具,请你用最少的移动次数将初始的玩具状态移动到某人心中的目标状态。
Input
前4行表示玩具的初始状态,每行4个数字1或0,1表示方格中放置了玩具,0表示没有放置玩具。接着是一个空行。接下来4行表示玩具的目标状态,每行4个数字1或0,意义同上。
Output
一个整数,所需要的最少移动次数。
Sample Input
1111
0000
1110
0010
1010
0101
1010
0101
Sample Output
4
数据很小直接暴搜。。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
using std::min;
using std::sort;
using std::pair;
using std::swap;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 1000000;
const int INF = 0x3f3f3f3f;
bool vis[N];
struct Node {
int s;
bool mat[4][4];
}que[N], end;
const int dx[] = { 0, 0, -1, 1 }, dy[] = { -1, 1, 0, 0 };
inline int hash(Node &x) {
int ret = 0, k = 1;
rep(i, 4) {
rep(j, 4) {
ret += k * x.mat[i][j];
k <<= 1;
}
}
return (ret + N) % N;
}
inline void read(Node &x) {
char buf[10];
rep(i, 4) {
scanf("%s", buf);
rep(j, 4) {
x.mat[i][j] = buf[j] - '0';
}
}
}
void bfs() {
int lb = 0, ub = 0, ret, ans;
ret = hash(que[0]), ans = hash(end);
if(ret == ans) { puts("0"); return; }
ub++, cls(vis, false), vis[ret] = true;
while(lb != ub) {
Node &x = que[lb++];
ret = hash(x);
if(ret == ans) { printf("%d\n", x.s); return; }
rep(i, 4) {
rep(j, 4) {
if(!x.mat[i][j]) continue;
rep(k, 4) {
int nx = dx[k] + i, ny = dy[k] + j;
if(nx < 0 || nx >= 4 || ny < 0 || ny >= 4) continue;
if(x.mat[nx][ny]) continue;
Node &t = que[ub];
t = x, t.s = x.s + 1;
swap(t.mat[i][j], t.mat[nx][ny]);
ret = hash(t);
if(vis[ret]) continue;
vis[ret] = true;
ub++;
}
}
}
}
puts("0");
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
read(que[0]), read(end);
bfs();
return 0;
}
bzoj 1054 移动玩具的更多相关文章
- [HAOI 2005][BZOJ 1054] 移动玩具
先贴一波题面 1054: [HAOI2008]移动玩具 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2288 Solved: 1270 Descr ...
- Bzoj 1055: [HAOI2008]玩具取名 (区间DP)
Bzoj 1055: [HAOI2008]玩具取名 (区间DP) 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1055 区间动态规划和可 ...
- 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 ...
- 【BZOJ 1054】 [HAOI2008]移动玩具
Description 在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩具,请你用最少的移动 ...
- BZOJ 1054: [HAOI2008]移动玩具(bfs)
题面: https://www.lydsy.com/JudgeOnline/problem.php?id=1054 题解: 将每一种状态十六位压成二进制,然后bfs..不解释.. p.s.注意特判初始 ...
- [BZOJ 1054][HAOI 2008]移动玩具 状态压缩
考试的时候一看是河南省选题,觉得会很难,有点不敢想正解.感觉是个状压.但是一看是十年前的题,那怂什么! 直接把十六个数的状态压进去,因为个数是不变的,所以状态枚举的时候只要找数目一样的转移即可.而且只 ...
- BZOJ 1054 题解
1054: [HAOI2008]移动玩具 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1888 Solved: 1035[Submit][Stat ...
- BZOJ 1054 广搜
1054: [HAOI2008]移动玩具 在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动 时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩 ...
随机推荐
- Flex4/Flash开发在线音乐播放器 , 含演示地址
要求 必备知识 本文要求基本了解 Adobe Flex编程知识和JAVA基础知识. 开发环境 MyEclipse10/Flash Builder4.6/Flash Player11及以上 演示地址 演 ...
- vss error reading from file 解决方法
vss error reading from file 解决方法 1 若服务器中存在 vss/data/backup目录,请将该目录删掉2 运行cmd cd.. cd C:\Program Files ...
- CENTOS7设置显示中文
vi /etc/locale.conf LANG="zh_CN.UTF-8" LANGUAGE="zh_CN.GB18030:zh_CN.GB2312:zh_CN&quo ...
- TCP/IP详解学习笔记(6)-- IP选路
1.概述 路由算法是用于获取路由表中的路由项目.它是路由选择协议的核心. 2.路由算法的分类 从路由算法能否随网络的通信量或拓扑自适应的进行调整变化来分,可以分为两类. 静态路由选 ...
- Centos linux php扩展安装步骤
使用phpinfo()函数输出PHP信息,然后找到Configuration File (php.ini) apachectl 其设计意图是帮助管理员控制Apache httpd后台守护进程的功能. ...
- NULL值比较,两个列的合并,列值按条件替换。
show create table 表名 -- 显示创建表的sql语句. 为已有的表增加新列.alter table 表名 add 列名 int NULL -- 此行加了一个int 类型 默认可以nu ...
- java排序练习
public void testSort3(){ List<String> str=new ArrayList<String>(); String st="12345 ...
- iptable原理
http://www.cnblogs.com/littlehann/p/3708222.html http://seanlook.com/2014/02/23/iptables-understand/ ...
- HTTP协议请求方式: 中GET、POST和HEAD的介绍以及错误提示码
HTTP协议中GET.POST和HEAD的介绍 2008-05-10 14:15 GET: 请求指定的页面信息,并返回实体主体. HEAD: 只请求页面的首部. POST: 请求服务器接受所指定的文档 ...
- win7发送到菜单
在文件浏览器地址栏输入 shell:sendto