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个(可以数一数) 这样的话,我们可以进行一个状压,把 ...
随机推荐
- Dubbo应用与异常记录
结合项目里使用暴露出的问题,对并发较多的核心业务或者对请求失败等敏感的业务场景不太建议使用Dubbo, 如电商的购买等行为,使用Dubbo就必须阅读源码,熟悉相关机制,或者直接自己造轮子. >& ...
- WCF批量打开服务
WCF服务.利用循环,读取配置文件,打开所有的代理服务 和关闭代理服务的方法 //list列表 ,用于存储打开的服务列表 List<ServiceHost> _host = new Lis ...
- PHP_Memcache函数详解
memcache函数所有的方法列表如下: Memcache::add – 添加一个值,如果已经存在,则返回false Memcache::addServer – 添加一个可供使用的服务器地址 Memc ...
- phpcms v9网站搬家更换域名的方法
PHPCMS 是国内领先的网站管理系统,同时也是一个开源的PHP开发框架. 本文介绍phpcms v9网站搬家更换域名的方法. 1.在新的主机空间把phpcms安装好. 新安装的版本一定要和准备搬迁的 ...
- ARM指令学习,王明学learn
ARM指令学习 一.算数和逻辑指令 1— MOV 数据传送指令 2.— MVN 数据取反传送指令 3.— CMP 比较指令 4.— CMN 反值比较指令 5.— TST 位测试 ...
- ASP.NET 4.0 取消表单危险字符验证
/// <summary> /// ASP.NET4.0 表单验证类 /// </summary> public class FormRequestValidation : R ...
- JAVA安装,环境变量配置
JAVA环境变量设置 PATH %JAVA_HOME%\bin JAVA_HOME D:\ProgramFiles\Java\jdk1.6.0_10 CLASSPATH .;%JAVA_HOME%\l ...
- 用js实现图片自动加载的瀑布流效果
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Confluent Platform 3.0支持使用Kafka Streams实现实时的数据处理(最新版已经是3.1了,支持kafka0.10了)
来自 Confluent 的 Confluent Platform 3.0 消息系统支持使用 Kafka Streams 实现实时的数据处理,这家公司也是在背后支撑 Apache Kafka 消息框架 ...
- 安装Maven、Eclipse设置、添加地址JAR
1.下载Maven 地址:http://maven.apache.org/download.cgi 2.安装Maven 系统变量:MAVEN_HOME = D:\maven\apache-maven- ...