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 ...
随机推荐
- Slf4j与其他日志系统兼容的使用
java生产的各种框架(如spring等)里各个框架会使用不同的日志体系,多个不同日志在一个jvm里混搭会出现一定问题 ,这里梳理一下java体系里常见的日志框架,以SFL4j为中心介绍下跟各个日志框 ...
- 搜索模式| 系列2——KMP算法
给定一个文本txt [0..n-1]和一个模式pat [0..m-1],写一个搜索函数search(char pat [],char txt []),在txt中打印所有出现的pat [] [].可以假 ...
- layer遮罩层 简单的遮罩层
在这里提供一个简单layer遮罩层,想深入了解可以进入 layer官网 多多学习哦. 先看下HTML页面代码 <!DOCTYPE html> <html lang="en& ...
- lesson - 3 ls /cd /path /alias /快捷键
内容概要: 1. 命令ls -l 详细信息-a 查看隐藏的文件或目录-d 只看目录本身,不列出目录下面的文件和目录-t 以时间先后排序 2 目录结构/bin, /sbin, /usr/bi ...
- 常用API接口汇总
下面列举了100多个国内常用API接口,并按照 笔记.出行.词典.电商.地图.电影.即时通讯.开发者网站.快递查询.旅游.社交.视频.天气.团队协作.图片与图像处理.外卖.消息推送.音乐.云.语义识别 ...
- CRM项目总结
CRM项目总结 一:开发背景 在公司日益扩大的过程中,不可避免的会伴随着更多问题出现. 对外 : 如何更好的管理客户与公司的关系?如何更及时的了解客户日益发展的需求变 ...
- leetcode — word-break-ii
import java.util.*; /** * Source : https://oj.leetcode.com/problems/word-break-ii/ * * Given a strin ...
- 初步了解 Django Models
Part1:了解主键 1. Python和Django版本如下: E:\django>python3 -V Python 3.5.2 E:\django>python3 -m d ...
- Web App、Hybrid App与Native App
在这个App的时代,转战了前端,一直接触的都是pc, 离out不远了. 那么接下来,app是我接下来半年的重点,为什么是半年,因为时间不多了. 因为是前端,那么我的重心肯定是 Web App, Hyb ...
- 骗子网站,X毛都没有,骗我九十九
前言 这几天在A市和B市奔波着,眼瞅着自己就要毕业了,必须得出来找份工作了. 和小伙伴在A市兜兜转转了几天,要不就是不合适没下文,要不就是给了offer,工资是在太低.心很累,然后就下B市了,看看B市 ...