解题思路: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. SQL Server索引进阶:第十级,索引内部结构

    原文地址: Stairway to SQL Server Indexes: Level 10,Index Internal Structure 本文是SQL Server索引进阶系列(Stairway ...

  2. WebApi个人理解概要

    WebApi概要 Global文件的作用: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class MvcApplication : System.We ...

  3. MSSQL数据库统计所有表的记录数

    今天需要筛选出来库中行数不为零的表,于是动手写下了如下存储过程. CREATE PROCEDURE TableCount AS BEGIN SET NOCOUNT ON ),RowsCount INT ...

  4. C# Best Practices - Define Proper Classes

    Application Architecture Define the components appropriately for the application and create project ...

  5. C3p0实践

    jar包 c3p0-0.9.2.1.jar mchange-commons-java-0.2.3.4.jar mysql-connector-java-5.1.28-bin.jar 建立数据库 CRE ...

  6. servlet同一用户的不同页面共享数据

    一.cookie技术 cookie的讲解和使用 --------------- 服务器在客户端保存用户的信息,比如登录名,密码等...就是cookie, 服务器端在需要时可以从客户端读取. cooki ...

  7. mybatis优化配置

    在src下建立db.properties driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis name=root ...

  8. golang并发编程

    golang并发编程 引子 golang提供了goroutine快速实现并发编程,在实际环境中,如果goroutine中的代码要消耗大量资源时(CPU.内存.带宽等),我们就需要对程序限速,以防止go ...

  9. 【分享】JS生成随机字符串

    之前忘了从哪里找到的一段代码,整理电脑时,记录为博文备查,原创不是我. function randomString(len) { len = len || 32; var $chars = 'ABCD ...

  10. 实战Windows 7的Windows Media Center

    简介 本文讲述如何通过Windows 7的Windows Media Center搭建强劲的综合娱乐电视系统,同时讲述Windows Media Center的实际使用感受,以及如何通过Windows ...