Sicily 1936. Knight Moves
题目地址: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的更多相关文章
- Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- HDU 1372 Knight Moves
最近在学习广搜 这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
- [宽度优先搜索] HDU 1372 Knight Moves
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- UVA 439 Knight Moves --DFS or BFS
简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...
- 【POJ 2243】Knight Moves
题 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are ...
- hdu Knight Moves
这道题实到bfs的题目,很简单,不过搜索的方向变成8个而已,对于不会下象棋的会有点晕. #include <iostream> #include <stdio.h> #incl ...
- HDU 1372 (搜索方向稍有改变) Knight Moves
其实手写模拟一个队列也挺简单的,尤其是熟练以后. 尼玛,这题欺负我不懂国际象棋,后来百度了下,国际象棋里骑士的走法就是中国象棋里面的马 所以搜索就有八个方向 对了注意初始化标记数组的时候,不要把起点标 ...
- HDU 1372 Knight Moves【BFS】
题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...
随机推荐
- Java 中 Vector、ArrayList、List 使用深入剖析【转载】
线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经为我们提供了一系列相应的类来实现基本的数据结构.这些类均在java.util包中.本文试图通过简单的描述,向读者阐述各个类的作用以 ...
- [Design Pattern] Filter Pattern 简单案例
Filter Pattern,即过滤模式,通过不同的过滤标准,或者低耦合将过滤标准组合在一起,对一组对象进行过滤,属于结构类的设计模式. 下面是一个过滤模式的简单案例. Criteria 定义过滤接口 ...
- Mina笔记
1.MINA框架简介 MINA(Multipurpose Infrastructure for Network Applications)是用于开发高性能和高可用性的网络应用程序的基础框架.通过使用M ...
- SheetOffice控件使用分享
1. 控件属性及说明 Template:套用的模板目录(套用模板会使用到) 模板中必须包含书签: Body,这个是在代码中写死了的,是把当前文档的内容插入到模板的Body书签中. 如果使用印章,必须 ...
- 利用golang语法检查对象是否实现了接口
var _ ipc.Server = &CenterServer{} CenterServer是否实现了 ipc.Server的接口.编译期间检测,这是很好的编程实践. 稍后详述...
- Demon_接金币(三个掉落物品预设体,一接物体的工具)
接物体的工具 using UnityEngine; using System.Collections; public class Tool : MonoBehaviour { float hor; V ...
- Android BaseAdapter ListView (SD卡中文件目录显示出来)
首先搭建activity_main.xml布局 搭建ListView中显示的布局 创建适配器 将File数据和UI适配 MainActivity中将ListView设置适配器,并设置监听 //获取SD ...
- 跟着Android学设计模式:代理(proxy)
代理模式 1.意图:为其它对象提供一种代理以控制对这个对象的訪问. 2.适用性: 远程代理:虚代理.保护代理:智能引用: Android源代码中用到了大量的代理设计模式.比方Context的设计,Bi ...
- Ubuntu ssh的使用
1. 题外话:install teamviewer on Ubuntu. wget http://download.teamviewer.com/download/teamviewer_linux.d ...
- hdu 2105
#include <iostream> #include <stdio.h> using namespace std; int main() { double a,b,c,d, ...