【HAOI 2008】 移动玩具
2024-10-01 08:41:54
【题目链接】
https://www.lydsy.com/JudgeOnline/problem.php?id=1054
【算法】
广度优先搜索
【代码】
#include<bits/stdc++.h>
using namespace std;
const int dx[] = {,,-,};
const int dy[] = {-,,,};
const int MAXS = << ; struct info
{
int mp[][];
int step;
} ; int i,j,k,goal,tx,ty,l,r,state;
int mp[][],g[][];
info q[MAXS];
info cur,tmp;
bool visited[MAXS]; inline bool valid(int x,int y)
{
return x > && x <= && y > && y <= ;
}
inline int get(int a[][])
{
int i,j,ret = ;
int b = ;
for (i = ; i <= ; i++)
{
for (j = ; j <= ; j++)
{
ret += a[i][j] * b;
b <<= ;
}
}
return ret;
} int main()
{ for (i = ; i <= ; i++)
{
for (j = ; j <= ; j++)
{
mp[i][j] = getchar() - '';
}
getchar();
}
getchar();
for (i = ; i <= ; i++)
{
for (j = ; j <= ; j++)
{
g[i][j] = getchar() - '';
}
getchar();
}
goal = get(g);
visited[get(mp)] = true;
if (visited[goal])
{
printf("0\n");
return ;
}
l = r = ;
memcpy(q[].mp,mp,sizeof(q[].mp));
q[].step = ;
while (l <= r)
{
cur = q[l];
l++;
for (i = ; i <= ; i++)
{
for (j = ; j <= ; j++)
{
if (cur.mp[i][j] == )
{
for (k = ; k < ; k++)
{
tx = i + dx[k];
ty = j + dy[k];
if (valid(tx,ty) && cur.mp[tx][ty] == )
{
swap(cur.mp[i][j],cur.mp[tx][ty]);
state = get(cur.mp);
if (!visited[state])
{
visited[state] = true;
if (state == goal)
{
printf("%d\n",cur.step+);
return ;
}
r++;
memcpy(q[r].mp,cur.mp,sizeof(q[r].mp));
q[r].step = cur.step + ;
}
swap(cur.mp[i][j],cur.mp[tx][ty]);
}
}
}
}
}
} return ; }
最新文章
- HTML input=";file"; 浏览时只显示指定文件类型 xls、xlsx、csv
- ccc pool
- 谷歌、百度、1万ip能赚多少钱?1000IP能够值多少钱呢?
- 编写简单的Mapreduce程序并部署在Hadoop2.2.0上运行
- How do you render tooltip on disabled HTML Button
- EqualsBuilder和HashCodeBuilder(重写equal和hashcode)
- C#_串口程序_二次打包_事件响应
- 求绝对值,hdu-2003
- Java变量参数
- LibVLC audio controls
- JS自动刷新页面一次
- bootstrap思考一
- MPI编程——分块矩阵乘法(cannon算法)
- vertica系列:解锁table
- matlab二维绘图学习摘要
- Python 调试器之pdb
- IDEA集成git和使用步骤
- spoj 7258 SUBLEX(求第k大字串
- Android系统常用的adb命令
- WPF 学习笔记-设置属性使窗口不可改变大小