解题思路:BFS

1)马的跳跃方向

在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向;

2)基本过程

设当前点(i,j),方向k,沿方向k跳一步后的新点(newi,newj);每走一步,都要判断新点(newi,newj)是否还在棋盘上:

若1£newi£8且1£newj£8,则新点仍在棋盘上,则还需判断该点是否已经走过,即

若visited[newi][newj]=0,表示该步可走;

若visited[newi][newj]=1,表示该点已经走过,不能再走,放弃当前方向,并转向下一个方向试探;

否则,直接放弃当前方向,并转向下一个方向试探;

本题其实BFS的模板题。照着模板写就是了。。。。

代码如下:

/*
* 1372_1.cpp
*
* Created on: 2013年8月15日
* Author: Administrator
*/ #include <iostream>
#include <queue> using namespace std; char str1[5],str2[5]; const int maxn = 100;
bool visited[maxn][maxn]; //*
int dir[8][2]={{2,1},{2,-1},{-2,1},{-2,-1},{1,2},{1,-2},{-1,2},{-1,-2}}; struct State{
int x;
int y;
int step_counter;
}; bool checkState(State st){ //*
if(!visited[st.x][st.y]&&(!(st.x <0 ||st.x >= 8 ||st.y <0 ||st.y >=8))){
return true;
} return false;
} int bfs(){ queue<State> q;
State st;
State now ,next;
int x_e,y_e; //*
st.x = str1[1] - '1';
st.y = str1[0] - 'a';
st.step_counter = 0;
x_e = str2[1] - '1';
y_e = str2[0] - 'a'; q.push(st);
memset(visited,0,sizeof(visited));
visited[st.x][st.y] = 1;
while(!q.empty()){
now = q.front(); if(now.x == x_e && now.y == y_e){
return now.step_counter;
}
int i;
for(i = 0 ; i < 8 ; ++i){
next.x = now.x + dir[i][0];
next.y = now.y + dir[i][1];
next.step_counter = now.step_counter + 1; if(checkState(next)){
q.push(next);
visited[next.x][next.y] = 1;
}
}
q.pop(); } return -9;
} int main(){ int ans;
while(scanf("%s%s",str1,str2)!=EOF){
ans = bfs();
printf("To get from %s to %s takes %d knight moves.\n",str1,str2,ans);
}
}

(step4.2.1) hdu 1372(Knight Moves——BFS)的更多相关文章

  1. HDU 1372 Knight Moves(BFS)

    题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...

  2. HDU 1372 Knight Moves(最简单也是最经典的bfs)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  3. HDU 1372 Knight Moves(bfs)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...

  4. HDU 1372 Knight Moves (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  5. HDU 1372 Knight Moves【BFS】

    题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...

  6. 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 ...

  7. HDOJ/HDU 1372 Knight Moves(经典BFS)

    Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...

  8. HDU 1372 Knight Moves

    最近在学习广搜  这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...

  9. [宽度优先搜索] HDU 1372 Knight Moves

    Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

随机推荐

  1. Repeater 无刷新分页

    原文:http://blog.csdn.net/Sandy945/archive/2009/05/22/4208998.aspx 本文讲述的是如何利用 XMLHttpRequest 来对 Repeat ...

  2. 一般处理程序在VS2012中打开问题

    问题:如果你用vs2012建立的一个一般处理程序,运行查看是,出现这样的界面 原因:VS2012默认使用IIS Web服务器,而不是Visual Studio开发服务器,基于安全考虑IIS默认不允许浏 ...

  3. django-rest-framework 快速开始

    搭建项目 # Set up a new project django-admin.py startproject tutorial cd tutorial # Create a virtualenv ...

  4. iOS合并静态库文件

    具体命令如下(在控制台输入如下命令): lipo -create 其中一个要合并的静态库 另一个要合并的静态库 -output 合并后的静态库

  5. Ubuntu 14.10 编译 qt4.8.6

    0. 假设你已经可以在上面写基本的C++程序.(即:c/C++开发环境已经就绪)1. $ sudo apt-get build-dep libqt4-dev 2. $ sudo apt-get ins ...

  6. 用wfastcgi在IIS下部署Django&Flask

    Django跟Flask在Linux底下都可以很方便地以FastCGI模式部署,貌似IIS下面不很好配置,而且IIS也缺少一个像PHPmanager一样的全自动配置工具,在公司服务器上部署起来颇费周折 ...

  7. python的内置函数bin()

    bin(x) 中文说明:将整数x转换为二进制字符串,如果x不为Python中int类型,x必须包含方法__index__()并且返回值为integer: 参数x:整数或者包含__index__()方法 ...

  8. float编码杂谈

    浮点数的编码转换采用的是IEEE规定的编码标准,float和double 这两种类型的数据的转换原理相同,但是由于范围不一样,编码方式有些区别.IEEE规定的编码会将一个浮点数转换为二进制数.以科学计 ...

  9. Java Web 入门(一)使用 Intellij IDEA 14.1.5 创建 Maven Web项目

    1.基础配置 1.1 安装 JDK1.7,配置系统变量:JAVA_HOME 和 Path 1.2 安装 Tomcat 7.0 1.3 安装  Intellij IDEA 14.1.5 1.4 Mave ...

  10. MVC-06 安装部署

    部署网站往往是一件麻烦事,因为在安装部署的过程中,经常有许多步骤要运行,对于许多不太熟悉IIS/SQL的新手来说,部署网站编程一件非常困难且危险的事.Visual Studio 2012在ASP.NE ...