题目链接

题意

中文题意。

思路

首先找到空白的格子,因为空白的格子可以和其他的骑士换。从空白的点开始搜索,每次和其他点交换。因为最多只有十五步,可以做16次搜索,搜索的时候,记录走过的步数和至少剩余的步数(还剩下多少个骑士不在原本的位置),这样剪枝。当check到所有的骑士都在合法位置的时候,就可以确定答案了。

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;
typedef long long LL;
int init[6][6] = {
{1, 1, 1, 1, 1},
{0, 1, 1, 1, 1},
{0, 0, 2, 1, 1},
{0, 0, 0, 0, 1},
{0, 0, 0, 0, 0}
};
char mm[7][7];
int mp[7][7], ans;
int dx[] = {2, 2, -2, -2, 1, -1, 1, -1};
int dy[] = {-1, 1, -1, 1, 2, 2, -2, -2}; int check() {
int cnt = 0;
for(int i = 0; i < 5; i++)
for(int j = 0; j < 5; j++)
if(mp[i][j] != init[i][j]) cnt++;
return cnt;
} void A_Star(int x, int y, int g, int now) {
if(g == now) {
if(check() == 0) ans = now;
return ;
}
if(~ans) return ;
for(int i = 0; i < 8; i++) {
int nx = x + dx[i], ny = y + dy[i];
if(nx < 0 || nx > 4 || ny < 0 || ny > 4) continue;
swap(mp[nx][ny], mp[x][y]);
if(check() + g <= now) A_Star(nx, ny, g + 1, now);
swap(mp[nx][ny], mp[x][y]);
}
} int main() {
int t; scanf("%d", &t);
while(t--) {
for(int i = 0; i < 5; i++) scanf("%s", mm[i]);
int ix, iy;
for(int i = 0; i < 5; i++) for(int j = 0; j < 5; j++) {
mp[i][j] = mm[i][j] - '0';
if(mm[i][j] == '*') mp[i][j] = 2, ix = i, iy = j;
}
ans = -1;
for(int i = 0; i <= 15; i++) {
A_Star(ix, iy, 0, i);
if(~ans) break;
}
printf("%d\n", ans);
}
return 0;
}

最新文章

  1. strsep和strtok_r替代strtok
  2. SDK Manager 中 没有 Support Library怎么弄?
  3. Mysql控制语句
  4. JAVA基础知识之JDBC——使用ResultSetMetaData分析结果集
  5. HDU1166-敌兵布阵(线段树)
  6. memcache简易教程
  7. jQuery1.8以上,ajaxSend,ajaxStart等一系列事件要绑定在document上才有效果
  8. Redis以及Redis的php扩展安装无错版
  9. uva:10340 - All in All(字符串匹配)
  10. Collections你用对了吗?
  11. mysql 创建表字段类型笔记
  12. 约会安排HDU - 4553
  13. 54.1 怎样才算学会django? 知道这28个知识点才算会django2
  14. 解决SVN提交和更新代码冲突?
  15. Drools+springboot
  16. OpenCV入门之寻找图像的凸包(convex hull)
  17. Java 基础 IO流
  18. linux sysfs文件系统
  19. 七、Spring Boot 启动配置原理
  20. React DevTools

热门文章

  1. WPF与缓动(三) 指数缓动
  2. glibc内存管理方式
  3. jQuery省市联动
  4. WPF中DataGrid自定义实现最后一行下面跟一个汇总行,类似MT4
  5. github page的两种类型
  6. ELINK离线编程器版本说明
  7. Windows下用VC与QT编译MPI程序入门
  8. winpcap在VS2012 Qt5 X64下的配置
  9. 测试链接服务器sql 语句
  10. Python基础(六) 函数