hiho_99_骑士问题
题目大意
给定国际象棋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_骑士问题的更多相关文章
- COGS746. [网络流24题] 骑士共存
骑士共存问题«问题描述:在一个n*n个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘 上某些方格设置了障碍,骑士不得进入. «编程任务:对于给定的n*n个方格的国际象棋棋盘和障碍标志 ...
- 【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS
[Usaco2005 Dec]Knights of Ni 骑士 Description 贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为 ...
- 骑士游历/knight tour - visual basic 解决
在visual baisc 6 how to program 中文版第七章的练习题上看到了这个问题,骑士游历的问题. 在8x8的国际象棋的棋盘上,骑士(走法:一个方向走两格,另一个方向一格)不重复走完 ...
- BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1800 Solved: 984[Submit][Statu ...
- BZOJ 1040 【ZJOI2008】 骑士
Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火 ...
- 【BZOJ-1040】骑士 树形DP + 环套树 + DFS
1040: [ZJOI2008]骑士 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3312 Solved: 1269[Submit][Status ...
- BZOJ1040 [ZJOI2008]骑士
Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各 界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战 ...
- BFS 骑士的移动
骑士的移动 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83498#problem/E 题目: Description A f ...
- LA 3523 圆桌骑士
题目链接:http://vjudge.net/contest/141787#problem/A http://poj.org/problem?id=2942 此题很经典 知识点:DFS染色,点-双连通 ...
随机推荐
- C#常用日期格式处理转换[C#日期格式转换大全
DateTime dt = DateTime.Now; Label1.Text = dt.ToString();//2005-11-5 13:21:25 Label2.Text = dt.ToFile ...
- C#中八皇后问题的递归解法——N皇后
百度测试部2015年10月份的面试题之——八皇后. 八皇后问题的介绍在此.以下是用递归思想实现八皇后-N皇后. 代码如下: using System;using System.Collections. ...
- EasyUI扩展方法
EasyUI扩展方法: 1.我想指定textarea的行,但editor:{type:'textarea', options: {rows:'4'}}这样写不行.请问大家怎么配置才是指定行的啊? 配置 ...
- reactjs源码
'use strict'; var EventConstants = _dereq_(15);var EventPropagators = _dereq_(19);var ExecutionEnvir ...
- Json 入门例子【3】
Javascript 和Jquery 通过ID 获取值. <script> var txt1 = [{ "CityId": 18, "CityName&quo ...
- .Net操作.exe文件
Process proc = new Process(); proc.StartInfo.FileName = @"D:\Program Files\Foxmail\Foxmail.exe& ...
- Cheatsheet: 2013 09.10 ~ 09.21
.NET Lucene.Net – Custom Synonym Analyzer Using FiddlerCore to Capture Streaming Audio Immutable col ...
- Cheatsheet: 2013 07.21 ~ 07.31
Mobile Android vs. iOS: Comparing the Development Process of the GQueues Mobile Apps Android Studio ...
- DataTables warning : Requested unknown parameter '5' from the data source for row 0
在该项目中我使用了jquery.dataTables.js来作为我的前端数据表格. 表格的官网地址:https://www.datatables.net/ 一.jsp部分代码片段如下: <tab ...
- 再论EM算法的收敛性和K-Means的收敛性
标签(空格分隔): 机器学习 (最近被一波波的笔试+面试淹没了,但是在有两次面试时被问到了同一个问题:K-Means算法的收敛性.在网上查阅了很多资料,并没有看到很清晰的解释,所以希望可以从K-Mea ...