【题目链接】

https://www.lydsy.com/JudgeOnline/problem.php?id=1085

【算法】

IDA*

【代码】

#include<bits/stdc++.h>
using namespace std;
const int dx[] = {-,-,-,-,,,,};
const int dy[] = {-,,-,,-,,-,};
const int goal[][] =
{
{,,,,},
{,,,,},
{,,,,},
{,,,,},
{,,,,}
}; int i,j,T,x,y,step;
char c;
bool solved;
int mp[][]; inline bool valid(int x,int y)
{
return x >= && x < && y >= && y < ;
}
inline int f()
{
int i,j;
int ret = ;
for (i = ; i < ; i++)
{
for (j = ; j < ; j++)
{
if (mp[i][j] != goal[i][j])
ret++;
}
}
return ret - ;
} inline bool IDDFS(int dep,int x,int y)
{
int i,tx,ty;
if (dep + f() > step) return false;
if (dep == step) return true;
for (i = ; i < ; i++)
{
tx = x + dx[i];
ty = y + dy[i];
if (valid(tx,ty))
{
swap(mp[x][y],mp[tx][ty]);
if (IDDFS(dep+,tx,ty)) return true;
swap(mp[x][y],mp[tx][ty]);
}
}
return false;
} int main()
{ scanf("%d",&T);
getchar();
while (T--)
{
solved = false;
for (i = ; i < ; i++)
{
for (j = ; j < ; j++)
{
c = getchar();
if (c == '') mp[i][j] = ;
else if (c == '') mp[i][j] = ;
else
{
x = i;
y = j;
mp[i][j] = ;
}
}
getchar();
}
for (i = ; i <= ; i++)
{
step = i;
if (IDDFS(,x,y))
{
printf("%d\n",i);
solved = true;
break;
}
}
if (!solved) printf("-1\n");
} return ; }

最新文章

  1. PHP之compact()函数
  2. 大型网站的 HTTPS 实践(1):HTTPS 协议和原理
  3. fstab 中 通过UUID挂载 参数解释
  4. phpDocumentor 注释语法详解
  5. ModernUI教程:使用预定义的页面布局
  6. BZOJ4002 [JLOI2015]有意义的字符串
  7. IDEA中利用JUnit进行单元测试
  8. C# string转int
  9. Session深入理解
  10. ListView OnScrollListener详解(滑屏分页显示数据)
  11. JS函数定义与匿名函数的调用
  12. Beta冲刺NO.5
  13. Android深入四大组件(九)Content Provider的启动过程
  14. JavaScript学习笔记之数组(二)
  15. Java生成生成密码类
  16. [UE4]宏
  17. Python 3.x标准模块库目录
  18. MinGW 是什么
  19. 使用MSBUILD 构建时出错 error MSB3086: 任务未能使用 SdkToolsPath“”或注册表项“XXX”找到“LC.exe”,请确保已设置 SdkToolsPath。
  20. vue父组件为子组件传值传不过去?vue为数组传值,不能直接用等于的方式,要用循环加push的方式

热门文章

  1. jquery中的left和top
  2. Leetcode0037--Sudoku Solver 数独游戏
  3. Mac下php连接mysql数据库失败解决办法
  4. eclipse中导入maven项目:org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.proje
  5. C 预处理程序指令(CPP)
  6. BZOJ 4817: [Sdoi2017]树点涂色 LCT+Access的性质+DFS序+线段树
  7. S-HR快速查看shr日志
  8. HTTP 状态码之:301、302 重定向
  9. 【习题 4-9 UVA - 815】Flooded!
  10. JavaSE 学习笔记之Java概述(一)