题目地址:1936. Knight Moves

思路:

这道题一开始不理解题意…orz...囧,看大神们理解的。

题意是说一个8*8的国际象棋,骑士以马的形式走动(“日”字型),指定两个点,输出最小的步骤。

可以利用广度搜索解决。

具体代码如下:

 #include <iostream>
#include <queue>
#include <cstring>
#include <string>
using namespace std; int dx[] = {-, -, -, -, , , , }; //可以走八个方向
int dy[] = {-, -, , , , , -, -}; bool visited[]; int main() {
int t;
cin >> t;
while (t--) {
memset(visited, false, sizeof(visited));
int distance[] = {}; string node1, node2;
cin >> node1 >> node2; int X = (node1[]-'a')* + node1[]-'';
int Y = (node2[]-'a')* + node2[]-''; queue<int> store;
store.push(X);
while (!store.empty()) {
if (store.front() == Y)
break; int x = store.front()/;
int y = store.front()%; for (int i = ; i < ; i++) {
int nx = x+dx[i];
int ny = y+dy[i]; if (nx < ||nx > ||ny < ||ny > )
continue;
int temp = nx* + ny; if (!visited[temp]) {
store.push(temp);
visited[temp] = true;
distance[temp] = distance[store.front()] + ;
}
}
store.pop();
}
cout << "To get from " << node1
<< " to " << node2 << " takes "
<< distance[Y] << " knight moves.\n";
} return ;
}

Sicily 1936. Knight Moves的更多相关文章

  1. Knight Moves

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  2. HDU 1372 Knight Moves

    最近在学习广搜  这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...

  3. [宽度优先搜索] HDU 1372 Knight Moves

    Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  4. HDU 1372 Knight Moves (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  5. UVA 439 Knight Moves --DFS or BFS

    简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...

  6. 【POJ 2243】Knight Moves

    题 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are ...

  7. hdu Knight Moves

    这道题实到bfs的题目,很简单,不过搜索的方向变成8个而已,对于不会下象棋的会有点晕. #include <iostream> #include <stdio.h> #incl ...

  8. HDU 1372 (搜索方向稍有改变) Knight Moves

    其实手写模拟一个队列也挺简单的,尤其是熟练以后. 尼玛,这题欺负我不懂国际象棋,后来百度了下,国际象棋里骑士的走法就是中国象棋里面的马 所以搜索就有八个方向 对了注意初始化标记数组的时候,不要把起点标 ...

  9. HDU 1372 Knight Moves【BFS】

    题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...

随机推荐

  1. 《Linear Algebra and Its Applications》-chaper1-线性方程组-线性相关性

    这篇文章主要简单的记录所谓的“线性相关性”. 线性相关性的对象是向量R^n,对于向量方程,如果说x1v1 + x2v2 + …+xmvm = 0(其中xi是常数,vi是向量)有且仅有一个平凡解,那么我 ...

  2. K - Transformation-hdu 4578(多操作混合区间更新)线段树

    题意:有四种操作 1,  区间 [l, r] 的值都加上 C 2,  区间 [l, r] 的值都乘上 C 3,  区间 [l, r] 的值都变为C 4,  求区间 [l, r]所有数的p次方的和 分析 ...

  3. PHP学习之[第03讲]PHP5.4 语法、常量、变量、数据类型详解

    1.<?php echo "Hello World"; ?> 2.//注释1,#注释2,/* 注释3 */ 3.系统常量: __FILE__默认常量,是指PHP程序文件 ...

  4. Android Studio 2.1.x 关联SDK API Source

    问题: 看图=>,当在android studio里ctrl+鼠标左键查看例如: TextUtils.isEmpty(content);这段代码的isEmpty方法的实现的时候经常就跑到如图所示 ...

  5. RMAN-00554: initialization of internal recovery manager package failed RMAN-04005

    [oracle@rac11g1 ~]$ rman target haha/haha@rac11g Recovery Manager: Release 11.2.0.3.0 - Production o ...

  6. URL具体解释

    浏览器因特网资源:URL是浏览器寻找信息时所需的资源位置.通过URL.应用程序才干找到并使用共享因特网上大量的数据资源. 大部分URL都遵循一种标准的格式: ①HTTP协议(http://或者http ...

  7. Ubuntu安装分区设置

    我的Ubuntu 安装分区 /boot   200M /          2000M /home  6000M swap    1000M /boot分区,它包含了操作系统的内核和在启动系统过程中所 ...

  8. 基于Android Volley的网络请求工具

    基于Android Volley的网络请求工具. 一.说明 AndroidVolley,Android Volley核心库及扩展工程.AndroidVolleySample,网络请求工具示例工程.Re ...

  9. JVM内存回收对象及引用分析

    自动垃圾回收是Java相较于C++的一个重要的特点,想了解JVM的垃圾回收机制,首先我们要知道垃圾回收是回收什么地方的垃圾,我在我的上一篇博客<JVM内存区域划分>里面有写到JVM里面的内 ...

  10. hdu 1232

    以前写的.....拿出来看看.... 并查集模板: #include <iostream> #include <string> using namespace std; int ...