hdu1372——Knight Moves

GStreamer 源代码入门 GStreamer 是一个高度模块化的多媒体框架,其源代码结构清晰,设计精良。 阅读详情

Knight Moves

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6810    Accepted Submission(s): 4107


Problem Description
A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of 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.
 

Input
The input file will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.
 

Output
For each test case, print one line saying "To get from xx to yy takes n knight moves.".
 

Sample Input
  
e2 e4 a1 b2 b2 c3 a1 h8 a1 h7 h8 a1 b1 c3 f6 f6
 

Sample Output
  
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.
 

Source
 

Recommend
Eddy   |   We have carefully selected several similar problems for you:   1072  1242  1016  1010  1548 
 

Statistic | Submit | Discuss | Note



#include<queue>  
#include<cstdio>  
#include<cstring>  
#include<iostream>  
#include<algorithm>  

using namespace std;  
      
int dir[8][2] = { {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2} };  
char mat[10][10];  
bool vis[10][10];  
      
struct node  
{  
	int x,y;  
 	int t;  
}temp1, temp2; 

queue< node >qu; 
 
bool is_legal(int x, int y)  
{  
	if( x < 1 || x > 8 || y < 1 || y > 8)  
 		return false;  
   	return true;  
}  
      
int bfs(int sx, int sy, int ex, int ey)  
{  
	memset( vis, 0, sizeof(vis) );  
 	while( !qu.empty() )  
  		qu.pop();  
    temp1.x = sx;  
    temp1.y = sy;  
    temp1.t = 0;  
    vis[temp1.x][temp1.y] = 1;  
    qu.push(temp1);
    while( !qu.empty() )  
    {  
    	temp1 = qu.front();  
     	qu.pop();
	 	if(temp1.x == ex && temp1.y == ey)
 		{
 			return temp1.t;
   		}     
      	for(int i = 0; i < 8; i ++)  
       	{  
        	temp2.x = temp1.x + dir[i][0];  
         	temp2.y = temp1.y + dir[i][1]; 
          	if( !is_legal(temp2.x, temp2.y) )  
           		continue;  
           	if(vis[temp2.x][temp2.y] == 1)  
            	continue; 
           	vis[temp2.x][temp2.y] = 1;  
            temp2.t = temp1.t + 1;  
            qu.push(temp2);  
            }  
        }  
    }  
    
int main()  
{
	char s[3];
	char e[3];
  while( ~scanf("%s%s", s, e))  
  { 
		int sx = s[0] - 'a' + 1;
		int sy = s[1] - '0';
		int ex = e[0] - 'a' + 1;
		int ey = e[1] - '0';
  		printf("To get from %s to %s takes %d knight moves.\n", s, e, bfs(sx, sy, ex, ey));
  }  
	return 0;  
}  


win10 跳过U8环境检测的IE WebControls.rar win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。 立即下载

相关推荐

Unity Scriptable Build Pipeline (SBP) 详解

Unity 的可编程构建管线(Scriptable Build Pipeline, SBP)标志着引擎构建架构的一次关键转变,它从不透明的 C++ 实现转向了灵活、可扩展的 C# 框架。SBP 旨在满足现代游戏开发日益增长的需求,通过改进增量处理和高效缓存机制显著提升了构建性能。它赋予开发者对内容构建过程前所未有的控制力,从而能够针对特定项目需求进行深度定制。尽管 SBP 是 Unity 推荐的 Addressables 系统的重要基础层,但理解其核心组件、构建流程和高级功能对于优化大型项目、简化 CI/C

m0_57771536的博客 2209

hdu1372 Knight Moves(BFS)

Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5519    Accepted Submission(s): 3370 Problem Description A friend of you

GAO1024 682

C#上位机实战:借助NetToPLCSim与西门子S7-1200仿真通信全攻略

本文详细介绍了如何使用C#开发上位机程序,通过NetToPLCSim与西门子S7-1200进行仿真通信的全过程。从环境准备、PLC仿真环境搭建、网络配置到C#代码实现,提供了完整的实战指南和常见问题解决方案,帮助开发者快速掌握工业自动化通信技术。

weixin_30596343的博客 440

hdu 1372 Knight Moves <java>

Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3761 Accepted Submission(s): 2342 Problem Description A friend of you is doin

陌生人的专栏 1148

Zotero内存占用与存储优化:从根源解决卡顿与空间不足

文献管理软件在学术工作中扮演着重要角色,其核心原理是通过数据库管理元数据与附件文件,实现文献的快速检索与组织。然而,随着文献库规模增长,软件的资源管理机制可能面临挑战,尤其是内存占用与存储空间优化问题。从技术价值看,高效的附件管理策略不仅能提升软件响应速度,还能确保数据安全与可移植性。在实际应用场景中,研究者常遇到软件卡顿、存储目录膨胀等痛点,这通常与附件存储模式、缓存机制及数据库维护有关。本文聚焦于Zotero这一主流工具,针对其内存占用高、文件无法删除等常见问题,提供了外置存储、链接附件、数据库维护等系

weixin_34209060的博客 464

鸿蒙之HML语法参考

HML(HarmonyOS Markup Language)是一套类HTML的标记语言,通过组件,事件构建出页面的内容。页面具备数据绑定、事件绑定、条件渲染和逻辑控制等高级能力。

