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染色,点-双连通 ...
随机推荐
- Unix下五种IO模型
http://blog.chinaunix.net/uid-25324849-id-247813.html 1. I/O模型 Unix下共有五种I/O模型 a. 阻塞I/O b. 非阻塞I/O c. ...
- ThreadLocal深入理解二
转载:http://doc00.com/doc/101101jf6 今天在看之前转载的博客:ThreadLocal的内部实现原理.突然有个疑问, 按照threadLocal的原理, 当把一个对象存入到 ...
- Winform自定义分页控件的实现
实现效果 有点丑陋 但是功能是没问题的 测试过 实现思路 先创建一个用户控件 代码实现 public partial class PagerControl : UserControl { ; /// ...
- 操作SQLite的dbhelper
操作SQLite的dbhelper public class DbHelper { string connStr = @"Data Source=" + System.Enviro ...
- 巧用ViewPager 打造不一样的广告轮播切换效果
一.概述 如果大家关注了我的微信公众号的话,一定知道我在5月6号的时候推送了一篇文章,文章名为Android超高仿QQ附近的人搜索展示(一),通过该文可以利用ViewPager实现单页显示多个Item ...
- D3D 模板缓存的创建过程
下面是我对模板缓存创建的理解: 1. 模板缓存是和深度缓存一起被创建的,将深度缓存的一部分作为模板缓存使用. 深度缓存和模板缓存是在Direct3D初始化时创建的,D3DPRESENT_PARAMET ...
- 通用的linux下安装配置svn独立服务
参考资料: http://www.blogjava.net/zhouf/articles/251476.html http://www.cnblogs.com/thinksasa/archive/20 ...
- android导入项目出现style错误,menu错误
android导入项目出现style错误,menu错误 style //查看 res/values/styles.xml 下的报错点. <style name="AppBaseThem ...
- 第六课 SQLite
总结:SQLite 1.SQLite的数据类型: NULL(空值).INTEGER(整型值).READL(浮点值).TEXT(字符串值).BLOB(二进制对象); 2.SQLite的应用 2.1 SQ ...
- 【leetcode❤python】171. Excel Sheet Column Number
#-*- coding: UTF-8 -*- # ord(c) -> integer##Return the integer ordinal of a one-character string. ...