- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有一个无限的棋盘,
从控制台中,我们输入将有多少个示例,棋盘上有多少个国际象棋骑士,他们的起始位置(即,他们的位置),以及骑士必须经过最少的步骤才能到达的点。
它应该是这样的:
2
-1
-国际象棋骑士5 5
-起点5 6
-终点2
-国际象棋骑士0 0
-第一个骑士1 0
-第二个骑士0 1
-第一个骑士1 1
-第二个骑士3
-第一个示例4
-第二个示例#include <iostream>
#include <set>
#include <queue>
#include <climits>
using namespace std;
#define N 8
// Below arrays details all 8 possible movements
// for a knight
int row[] = { 2, 2, -2, -2, 1, 1, -1, -1 };
int col[] = { -1, 1, 1, -1, 2, -2, 2, -2 };
// Check if (x, y) is valid chess board coordinates
// Note that a knight cannot go out of the chessboard
bool valid(int x, int y)
{
if (x < 0 || y < 0 || x >= N || y >= N)
return false;
return true;
}
// queue node used in BFS
struct Node
{
// (x, y) represents chess board coordinates
// dist represent its minimum distance from the source
int x, y, dist;
// Node constructor
Node(int x, int y, int dist = 0): x(x), y(y), dist(dist) {}
// As we are using struct as a key in a std::set,
// we need to overload < operator
// Alternatively we can use std::pair<int, int> as a key
// to store coordinates of the matrix in the set
bool operator<(const Node& o) const
{
return x < o.x || (x == o.x && y < o.y);
}
};
// Find minimum number of steps taken by the knight
// from source to reach destination using BFS
int BFS(Node src, Node dest)
{
// set to check if matrix cell is visited before or not
set<Node> visited;
// create a queue and enqueue first node
queue<Node> q;
q.push(src);
// run till queue is not empty
while (!q.empty())
{
// pop front node from queue and process it
Node node = q.front();
q.pop();
int x = node.x;
int y = node.y;
int dist = node.dist;
// if destination is reached, return distance
if (x == dest.x && y == dest.y)
return dist;
// Skip if location is visited before
if (!visited.count(node))
{
// mark current node as visited
visited.insert(node);
// check for all 8 possible movements for a knight
// and enqueue each valid movement into the queue
for (int i = 0; i < 8; ++i)
{
// Get the new valid position of Knight from current
// position on chessboard and enqueue it in the
// queue with +1 distance
int x1 = x + row[i];
int y1 = y + col[i];
if (valid(x1, y1))
q.push({x1, y1, dist + 1});
}
}
}
// return INFINITY if path is not possible
return INT_MAX;
}
// main function
int main()
{
// source coordinates
Node src = {0, 7};
// destination coordinates
Node dest = {7, 0};
cout << "Minimum number of steps required is " << BFS(src, dest);
return 0;
}
最佳答案
假设终点位置未链接到骑士(任何骑士都可以以任何终点位置结束),这是一个答案。这是一个与编程语言无关的算法问题,因此我不会显示任何代码。
最简单但效率最低的方法是假设每个骑士都有一个所需的最终位置。您通过索引将最终位置的所有排列分配给骑士,并为每个排列计算结果。最后,您返回最小结果。在您的示例中,一个结果将是6
(原始映射),另一个结果将是4
(交换最终位置,唯一的其他排列),因此您将获得4
。这种方法的问题是,对于n个骑士,您将拥有n个!排列考虑。
贪婪的方法是让每个骑士移动直到到达最后一个地点,然后让另一个跳到另一个地点。这将对您的示例有效,但在此示例中会出错:
5
。但是,最佳解决方案是:
(5,4) <- first knight
(3,3) = 1 <- distance to first final position
(9,6) = 2 <- distance to second final position
(2,1) <- second knight
(3,3) = 1
(9,6) = 4
(3,3)
上为骑士选择
(5,4)
将会受到3的惩罚,因为另一个骑士现在将需要再移动3步(因为我们标记了其最近的最终位置)。但是,在
(3,3)
上为骑士选择
(2,1)
的惩罚为1,因为另一个骑士只需要再移动一个步骤即可。
关于c++ - 国际象棋骑士到达棋盘上某个位置的最小步骤数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59509860/
我正在尝试制作一个程序,显示飞机到达和起飞的时间表,然后要求用户在 C 中输入时间。然后程序将找到最接近输入时间的到达时间用户。问题是它没有按预期工作,并且给我一个错误的到达时间,甚至不接近输入的时间
我有一个不断填充新信息行的 Excel 表,其中一列与联系客户的日期(有时为空 - 无需输入日期)相关,如果在 10 内没有收到回复从那以后的几天,我们必须发送提醒,如果过了 17 天,我们必须取消订
实际到达很简单,标签进入接收器天线范围,但是偏离是造成问题的原因。 首先,我们了解一些有关设置的信息。 标签: 它们以433Mhz的速度工作,每1.5秒钟发送一次“心跳”,移动时进入传输突发模式,这种
我构建了这段代码来从 URL 获取 XML我使用了 AsyncTask,当到达 getInputStream() 点时,半身应用程序仍然崩溃 重点是我想从 url 获取 XML 作为字符串。 我尝试不
所以我有一个 TDBGrid,我的目的是搜索 DBGrid 的 Fieldname 并将其与我的编辑的 Text 属性进行比较,如果它们相等,则 我想将找到匹配项的整列写入列表框。 通过带有 fiel
我会写得非常简单,因为实际的修复并不比我不理解的实际设计重要。似乎一旦我的 @RequestBody 命中 @Controller,有关 subtype 的信息就会丢失。 假设我们有: class A
所以我正在做这个简单的动态编程问题,关于达到 n一次只能走 1 或 2 步。我知道答案基本上是一个斐波那契序列,答案是:达到n-2的步骤数+ 到达 n-1 的步数. T(n) = T(n-1) +
(function start (){ $('.bar').each(function(i){ var $bar = $(this); $(this).append('')
我有一个程序,我在启动它之前要求用户输入。 public static void main(String args[]) { String database = JOptionPane.sho
就是这样,我必须在提交按钮上有一张图片,但它根本没有出现。 我希望它看起来像这样: 现在看到我的是这样的,我不明白为什么它没有出现在页面上。 HTML CSS #sognu { bac
click here 点击后重定向至 xyz.com/#contact, 现在我想获得div #abc的顶部位置 //set the value as a variable, and remove t
here is a fiddle to know where I am starting from 我要解决的问题涉及对单个 html 文件的内容进行“分页”,以一种将它们一次锁定在一个部分中的方式。
是否可以在传递页面部分时运行 javascript 函数?我想要实现的是类似于 Twitter Bootstrap 的 scrollspy。 最佳答案 您可以使用 waypoints 插件: http
我有一个可以动态调整其大小的 iframe。我通过父页面上的发布消息和监听器解决了这个问题,因此每次 iframe 的内容发生变化时,iframe 的大小也会发生变化,并且永远不会有滚动条。 在 if
我试图让我的导航栏在到达我在网站下方设置的 anchor 时变得透明。 这是我的HTML Home About Logo W
我写了一个简单的程序来管理姓名列表(下面是程序的一部分)。我希望函数“choice()”结束并返回到 main()——从而结束程序——当用户对变量“option”的输入为 4 时。然而,choice(
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
代码片段在 while 循环后有一个 EOF,之后必须再次重新打开文件 - fopen 被重用。我的问题是是否有办法避免这种笨拙的 fopen 双重使用或以某种方式不使用 EOF? if (!(f=f
从这个页面: http://www.beta.inegi.org.mx/app/buscador/default.html?q=e15a61a 我正在尝试检索此网址: http://www.beta.
我使用维基百科的 API 来获取有关页面的信息。API 给我这样的 JSON: "query":{ "pages":{ "188791":{ "pageid":18879
我是一名优秀的程序员,十分优秀!