bzoj1054(广搜+hash??)
来源:互联网 发布:java replace第一个 编辑:程序博客网 时间:2024/04/29 01:08
1054: [HAOI2008]移动玩具
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 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
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>
usingnamespacestd;
intbg,ed,h,xx[4]={0,0,1,-1},yy[4]={1,-1,0,0};
boolans[5][5],mark[65536];
structdata{boola[5][5];intstep;}q[65536];
inthash(boola[5][5])
{
intk=1,s=0;
for(inti=1;i<=4;i++)
for(intj=1;j<=4;j++)
{s+=k*a[i][j];k<<=1;}
returns;
}
voidbfs()
{
intt=0,w=1;
charch[5];
for(inti=1;i<=4;i++)
{
scanf("%s",ch);
for(intj=1;j<=4;j++)
q[0].a[i][j]=ch[j-1]-'0';
}
for(inti=1;i<=4;i++)
{
scanf("%s",ch);
for(intj=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;
intx,y;
while(t<w)
{
for(inti=1;i<=4;i++)
for(intj=1;j<=4;j++)
if(q[t].a[i][j])
for(intk=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++;
}
}
intmain()
{
bfs();
return0;
}
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>
usingnamespacestd;
intbg,ed,h,xx[4]={0,0,1,-1},yy[4]={1,-1,0,0};
boolans[5][5],mark[65536];
structdata{boola[5][5];intstep;}q[65536];
inthash(boola[5][5])
{
intk=1,s=0;
for(inti=1;i<=4;i++)
for(intj=1;j<=4;j++)
{s+=k*a[i][j];k<<=1;}
returns;
}
voidbfs()
{
intt=0,w=1;
charch[5];
for(inti=1;i<=4;i++)
{
scanf("%s",ch);
for(intj=1;j<=4;j++)
q[0].a[i][j]=ch[j-1]-'0';
}
for(inti=1;i<=4;i++)
{
scanf("%s",ch);
for(intj=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;
intx,y;
while(t<w)
{
for(inti=1;i<=4;i++)
for(intj=1;j<=4;j++)
if(q[t].a[i][j])
for(intk=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++;
}
}
intmain()
{
bfs();
return0;
}
0 0
- bzoj1054(广搜+hash??)
- BZOJ1054 移动玩具 [BFS][HASH]
- BZOJ1054 移动玩具 BFS+hash
- 【BZOJ1054】[HAOI2008]移动玩具【BFS】【Hash】
- [BZOJ1054][HAOI2008]移动玩具(bfs+hash)
- [BZOJ1054][HAOI2008]移动玩具(bfs+hash)
- BZOJ1054
- pku 1077 Eight【八数码、广搜、hash判重】
- UVA 1533 - Moving Pegs (广搜+hash判重)
- POJ 1198 双广+Hash
- BZOJ1054(搜索)
- POJ 3131 八进制表示状态+dfs+双向广搜+邻接表hash 好题
- poj 3131 Cubic Eight-Puzzle 双向广搜 Hash判重
- hdu1043(八数码问题,广搜 + hash(实现状态压缩) )
- 总结: 广搜 + hash(哈希搜索) (has…
- hoj 1868 八数码(双广+hash)
- 广搜
- 广搜
- 进程和线程的解释
- Codeforces 629D Babaei and Birthday Cake(树状数组优化dp)
- 容器的选用
- Python pika.ConnectionParameters Examples
- 安卓系统底层C语言算法之测试参数是几个long型的算法
- bzoj1054(广搜+hash??)
- 关于安装Httpd Web服务器
- Oracle SQL性能优化
- awk 处理
- Label创建字体
- ListView的HeaderView与FooterView
- 18.UIWebView
- 删除字符串中字符数最少的字符
- HTTP中的Host字段