题目连接

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 移动玩具的更多相关文章

  1. [HAOI 2005][BZOJ 1054] 移动玩具

    先贴一波题面 1054: [HAOI2008]移动玩具 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2288  Solved: 1270 Descr ...

  2. Bzoj 1055: [HAOI2008]玩具取名 (区间DP)

    Bzoj 1055: [HAOI2008]玩具取名 (区间DP) 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1055 区间动态规划和可 ...

  3. bzoj 1054: [HAOI2008]移动玩具 bfs

    1054: [HAOI2008]移动玩具 Time Limit: 10 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Description 在 ...

  4. BZOJ 1054 [HAOI2008]移动玩具

    1054: [HAOI2008]移动玩具 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1388  Solved: 764[Submit][Statu ...

  5. 【BZOJ 1054】 [HAOI2008]移动玩具

    Description 在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩具,请你用最少的移动 ...

  6. BZOJ 1054: [HAOI2008]移动玩具(bfs)

    题面: https://www.lydsy.com/JudgeOnline/problem.php?id=1054 题解: 将每一种状态十六位压成二进制,然后bfs..不解释.. p.s.注意特判初始 ...

  7. [BZOJ 1054][HAOI 2008]移动玩具 状态压缩

    考试的时候一看是河南省选题,觉得会很难,有点不敢想正解.感觉是个状压.但是一看是十年前的题,那怂什么! 直接把十六个数的状态压进去,因为个数是不变的,所以状态枚举的时候只要找数目一样的转移即可.而且只 ...

  8. BZOJ 1054 题解

    1054: [HAOI2008]移动玩具 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1888  Solved: 1035[Submit][Stat ...

  9. BZOJ 1054 广搜

    1054: [HAOI2008]移动玩具 在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动 时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩 ...

随机推荐

  1. 【转】最实用的IT类网站及工具大集合

    转自:http://www.cnblogs.com/annie00/p/5753507.html 1.聚合数据 大家在开发过程中,可能会用到各种各样的数据,想找一些接口来提供一些数据.比如天气预报查询 ...

  2. 在.net中序列化读写xml方法

    收集XML的写法 XML是一种很常见的数据保存方式,我经常用它来保存一些数据,或者是一些配置参数. 使用C#,我们可以借助.net framework提供的很多API来读取或者创建修改这些XML, 然 ...

  3. mjrefresh源码分析

    最近想自己写个下拉刷新的库,但是始终感觉无从下手,想想总是容易的.原理也很简单,真正要下手写的时候,呵呵.不得不说ios封装得很好,网上可以用的成熟的库也很多,也正是因为如此很多开发者也忽略了很多底层 ...

  4. 企业内网信息安全实践-记ChinaUnix技术交流

    企业内网信息安全实践 随着棱镜计划的曝光,越来越多的信息安全的事件暴露在公众面前.对于企业来说,遭受到黑客攻击和破坏是家常便饭,只是您没有觉察到.自从09年就开始研究Ossim0.9版本,历经进10个 ...

  5. [AngularJS 1] Introduction to AngularJS

    introduction:this article is going to introduce AngularJS in generally. I will write it through five ...

  6. BG雪碧图制作要求

    使用软件 firework 保存png-8 然后参数如下图: 格式:PNG 8 调色板:最适合 深度:128 透明度:Alpha 透明度

  7. Jqgrid获取行id

    //获取选中行(单行)的ID var id = $("#table").jqGrid('getGridParam','selrow'); //根据id获取行数据,返回的是列表 va ...

  8. leetcode3:不重复的最长子串长度

    package hashmap; import java.util.HashMap; import java.util.Map; public class hashmap { public stati ...

  9. 移植u-boot-1.1.6之NOR的支持

    u-boot-1.1.6里面默认配置文件里面支持的nor型号是 #if 0 #define CONFIG_AMD_LV400 1 /* uncomment this if you have a LV4 ...

  10. Unable to write inside TEMP environment path

    安装PostgreSQL 9:Unable to write inside TEMP environment path 注册表:regedit HKEY_CLASSES_ROOT\.vbs,设置默认为 ...