HDU 1372 Knight Moves【BFS】
题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点。
因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形)
另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxn 105
using namespace std;
struct node
{
int x,y;
int step;
} now,next,st,en;
queue<node>q;
char m,n;
int vis[maxn][maxn];
int dir[][]={{,},{,},{-,},{-,},{-,-},{-,-},{,-},{,-}};
void bfs(int x,int y)
{
now.x=x;now.y=y;now.step=;
while(!q.empty()) q.pop();
q.push(now);
vis[x][y]=;
while(!q.empty())
{
now=q.front();q.pop();
if(now.x==en.x&&now.y==en.y)
{
printf("To get from %c%d to %c%d takes %d knight moves.\n",m,st.y,n,en.y,now.step);
return;
};
for(int i=;i<;i++)
{
next.x=now.x+dir[i][];
next.y=now.y+dir[i][];
if(next.x<||next.y>||next.y<||next.y>) continue;
if(!vis[next.x][next.y])
{
vis[next.x][next.y]=;
next.step=now.step+;
q.push(next);
if(next.x==en.x&&next.y==en.y)
{
printf("To get from %c%d to %c%d takes %d knight moves.\n",m,st.y,n,en.y,next.step);
return;
}
}
}
}
}
int main()
{
while(cin>>m>>st.y>>n>>en.y)
{
memset(vis,,sizeof(vis));
st.x=m-'a'+;
en.x=n-'a'+;
bfs(st.x,st.y);
}
}
HDU 1372 Knight Moves【BFS】的更多相关文章
- HDU 1372 Knight Moves(bfs)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- ZOJ 1091 (HDU 1372) Knight Moves(BFS)
Knight Moves Time Limit: 2 Seconds Memory Limit: 65536 KB A friend of you is doing research on ...
- HDOJ/HDU 1372 Knight Moves(经典BFS)
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
- HDU 1372 Knight Moves(最简单也是最经典的bfs)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- (step4.2.1) hdu 1372(Knight Moves——BFS)
解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...
- HDU 1372 Knight Moves(BFS)
题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...
- 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 ...
随机推荐
- 字典树trie的学习与练习题
博客详解: http://www.cnblogs.com/huangxincheng/archive/2012/11/25/2788268.html http://eriol.iteye.com/bl ...
- POJ 2039
#include<iostream> #include<stdio.h> #include<string> #define MAXN 20 using namesp ...
- POJ 1455
/* 冒泡排序n*(n-1)/2; */ #include <iostream> using namespace std; int main() { //freopen("acm ...
- java基础知识回顾之javaIO类---FileInputStream和FileOutputStream字节流复制图片
package com.lp.ecjtu; import java.io.FileInputStream; import java.io.FileNotFoundException; import j ...
- 几点基于Web日志的Webshell检测思路
http://www.open-open.com/lib/view/open1456751673359.html
- CKEditor上传图片—配置CKFinder
参考:http://blog.csdn.net/gavin710/article/details/8741738
- spring 注入失败
最近发现autowired注入总是失败. 总结下: 一个bean 要么都通过getter setter加上配置文件配置注入. <bean id="temResetService&quo ...
- 2016网易实习生编程题:数组中两个数的和等于sum
题目 找出数组中两个数的和等于sum的这两个数 解题 这个题目做过很多次了,利用HashMap,key为 sum-A[i] value为 i 当 加入HashMap时候A[i] 已经存在map中,ge ...
- Project Euler 103:Special subset sums: optimum 特殊的子集和:最优解
Special subset sums: optimum Let S(A) represent the sum of elements in set A of size n. We shall cal ...
- Cloudinsight Agent install script
#!/bin/bash # Cloudinsight Agent install script. set -e logfile="ci-agent-install.log" gis ...