解题思路: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. ceph启动脚本

    放在/etc/init.d/目录下,用法如下: root@u253:~# /etc/init.d/ceph === mon.a === usage: /etc/init.d/ceph [options ...

  2. SERVLET API 中 forward() 与 redirect()的区别?

    答:前者仅是容器中控制权的转向, 在客户端浏览器地址栏中不会显示出转向后的地址: 后者则是完全的跳转, 浏览器将会得到跳转的地址, 并重新发送请求链接. 这样, 从浏览器的地址栏中可以看到跳转后的链接 ...

  3. UVa10082 WERTYU

    #include <stdio.h>#include <string.h> int main(){ // 用C++提交AC    char s[] = "`12345 ...

  4. 服务器表导入到本地数据库SQL语句

    这是开启权限 exec sp_configure 'show advanced options',1 reconfigure exec sp_configure 'Ad Hoc Distributed ...

  5. 关于codestyle

    如果你的代码易于阅读,那么代码中bug也将会很少,因为一些bug可以很容被调试,并且,其他开发者参与你项目时的门槛也会比较低.因此,如果项目中有多人参与,采取一个有共识的编码风格约定非常有必要. 以t ...

  6. idea中使用scala运行spark出现Exception in thread "main" java.lang.NoClassDefFoundError: scala/collection/GenTraversableOnce$class

    idea中使用scala运行spark出现: Exception in thread "main" java.lang.NoClassDefFoundError: scala/co ...

  7. HTML+CSS笔记 CSS进阶再续

    CSS的布局模型 清楚了CSS 盒模型的基本概念. 盒模型类型, 我们就可以深入探讨网页布局的基本模型了.布局模型与盒模型一样都是 CSS 最基本. 最核心的概念. 但布局模型是建立在盒模型基础之上, ...

  8. 07-C语言流程控制if、switch

    目录: 一.流程控制 条件分支 if else 二.流程控制 开关分支 switch 回到顶部 一.流程控制 条件分支 1.语法格式:if(表达式1){ //表达式1为真(非0时),执行的语句部分. ...

  9. IOS 表视图(UITableVIew)的使用方法(8)表视图的编辑功能(多选)

    在表视图的删除操作中,每次只能够对其中一个单元进行删除,如果想要同时删除多条记录,不得不挨个地进行标准的删除操作 所以如果能够实现多选的机制,无论是删除还是其他功能的嫁接,都会变得更加方便 当UITa ...

  10. 关于js的一些关键知识点(call,apply,callee, caller,clourse,prototypeChain)

    可能不少学习javascript在使用call,apply,callee时会感到困惑,以下希望对于你有所帮助: 1.~~~call ,apply是函数(函数对象)的方法:callee是函数argume ...