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个(可以数一数) 这样的话,我们可以进行一个状压,把 ...
随机推荐
- 在线预览Office文件【效果类似百度文库】(转载)
转载地址:http://www.cnblogs.com/sword-successful/p/4031823.html 引言 结合上个项目和目前做的这个项目,其中都用到了Office文件在线预览,目前 ...
- 玩玩Excel下的Power View
作为微软平台下的数据展示工具,Power View是一个不错的选择.而在Excel 2013下,即使你没有SharePoint的实例那么你也可以玩转它.此篇讲对Excel 2013下的Power Vi ...
- 难得的中文ASP.NET 5/MVC 6入门教程
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:由于ASP.NET 5还未正式发布,即使是官方文档都还不完善,更不要说系统的中文文档了 ...
- 遍历List过程中删除元素的正确做法(转)
遍历List过程中删除元素的正确做法 public class ListRemoveTest { 3 public static void main(String[] args) { 4 ...
- waterMarkTextBox
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...
- LeetCode——Single Number(找出数组中只出现一次的数)
问题: Given an array of integers, every element appears twice except for one. Find that single one. No ...
- 通信原理实践(一)——音频信号处理
一.信号的离散化 1.采样定理: –如果信号是带限的,并且采样频率fs超过信号最高频率的两倍,那么,原来的连续信号可以从采样样本中完全重建出来. 因此在仿真过程中,采样率(fs)是一个非常重要的参数. ...
- Linux学习笔记(21) Linux日志管理
1. 简介 (1) 日志服务 在CentOS 6.x中日志服务已经由rsyslogd取代了原先的syslogd服务.rsyslogd日志服务更加先进,功能更多.但是不论该服务的使用,还是日志文件的格式 ...
- 多态、类库、委托、is和as运算符、泛型集合
多态:简而言之就是龙生九子,各有不同 有了继承,才有了多态 1.虚方法 virtual 重写 override 父类中的方法,在子类中并不适用,那么子类需要自主更改继承的方法或者是属性,那父类中加了v ...
- 近实时运算的利器---presto在公司实践
1.起因 公司hadoop集群里的datanonde和tasktracker节点负载主要集中于晚上到凌晨,平日工作时间负载不是很高.但在工作时间内,公司业务人员有实时查询需求,现在主要 借助于hive ...