bzoj1054(广搜+hash??)

1 篇文章 0 订阅

1054: [HAOI2008]移动玩具

Time Limit: 10 Sec   Memory Limit: 162 MB
Submit: 1643   Solved: 897
[ 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

解题思路:hash判重,这么小就想到了二进制。


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std ;
int bg , ed , h , xx [ 4 ] = { 0 , 0 , 1 , - 1 } , yy [ 4 ] = { 1 , - 1 , 0 , 0 } ;
bool ans [ 5 ] [ 5 ] , mark [ 65536 ] ;
struct data { bool a [ 5 ] [ 5 ] ; int step ; } q [ 65536 ] ;
int hash ( bool a [ 5 ] [ 5 ] )
{
int k = 1 , s = 0 ;
for ( int i = 1 ; i <= 4 ; i ++ )
   for ( int j = 1 ; j <= 4 ; j ++ )
       { s += k * a [ i ] [ j ] ; k << = 1 ; }
     return s ;
}
void bfs ( )
{
int t = 0 , w = 1 ;
char ch [ 5 ] ;
for ( int i = 1 ; i <= 4 ; i ++ )
{
scanf ( "%s" , ch ) ;
for ( int j = 1 ; j <= 4 ; j ++ )
   q [ 0 ] . a [ i ] [ j ] = ch [ j - 1 ] - '0' ;
     }   
     for ( int i = 1 ; i <= 4 ; i ++ )
{
scanf ( "%s" , ch ) ;
for ( int j = 1 ; j <= 4 ; j ++ )
   ans [ i ] [ j ] = ch [ j - 1 ] - '0' ;
     }
     bg = hash ( q [ t ] . a ) ;
     ed = hash ( ans ) ;
     if ( bg == ed ) { printf ( "0" ) ; return ; }
     mark [ bg ] = 1 ;
     int x , y ;
while ( t < w )
{
for ( int i = 1 ; i <= 4 ; i ++ )
           for ( int j = 1 ; j <= 4 ; j ++ )
               if ( q [ t ] . a [ i ] [ j ] )
               for ( int k = 0 ; k < 4 ; k ++ )
               {
                  x = i + xx [ k ] , y = j + yy [ k ] ;
                  if ( q [ t ] . a [ x ] [ y ] || x > 4 || y > 4 || x < 1 || y < 1 ) continue ;
                  swap ( q [ t ] . a [ i ] [ j ] , q [ t ] . a [ x ] [ y ] ) ;
                  h = hash ( q [ t ] . a ) ;
                  if ( ! mark [ h ] ) {
                     if ( h == ed ) { printf ( "%d" , q [ t ] . step + 1 ) ; return ; }
mark [ h ] = 1 ;
memcpy ( q [ w ] . a , q [ t ] . a , sizeof ( q [ w ] . a ) ) ;
q [ w ] . step = q [ t ] . step + 1 ;
w ++ ;
}
               swap ( q [ t ] . a [ i ] [ j ] , q [ t ] . a [ x ] [ y ] ) ;
               }
         t ++ ;
}
}
int main ( )
{
     bfs ( ) ;
return 0 ;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std ;
int bg , ed , h , xx [ 4 ] = { 0 , 0 , 1 , - 1 } , yy [ 4 ] = { 1 , - 1 , 0 , 0 } ;
bool ans [ 5 ] [ 5 ] , mark [ 65536 ] ;
struct data { bool a [ 5 ] [ 5 ] ; int step ; } q [ 65536 ] ;
int hash ( bool a [ 5 ] [ 5 ] )
{
int k = 1 , s = 0 ;
for ( int i = 1 ; i <= 4 ; i ++ )
   for ( int j = 1 ; j <= 4 ; j ++ )
       { s += k * a [ i ] [ j ] ; k << = 1 ; }
     return s ;
}
void bfs ( )
{
int t = 0 , w = 1 ;
char ch [ 5 ] ;
for ( int i = 1 ; i <= 4 ; i ++ )
{
scanf ( "%s" , ch ) ;
for ( int j = 1 ; j <= 4 ; j ++ )
   q [ 0 ] . a [ i ] [ j ] = ch [ j - 1 ] - '0' ;
     }   
     for ( int i = 1 ; i <= 4 ; i ++ )
{
scanf ( "%s" , ch ) ;
for ( int j = 1 ; j <= 4 ; j ++ )
   ans [ i ] [ j ] = ch [ j - 1 ] - '0' ;
     }
     bg = hash ( q [ t ] . a ) ;
     ed = hash ( ans ) ;
     if ( bg == ed ) { printf ( "0" ) ; return ; }
     mark [ bg ] = 1 ;
     int x , y ;
while ( t < w )
{
for ( int i = 1 ; i <= 4 ; i ++ )
           for ( int j = 1 ; j <= 4 ; j ++ )
               if ( q [ t ] . a [ i ] [ j ] )
               for ( int k = 0 ; k < 4 ; k ++ )
               {
                  x = i + xx [ k ] , y = j + yy [ k ] ;
                  if ( q [ t ] . a [ x ] [ y ] || x > 4 || y > 4 || x < 1 || y < 1 ) continue ;
                  swap ( q [ t ] . a [ i ] [ j ] , q [ t ] . a [ x ] [ y ] ) ;
                  h = hash ( q [ t ] . a ) ;
                  if ( ! mark [ h ] ) {
                     if ( h == ed ) { printf ( "%d" , q [ t ] . step + 1 ) ; return ; }
mark [ h ] = 1 ;
memcpy ( q [ w ] . a , q [ t ] . a , sizeof ( q [ w ] . a ) ) ;
q [ w ] . step = q [ t ] . step + 1 ;
w ++ ;
}
               swap ( q [ t ] . a [ i ] [ j ] , q [ t ] . a [ x ] [ y ] ) ;
               }
         t ++ ;
}
}
int main ( )
{
     bfs ( ) ;
return 0 ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值