题目大意

给定国际象棋8x8棋盘上三个起始点,三个骑士分别从三个起始点开始移动(骑士只能走日字,且骑士从任意一点出发可以走遍整个棋盘)。现要求三个骑士汇聚到棋盘上某个点,且使得骑士到达该点所移动的次数总和最小。求该最小移动次数。 
题目连接:骑士问题

题目分析

典型的搜索,最短路径可以使用BFS。骑士数只有三个,因此可以求出每个骑士到达棋盘上所有点的移动的次数,再遍历一遍棋盘,求出最小次数和。

实现

#pragma once
#pragma execution_character_set("utf-8")
// 本文件为utf-8 编码格式
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<unordered_map>
#include<unordered_set>
#include<string>
#include<stack>
#include<queue>
using namespace std;
struct Node{
int x, y, step;
Node(int xx, int yy, int s) :x(xx), y(yy), step(s){};
};
int move_step[9][9][3];
bool visited[9][9];
int move_inc[8][2] = { { -2, -1 }, { -2, 1 }, { -1, 2 }, { 1, 2 }, { 2, 1 }, { 2, -1 }, { 1, -2 }, { -1, -2 } };
void Bfs(int start_x, int start_y, int knight){
memset(visited, false, sizeof(visited));
queue<Node> Q;
Node node(start_x, start_y, 0);
Q.push(node);
visited[start_x][start_y] = true;
while (!Q.empty()){
node = Q.front();
Q.pop();
move_step[node.x][node.y][knight] = node.step;
for (int i = 0; i < 8; i++){
int next_x = node.x + move_inc[i][0];
int next_y = node.y + move_inc[i][1];
if (next_x >= 1 && next_x <= 8 &&
next_y >= 1 && next_y <= 8 &&
!visited[next_x][next_y]){
visited[next_x][next_y] = true;
Q.push(Node(next_x, next_y, node.step + 1));
}
}
}
}
void Init(){
memset(move_step, -1, sizeof(move_step));
}
int MinStep(){
int min = 1 << 30;
for (int r = 1; r <= 8; r++){
for (int c = 1; c <= 8; c++){
min = min < (move_step[r][c][0] + move_step[r][c][1] + move_step[r][c][2]) ?
min : (move_step[r][c][0] + move_step[r][c][1] + move_step[r][c][2]);
}
}
return min;
}
int main(){
int T;
char row, col;
scanf("%d", &T);
int start_x[3];
int start_y[3]; while (T--){
for (int i = 0; i < 3; i++){
getchar();
scanf("%c%c", &row, &col);
start_x[i] = row - 'A' + 1;
start_y[i] = col - '0';
}
for (int i = 0; i < 3; i++){
Bfs(start_x[i], start_y[i], i);
}
int result = MinStep();
printf("%d\n", result);
}
return 0;
}

hiho_99_骑士问题的更多相关文章

  1. COGS746. [网络流24题] 骑士共存

    骑士共存问题«问题描述:在一个n*n个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘 上某些方格设置了障碍,骑士不得进入. «编程任务:对于给定的n*n个方格的国际象棋棋盘和障碍标志 ...

  2. 【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS

    [Usaco2005 Dec]Knights of Ni 骑士 Description  贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为 ...

  3. 骑士游历/knight tour - visual basic 解决

    在visual baisc 6 how to program 中文版第七章的练习题上看到了这个问题,骑士游历的问题. 在8x8的国际象棋的棋盘上,骑士(走法:一个方向走两格,另一个方向一格)不重复走完 ...

  4. BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1800  Solved: 984[Submit][Statu ...

  5. BZOJ 1040 【ZJOI2008】 骑士

    Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火 ...

  6. 【BZOJ-1040】骑士 树形DP + 环套树 + DFS

    1040: [ZJOI2008]骑士 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3312  Solved: 1269[Submit][Status ...

  7. BZOJ1040 [ZJOI2008]骑士

    Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各 界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战 ...

  8. BFS 骑士的移动

    骑士的移动 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83498#problem/E 题目: Description A f ...

  9. LA 3523 圆桌骑士

    题目链接:http://vjudge.net/contest/141787#problem/A http://poj.org/problem?id=2942 此题很经典 知识点:DFS染色,点-双连通 ...

随机推荐

  1. Animation在每一帧中的执行顺序测试

    测试代码: void Update() { transform.position = Vector3.zero; } void LateUpdate() { Debug.Log(transform.p ...

  2. Obj格式解析以及在Unity3D下导入测试

    目前基本实现了导入,注意只能打开含有单个模型的obj文件 四边面模型: 全三角面模型(测试单一材质,自动分了下UV): 这里介绍下obj格式: obj格式是waveFront推出的一种3D模型格式,可 ...

  3. C++的try_catch异常

    http://blog.sina.com.cn/s/blog_a9303fd901018ost.html 大部分内容转自:http://blog.csdn.net/codestinity/articl ...

  4. hiho一下,第115周,FF,EK,DINIC

    题目1 : 网络流一·Ford-Fulkerson算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho住在P市,P市是一个很大很大的城市,所以也面临着一个 ...

  5. CentOS 安装ftp

    Linux安装ftp组件 1 安装vsftpd组件 安装完后,有/etc/vsftpd/vsftpd.conf 文件,是vsftp的配置文件. [root@bogon ~]# yum -y insta ...

  6. 【MySQL】MySQL复制表结构、表数据

    平常,复制.备份表,一般都直接操作IDE完成.但有时,一些初始化数据的脚本,在操作数据前,最好备份下操作表的结构.数据,不至于出错了被置于为难的境地. 所以复制表结构.表数据的语句就派上用场. > ...

  7. duilib\utils\utils.h(251) : error C2504: “VARIANT”: 未定义基类

    转载:http://blog.csdn.net/SP_daiyq/article/details/44542939?locationNum=3 创建win32应用程序的工程文件,删除不需要的代码,只留 ...

  8. UPC 2170 D Equal Is Not Really Equal (欧拉路径)

    题目链接:http://acm.upc.edu.cn/problem.php?id=2170 题意:给出一个字符串,比如ABACA,在这个串里,AB.BA.AC.CA各出现一次.若存在另外一个串,里面 ...

  9. 《微信开发日志》之OAuth2验证接口

    OAuth2接口说明: 企业应用中的URL链接(包括自定义菜单或者消息中的链接),可以通过OAuth2.0验证接口来获取员工的身份信息. 通过此接口获取用户身份会有一定的时间开销.对于频繁获取用户身份 ...

  10. Mybatis+SpringMVC+Spring整合

    1,先添加spring支持: applicationContext.xml  配在WEBINF下,四个命名空间:aop,context,tx,p 配Listener:ContextLoaderList ...