题目大意

给定国际象棋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. wpf的研究和反思

    WPF的研究和反思 目前是否适合使用wpf      WPF(Windows Presentation Foundation)是微软推出的基于Windows Vista的用户界面框架,属于.NET F ...

  2. 2016年11月30日 星期三 --出埃及记 Exodus 20:21

    2016年11月30日 星期三 --出埃及记 Exodus 20:21 The people remained at a distance, while Moses approached the th ...

  3. [金阳光测试]IOS 自动化测试

    第一讲 一 自备能力 1.熟悉js脚本(必须) 2.会常用的linux命令(必须) 3.英文能力(必须) 4.熟悉java(必须) 5.会一点点shell和python 6.会一些oc和IOS开发 二 ...

  4. SaveXml的方法汇总

    /// <summary> /// 保存XML文件 /// </summary> /// <returns></returns> public bool ...

  5. Android 内部存储和外部存储

    应用程序的一些配置文件需要存储在手机上.一般分为内部存储和SD卡存储. 一. 内部存储 ,以 FileOutputStream File file = new File(getFilesDir(),& ...

  6. [Cocoa]深入浅出 Cocoa 之 Core Data(1)- 框架详解

    Core data 是 Cocoa 中处理数据,绑定数据的关键特性,其重要性不言而喻,但也比较复杂.Core Data 相关的类比较多,初学者往往不太容易弄懂.计划用三个教程来讲解这一部分: 框架详解 ...

  7. 通过NuGet获取sqlite对应的.net的dll

    https://www.nuget.org/packages/System.Data.SQLite/ 直接在Package Manager Console中执行命令,会自动安装依赖项的 Install ...

  8. request 获取各种路径

    从request获取各种路径总结 request.getRealPath("url"); // 虚拟目录映射为实际目录 request.getRealPath("./&q ...

  9. Linux红帽认证----I Want

    仅此设定一个目标,此证一定搞到手!!!

  10. So easy Webservice 3.使用HttpClient工具调用Webservice接口

    首先,看看webservice服务调用演示: a) 登录http://www.webxml.com.cn b) 单击手机查询服务 c) 选择要调用的方法 例如: getMobileCodeInfo 输 ...