首页 技术 正文
技术 2022年11月18日
0 收藏 781 点赞 3,735 浏览 1439 个字

1054: [HAOI2008]移动玩具

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

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=2e5+,M=1e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+;
struct is
{
char a[][];
int step;
};
char mp[][];
char ans[][];
map<string,int>m;
string check(char mp[][])
{
string a="";
for(int i=;i<;i++)
for(int t=;t<;t++)
a+=mp[i][t];
return a;
}
queue<is>q;
int ff(int x,int y)
{
if(x<||x>=||y<||y>=)
return ;
return ;
}
int xx[]={,,,-};
int yy[]={,,-,};
int main()
{
for(int i=;i<;i++)
scanf("%s",mp[i]);
for(int i=;i<;i++)
scanf("%s",ans[i]);
m[check(mp)]=;
is f;
for(int i=;i<;i++)
for(int t=;t<;t++)
f.a[i][t]=mp[i][t];
f.step=;
q.push(f);
int out;
while(!q.empty())
{
is b=q.front();
q.pop();
int flag=;
for(int i=;i<;i++)
{
for(int t=;t<;t++)
if(b.a[i][t]!=ans[i][t])
{
flag=;
break;
}
}
if(flag)
{
out=b.step;
break;
}
for(int i=;i<;i++)
{
for(int t=;t<;t++)
if(b.a[i][t]=='')
{
for(int j=;j<;j++)
{
int xxx=i+xx[j];
int yyy=t+yy[j];
//cout<<xxx<<" "<<yyy<<" "<<ff(xxx,yyy)<<endl;
if(ff(xxx,yyy)&&b.a[xxx][yyy]=='')
{
b.a[xxx][yyy]='';
b.a[i][t]='';
b.step++;
if(m[check(b.a)]==)
q.push(b),m[check(b.a)]=;
b.step--;
b.a[xxx][yyy]='';
b.a[i][t]='';
}
}
}
}
}
printf("%d\n",out);
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,087
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,562
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,821
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905