2401_82772199的博客 1649

hdu1372 Knight Moves(BFS)

Link:http://acm.hdu.edu.cn/showproblem.php?pid=1372

林下的码路 785

使用ACOLITE对OLCI影像进行大气校正+批处理

使用ACOLITE对OLCI影像进行大气校正+批处理

mrzhy1的博客 7361

HDU-1372-Knight Moves

HDU-1372-Knight Moves http://acm.hdu.edu.cn/showproblem.php?pid=1372 求“马”从一点到另一点的最短距离,马走日,BFS即可 #include&lt;stdio.h&gt; #include&lt;string.h&gt; #include&lt;stdlib.h&gt; int dir[8][2]={{-2,1},{-2,...

java开发指南博客 【转载】 146

【3天快速入门WPF】11-附加属性

创建一个静态类,用于定义附加属性。使用方法注册附加属性。提供Get和Set方法来获取和设置附加属性的值。通过以上步骤,我们可以在WPF中轻松实现和使用附加属性。附加属性的主要用途包括为控件添加自定义行为、控制布局信息(如Grid.Row和)以及实现动态样式。

Zy100Papa的博客 679

如何通过单步调试的方法,找到 SAP Gateway 到底把 Fiori 应用发送的 OData 请求,投递到哪一台 ABAP 后台服务器了?

这是一个 Count 请求,得到服务器响应的数据是 204,这个 204 会以数字的形式,显示在 Fiori Launchpad tile 上。那如果当前 Gateway 系统可以连接到多台 ABAP 后台服务器,比如下图所示,可以连接 SAP S/4HANA,ERP 或者 SAP CRM,我怎么知道 SAP UI5 应用上显示的数据,到底来自哪一台 ABAP 后台服务器呢?,就是上图 ABAP 调试器里断点停留的位置,即 SAP Frontend 服务器,通过 RFC 将 OData 请求投递给。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 642

HUD 1372 Knight Moves DFS()

Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3929    Accepted Submission(s): 2448 Problem Description A friend of you

qq490691606的专栏 814

BART non-Cartesian 重建:并行成像 压缩感知

本文主要使用并行成像和压缩感知方法实现non-Cartesian MRI 数据的重建。

张哥频道:电生理信号和磁共振成像 1182

HDU - 1372 Knight Moves(bfs入门)

HDU - 1372 Knight Moves 题目链接:https://vjudge.net/problem/HDU-1372#author=swust20141567 题目: 在象棋王国,尼古拉斯.火山是一匹英俊的马,他非常幸运迎娶了白马王国的公主,他们将度蜜月,你现在是他们的女仆,火山会问你去一些地方最少需要多少步,这么简单的事当然难不倒你。由于火山是一匹马,他的移动方式将会遵...

weixin_30294709的博客 94

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

hdu 1372 Knight Moves

hdu   1372   Knight Moves              题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372                                                               题目大意:同样还是跑马数步数(不过千万别以为是和poj1915一样的题啊~) 题目分

slicer的技术交流栈 927

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

[ACM] hdu 1372 knight moves【BFS】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 参考链接:http://www.acmerblog.com/hdu-1372-Knight-Moves-1802.html 转载于:https://www.cnblogs.com/fjl-vxee/p/6402987.html

weixin_30835933的博客 102

HDU 1372 Knight Moves

A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboa...

Gem 2003

HDU T1372 Knight Moves

                                  HDU T1372 Knight Moves 题解:     题目意思就是给你起点和终点,让你以国际象棋的骑士的走法走到终点,其实就相当于玩中国象棋,马去吃掉其他子类似的走法。搞清楚八个方向怎么走,这道题就差不多了。 走法: (辛苦大佬花时间做图,小弟借用,取自秦石秦草) 还有注意的一点是看清楚题呀,鄙人以为a-z,...

Mr__Charles的博客 235

hdu 1372 Knight Moves

hdu 1372  Knight Moves Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64d Description A friend of you is doing research on the Traveling Knight Problem (TKP)

GrEedWish_X的专栏 512

hdu 1372 bfs Knight Moves

题目  http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一个简单的搜索问题    题目意思是   一只马从一个点到另外一个点就可以了   你要知道象棋中马是怎么走的。 其他的就可以了,刚开始做这个题目的时候 ,由于标记数组为mark[9][9]   但是我在初始化的时候 for(int i=0;i<=9;i++) for(int j

aibianchendewo的专栏 594

Hdu 1372 Knight Moves

Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3740    Accepted Submission(s): 2326 Problem Description A friend of you i

Magic__ 554

HDU 1372 Knight Moves

Knight Moves http://acm.hdu.edu.cn/showproblem.php?pid=1372 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3908    Accepted Submission(s): 2...

weixin_34221773的博客 155

HDU 1372 Knight Moves

Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11424    Accepted Submission(s): 6712 Problem Description A friend of

小学生 423

hdu 1372 Knight Moves

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed...

dong12276的博客 110

HDU-1372 Knight Moves

HDU 1372 Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n...

你呢的博客 935
上一篇: hdu1240——Asteroids!
下一篇: hdu1495——非常可乐
tokers
博客等级 码龄12年 71粉丝 700原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值