【题目链接】

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 ; }

最新文章

  1. HTML input=&quot;file&quot; 浏览时只显示指定文件类型 xls、xlsx、csv
  2. ccc pool
  3. 谷歌、百度、1万ip能赚多少钱?1000IP能够值多少钱呢?
  4. 编写简单的Mapreduce程序并部署在Hadoop2.2.0上运行
  5. How do you render tooltip on disabled HTML Button
  6. EqualsBuilder和HashCodeBuilder(重写equal和hashcode)
  7. C#_串口程序_二次打包_事件响应
  8. 求绝对值,hdu-2003
  9. Java变量参数
  10. LibVLC audio controls
  11. JS自动刷新页面一次
  12. bootstrap思考一
  13. MPI编程——分块矩阵乘法(cannon算法)
  14. vertica系列:解锁table
  15. matlab二维绘图学习摘要
  16. Python 调试器之pdb
  17. IDEA集成git和使用步骤
  18. spoj 7258 SUBLEX(求第k大字串
  19. Android系统常用的adb命令
  20. WPF 学习笔记-设置属性使窗口不可改变大小

热门文章

  1. HTTPS 为什么更安全,先看这些
  2. ES6 arrow function
  3. Beta冲刺-星期三
  4. 查看 Android App 的 versionCode
  5. 【SQL】IN、EXISTS和表连接三者的效率比较
  6. JavaScript图片轮播,举一反三
  7. 删除git上已经提交的文件
  8. 模拟人的手指在UI上滑动时3D模型跟随着移动(Unity)
  9. Let's Encrypt,免费好用的 HTTPS 证书
  10. 利用vue-gird-layout 制作可定制桌面 (一)