BZOJ 1054 题解
1054: [HAOI2008]移动玩具
Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 1888 Solved: 1035
[Submit][Status][Discuss]
Description
Input
Output
一个整数,所需要的最少移动次数。
Sample Input
0000
1110
0010
1010
0101
1010
0101
Sample Output
Solution
裸广搜,将状态用二进制数表示即可。
/**************************************************************
Problem: 1054
User: shadowland
Language: C++
Result: Accepted
Time:108 ms
Memory:16152 kb
****************************************************************/ #include "bits/stdc++.h" using namespace std;
struct Matrix { int M[ ][ ] , num ; } ;
const int maxN = ;
const int INF = ;
typedef long long QAQ ; Matrix Goal , Q[ maxN ] ;
int Find_Table[ maxN ] ;
bool vis[ ][ ] ;
int dx[ ] = { - , , , } , dy[ ] = { , , , - } ; QAQ ans , final ; int Condense ( Matrix tmp ) {
int ret = ;
for( int i= ; i<= ; ++i )
for( int j= ; j<= ; ++j )
ret = ( ret << ) + tmp.M[ i ][ j ] ;
return ret ;
} bool Judge( Matrix SHHHS ) {
int tmp = Condense( SHHHS ) ;
if ( tmp == ans ){ final = SHHHS.num ; return true ; }
bool key = true ;
if ( Find_Table[ tmp ] == true ) key = false ; if ( key == true ) Find_Table[ tmp ] = true ;
if ( key == true ) return true ;
else return false ;
} void BFS ( ) {
Find_Table[ Condense( Q[ ] ) ] = true ;
int head = , tail = ;
while( head <= tail ) {
Matrix t1 = Q[ head ] ;
for ( int i= ; i<= ; ++i ) {
for ( int j= ; j<= ; ++j ) {
if ( t1.M[ i ][ j ] == ) {
for ( int xi= ; xi< ; ++xi ) {
int xx = i + dx[ xi ] ;
int yy = j + dy[ xi ] ;
if( vis[ xx ][ yy ] ){
Matrix t2 = t1 ;
++ t2.num ;
t2.M[ xx ][ yy ] = t1.M[ i ][ j ] ;
t2.M[ i ][ j ] = t1.M[ xx ][ yy ] ;
if( Judge( t2 ) ) Q[ ++ tail ] = t2 ;
if( final ) return ;
}
}
}
}
}
++ head ;
}
return;
} int main( ) { memset ( vis , true , sizeof ( vis ) ) ; for ( int i= ; i<= ; ++i ) {
for ( int j= ; j<= ; ++j ) {
char ch = getchar ( ) ;
Q[ ].M[ i ][ j ] = ch - '' ;
}
getchar ( ) ;
} getchar ( ) ; for ( int i= ; i<= ; ++i ){
for ( int j= ; j<= ; ++j ){
char ch = getchar ( ) ;
Goal.M[ i ][ j ] = ch - '' ;
}
getchar ( ) ;
} ans = Condense( Goal );
if ( ans == Condense ( Q[ ] ) ) { cout << '' << endl ; goto End ;}
BFS ( ) ;
printf( "%lld\n" , final );
End :
return ;
}
2016-10-14 23:41:11
(完)
BZOJ 1054 题解的更多相关文章
- BZOJ 1054: [HAOI2008]移动玩具(bfs)
题面: https://www.lydsy.com/JudgeOnline/problem.php?id=1054 题解: 将每一种状态十六位压成二进制,然后bfs..不解释.. p.s.注意特判初始 ...
- BZOJ 1054 [HAOI2008]移动玩具
1054: [HAOI2008]移动玩具 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1388 Solved: 764[Submit][Statu ...
- BZOJ 3732 题解
3732: Network Description 给你N个点的无向图 (1 <= N <= 15,000),记为:1…N. 图中有M条边 (1 <= M <= 30,000) ...
- BZOJ 1054 广搜
1054: [HAOI2008]移动玩具 在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动 时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩 ...
- bzoj 1054: [HAOI2008]移动玩具 bfs
1054: [HAOI2008]移动玩具 Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Description 在 ...
- bzoj 1054 移动玩具
题目连接 http://www.lydsy.com/JudgeOnline/problem.php?id=1054 移动玩具 Description 在一个4*4的方框内摆放了若干个相同的玩具,某人想 ...
- 浙大pat 1054 题解
1054. The Dominant Color (20) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard Behind the scen ...
- [HAOI 2005][BZOJ 1054] 移动玩具
先贴一波题面 1054: [HAOI2008]移动玩具 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2288 Solved: 1270 Descr ...
- bzoj 2669 题解(状压dp+搜索+容斥原理)
这题太难了...看了30篇题解才整明白到底咋回事... 核心思想:状压dp+搜索+容斥 首先我们分析一下,对于一个4*7的棋盘,低点的个数至多只有8个(可以数一数) 这样的话,我们可以进行一个状压,把 ...
随机推荐
- Notice: Only variable references should be returned by reference(PHP版本兼容性问题)
摘自:http://sushener.spaces.live.com/blog/cns!BB54050A5CFAFCDD!435.entry PHP5一个很让人恼火的一点就是BC(向后兼容)不是很理想 ...
- 几年前做家教写的C教程(之三专讲了递归和斐波那契)
C语言学习宝典(3) 数组: 一维数组的定义: 类型说明符 数组名[常量表达式] 例如: int a[10]; 说明:(1)数组名的命名规则和变量名相同,遵循标示符命名规则 (2)在定义数组时需要 ...
- poj 2236:Wireless Network(并查集,提高题)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 16065 Accepted: 677 ...
- AngularJS——依赖注入
依赖注入 依赖注入(DI)是一个经典的设计模式, 主要是用来处理组件如何获得依赖的问题.关于DI,推荐阅读Martin Flower的文章(http://martinfowler.com/art ...
- [Tips] JavaScript 使用hash 对象传参
转自Web 前端开发修炼之道. 在JavaScript 中funciton 包含多个参数的时候,我们想要实现可选参数的功能,传很多个null 其实是个很讨厌的事情,这个时候就可以使用这个技巧. 具体见 ...
- [v9] 列表页 调用 正文内容 或 自定义 字段(moreinfo的调用方法)
"才能使用的字段) id content readpoint groupids_view paginationtype maxcharperpage template paytype all ...
- 5.linux内核模块基础,内核模块学习
linux内核模块基础 一.定义 Linux 内核的整体结构非常庞大,其包含的组件也非常多,如何使用这些组件呢: 方法 1:把所有的组件都编译进内核文件,即:zImage 或 bzImage,但这样会 ...
- MOS X 下Apache服务器配置,及日志读取
A01-配置Apache 在当前用户的目录创建一个文件夹 打开finder进入/etc/apache2/etc/apache2 是系统目录,默认不显示 进入该目录有两种方法 i. 显示所有隐藏和系统目 ...
- KMP模式匹配算法
KMP模式匹配算法 相信很多人对于这个还有点不了解,或者说是不懂,下面,通过一道题,来解决软考中的这个问题! 正题: aaabaaa,其next函数值为多少? 对于这个问题,我们应该怎么做呢? 1.整 ...
- 【java基础】面向过程~面向对象
相信大家都知道这两个东西,可是大家是如何知道的呢?我们又该如何区分这个东西到底是面向过程还是面向对象的呢? 那,我们首先就要知道什么是面向过程,什么是面向对象: 面向过程"(Procedur ...