zoukankan      html  css  js  c++  java
  • BZOJ 1054 题解

    1054: [HAOI2008]移动玩具

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 1888  Solved: 1035
    [Submit][Status][Discuss]

    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

    Solution

    裸广搜,将状态用二进制数表示即可。

      1 /**************************************************************
      2     Problem: 1054
      3     User: shadowland
      4     Language: C++
      5     Result: Accepted
      6     Time:108 ms
      7     Memory:16152 kb
      8 ****************************************************************/
      9  
     10 #include "bits/stdc++.h"
     11   
     12 using namespace std;  
     13 struct Matrix { int M[ 6 ][ 6 ] , num ; } ; 
     14 const int maxN = 100100 ;
     15 const int INF = 2147483647 ;
     16 typedef long long QAQ ;
     17  
     18 Matrix Goal , Q[ maxN ] ; 
     19 int Find_Table[ maxN ] ;
     20 bool vis[ 10 ][ 10 ] ;
     21 int dx[ 10 ] = { -1 , 0 , 1 , 0 } , dy[ 10 ] = { 0 , 1 , 0 , -1 } ;
     22  
     23 QAQ ans , final ;
     24   
     25 int Condense ( Matrix tmp ) {
     26         int ret = 0 ;
     27         for( int i=1 ; i<=4 ; ++i )
     28                 for( int j=1 ; j<=4 ; ++j )
     29                         ret = ( ret << 1 ) + tmp.M[ i ][ j ] ;
     30         return ret ;
     31 }
     32  
     33 bool Judge( Matrix SHHHS ) {
     34         int tmp = Condense( SHHHS ) ;
     35         if ( tmp == ans ){ final = SHHHS.num ; return true ; }
     36         bool key = true ;
     37         if ( Find_Table[ tmp ] == true ) key = false ;
     38          
     39         if ( key == true ) Find_Table[ tmp ] = true ;
     40         if ( key == true ) return true ;
     41         else               return false ;
     42 }
     43  
     44 void BFS ( ) {
     45         Find_Table[ Condense( Q[ 1 ] ) ] = true ;
     46         int head = 1 , tail = 1 ;
     47         while( head <= tail ) {
     48                 Matrix t1 = Q[ head ] ;
     49                 for ( int i=1 ; i<=4 ; ++i ) {
     50                         for ( int j=1 ; j<=4 ; ++j ) {
     51                                 if ( t1.M[ i ][ j ] == 0 ) {
     52                                         for ( int xi=0 ; xi<4 ; ++xi ) {
     53                                                 int xx = i + dx[ xi ] ;
     54                                                 int yy = j + dy[ xi ] ;
     55                                                 if( vis[ xx ][ yy ] ){
     56                                                         Matrix t2 = t1 ;
     57                                                         ++ t2.num ;
     58                                                         t2.M[ xx ][ yy ] = t1.M[ i ][ j ] ;
     59                                                         t2.M[ i ][ j ] = t1.M[ xx ][ yy ] ;
     60                                                         if( Judge( t2 ) ) Q[ ++ tail ] = t2 ;
     61                                                         if( final ) return ;
     62                                                 }
     63                                         }
     64                                 }
     65                         }
     66                 }
     67                 ++ head ;
     68         }
     69         return;
     70 }
     71   
     72 int main( ) {
     73          
     74         memset ( vis , true , sizeof ( vis ) ) ;
     75  
     76         for ( int i=1 ; i<=4 ; ++i ) {
     77                 for ( int j=1 ; j<=4 ; ++j ) {
     78                         char ch = getchar ( ) ;
     79                         Q[ 1 ].M[ i ][ j ] = ch - '0' ;
     80                 }
     81                 getchar ( ) ;
     82         }
     83      
     84         getchar ( ) ;
     85      
     86         for ( int i=1 ; i<=4 ; ++i ){
     87                 for ( int j=1 ; j<=4 ; ++j ){
     88                         char ch = getchar ( ) ;
     89                         Goal.M[ i ][ j ] = ch - '0' ;
     90                 }
     91                 getchar ( ) ;
     92         }
     93          
     94         ans = Condense( Goal );
     95         if ( ans == Condense ( Q[ 1 ] ) ) { cout << '0' << endl ; goto End ;}
     96         BFS ( ) ;
     97         printf( "%lld
    " , final );
     98         End :
     99         return 0;  
    100 }
    View Code

    2016-10-14 23:41:11

    ()

  • 相关阅读:
    html JS 打开本地程序及文件
    [转]jquery mobile中redirect重定向问题
    kafka中broker、producer、consumer主要配置参数说明
    手写一个LruCache
    ES发布时max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144] 错误
    UML类图
    JVM调优总结
    JVM学习系列(五) 常见的JVM参数
    JVM学习系列(四) 相关概念
    JVM学习系列(三) 虚拟机监控相关
  • 原文地址:https://www.cnblogs.com/shadowland/p/5962525.html
Copyright © 2011-2022 走看看