HDU1372搜索
Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9320 Accepted Submission(s): 5495
the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.
Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves. To get from f6 to f6 takes 0 knight moves. 题意:象棋中悍马从起点到终点所需要走的最少步数#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <queue> using namespace std; char c1, c2;
int n1, n2, n3, n4;
int map[10][10];
int b[8][2] = {2,1,1,2,2,-1,1,-2,-1,-2,-2,-1,-2,1,-1,2};
int ans = 0;
struct node {
int x;
int y;
int step;
}; int judge(int x, int y) {
if ((x<1 || x>8) || (y<1 || y>8)) return 0;
if (map[x][y]) return 0;
return 1;
} int bfs() {
node cur, next;
cur.x = n1;
cur.y = n3;
cur.step = 0;
map[n1][n3] = 1;
queue <node> q;
q.push(cur);
while (!q.empty()) {
cur = q.front();
q.pop();
if (cur.x == n2 && cur.y == n4) return cur.step;
for (int i = 0; i<8; i++) {
next.x = cur.x + b[i][0];
next.y = cur.y + b[i][1];
if (next.x == n2 && next.y == n4) return cur.step+1;
if (judge(next.x, next.y)) { next.step = cur.step + 1;
map[next.x][next.y] = 1;
q.push(next);
}
//cout << i<< endl; } }
return -1;
} int main() {
while (cin >> c1>> n1 >> c2>> n2) {
n3 = (int)c1-96;
n4 = (int)c2-96;
//cout << n3 << endl << n4 << endl; memset(map, 0, sizeof(map));
ans = bfs();
cout << "To get from "<<c1<< n1<<" to "<<c2<< n2<<" takes " << ans << " knight moves." << endl;
}
return 0;
}
HDU1372搜索的更多相关文章
- hdu1372 dfs搜索之国际象棋的马
原题地址 题意 一个8x8的国际象棋棋盘,你有一个棋子"马".算出棋子"马"从某一格到还有一格子的最少步数. 与普通dfs不同的是,你能走的路线不是上下左右,四 ...
- hdu1372 Knight Moves BFS 搜索
简单BFS题目 主要是读懂题意 和中国的象棋中马的走法一样,走日字型,共八个方向 我最初wa在初始化上了....以后多注意... 代码: #include <iostream> #incl ...
- SQLSERVER走起微信公众帐号已经开通搜狗微信搜索
SQLSERVER走起微信公众帐号已经开通搜狗微信搜索 请打开下面链接 http://weixin.sogou.com/gzh?openid=oIWsFt-hiIb_oYqQHaBMoNwRB2wM ...
- solr_架构案例【京东站内搜索】(附程序源代码)
注意事项:首先要保证部署solr服务的Tomcat容器和检索solr服务中数据的Tomcat容器,它们的端口号不能发生冲突,否则web程序是不可能运行起来的. 一:solr服务的端口号.我这里的sol ...
- SQLServer地址搜索性能优化例子
这是一个很久以前的例子,现在在整理资料时无意发现,就拿出来再改写分享. 1.需求 1.1 基本需求: 根据输入的地址关键字,搜索出完整的地址路径,耗时要控制在几十毫秒内. 1.2 数据库地址表结构和数 ...
- HTML5轻松实现搜索框提示文字点击消失---及placeholder颜色的设置
在做搜索框的时候无意间发现html5的input里有个placeholder属性能轻松实现提示文字点击消失功能,之前还傻傻的在用js来实现类似功能... 示例 <form action=&quo ...
- bzoj1079--记忆化搜索
题目大意:有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块.所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n.相邻两个木块涂相同色显得 ...
- bzoj3208--记忆化搜索
题目大意: 花花山峰峦起伏,峰顶常年被雪,Memphis打算帮花花山风景区的人员开发一个滑雪项目. 我们可以把风景区看作一个n*n的地图,每个点有它的初始高度,滑雪只能从高处往低处滑[严格大于] ...
- Android中通过ActionBar为标题栏添加搜索以及分享视窗
在Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提供更为丰富的导航效果.Action ...
随机推荐
- 转:一篇讲线上优化查 CPU的脚本
原文链接:https://my.oschina.net/leejun2005/blog/1524687 摘要: 本文主要针对 Java 服务而言 0.背景 经常做后端服务开发的同学,或多或少都遇到 ...
- Python下载一张图片与有道词典
1.下载一张图片代码1 import urllib.request response = urllib.request.urlopen('http://photocdn.sohu.com/201009 ...
- Weblogic用户名密码获取
1.获取服务器上的Weblogic用户名.密码 工具:Xshell 第一步:连接至服务器上,新建目录: mkdir /scripts/DecryptionDemo 第二步:将Decrypt.java放 ...
- Mac_OS_Sierra_10.12.6编译OpenJDK9
编译环境以及依赖 macOS:Sierra,10.12.6 处理器:2.6 GHz Intel Core i7 内存:16 GB 2133 MHz LPDDR3 Command Line Tools ...
- C#后台生成验证码
https://www.cnblogs.com/vchenpeng/archive/2013/05/12/3074887.html /// <summary> /// 获 ...
- Head First设计模式之模板方法模式
一.定义 在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中,使得子类可以不改变算法结构的情况下,重定义该算法中的某些特定步骤. 比较通俗的说法,子类决定如何实现算法中的某些步骤,比如两个一连串 ...
- mysql commit 和 rollback
转自:http://blog.csdn.net/ying_593254979/article/details/12134629 SQL 语言类型 从功能上划分,SQL 语言可以分为DDL,DML和DC ...
- ldap命令的使用
转自:http://blog.chinaunix.net/uid-20690190-id-4085176.html 增:ldapadd 1)选项: -x 进行简单认证 -D 用来绑定服务器的D ...
- Nginx集群之WCF大文件上传及下载(支持6G传输)
目录 1 大概思路... 1 2 Nginx集群之WCF大文件上传及下载... 1 3 BasicHttpBinding相关配置解析... 2 4 编写 ...
- CDN 边缘规则,三秒部署、支持定制、即时生效,多种规则覆盖常用业务场景
2017年的最后一周,又拍云进行了一次重要升级,将自定义 Rewrite 升级为"边缘规则".互联网应用场景的日益多样化,简单.方便.快速的根据不同应用场景实现不同的功能变得越来越 ...