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个(可以数一数) 这样的话,我们可以进行一个状压,把 ...
随机推荐
- Shell编程基础教程5--文本过滤、正则表达式、相关命令
5.文本过滤.正则表达式.相关命令 5.1.正则表达式(什么是正则表达式?正则表达式怎么进行匹配?常用命令) 简介: 一种用来描述文本模式的特殊语法 ...
- 【JAVA集合框架之工具类】
一.概述 JAVA集合框架中有两个很重要的工具类,一个是Collections,另一个是Arrays.分别封装了对集合的操作方法和对数组的操作方法,这些操作方法使得程序员的开发更加高效. public ...
- Async/Await 最佳实践
其实好久以前就看过这个文章,以及类似的很多篇文章.最近在和一个新同事的交流中发现原来对async的死锁理解不是很透彻,正好最近时间比较充裕就再当一回搬运工. 本文假定你对.NET Framework ...
- 开心网的账号登录及api操作
.kaixin.php <?php /** * PHP Library for kaixin001.com * * @author */ class kaixinPHP { function _ ...
- 如何通过阅读C标准来解决C语言语法问题
有时候必须非常专注地阅读ANSI C标准才能找到某个问题的答案.一位销售工程师把下面这段代码作为测试用例发给Sun的编译小组. foo(const char **p) {} int main(int ...
- 计算第K个素数
暂时没有时间整理,先放在这里: http://www.quora.com/Prime-Numbers/What-are-good-ways-to-find-nth-prime-number-in-th ...
- vmware虚拟机检测
jpg改rar
- SQLServer 维护脚本分享(09)相关文件读取
/********************[读取跟踪文件(trc)]********************/ --查看事件类型描述 SELECT tc.name,te.trace_event_id, ...
- jQuery事件和JavaScript事件
1.JavaScript事件: 属性 当以下情况发生时,出现此事件 FF N IE onabort 图像加载被中断 1 3 4 onblur 元素失去焦点 1 2 3 onchange 用户改变域的内 ...
- AngularJS html+DOM+ng-click事件
ng-disabled 指令直接绑定应用程序数据到 HTML 的 disabled 属性. ng-show 指令用于设置应用部分是否可见. ng-show="true" 设置 HT ...