(step4.2.1) hdu 1372(Knight Moves——BFS)
解题思路: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)的更多相关文章
- 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(最简单也是最经典的bfs)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- 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 ...
- HDU 1372 Knight Moves【BFS】
题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...
- 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
最近在学习广搜 这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
- [宽度优先搜索] HDU 1372 Knight Moves
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
随机推荐
- Head First设计模式学习笔记
最近在学C++,直接语法之后觉得不太有意思,直接做项目又觉得太肤浅.正好之前一直想学设计模式来着,可惜之前一直在玩C,所以没有机会深入学习,于是决定用C++把设计写一遍.看了点GOF的<设计模式 ...
- 在 win 10 中使用sql 2012 附加低版本数据失败的解决办法。
随着win 10 的发布,我也尝试把自己的笔记本升级下,体验win10,由于自己电脑好长时间没有管理过,东西比较乱,一激动就格式了硬盘.但是所有的资料都丢失了,不过我都提前备份到网盘上.好了,废话不多 ...
- eclipse自动生成的appcompat_v7出错
用eclipse新建Android工程时,自动生成的appcompat_v7出错,有个红色交叉,而且新建的Android工程有一个红色感叹号. 这时你去看看你新建的Android工程是不是没有生成R文 ...
- C# 委托和方法
委托是一种特殊的引用类型,它将方法也作为特殊的对象封装起来,从而将方法作为变量或参数进行传递 using System; using System.Collections.Generic; using ...
- Foundation 框架 NSArray、NSMutableArray排序
一.使用selector对数组进行排序(无返回) 数组 book 中包含 AddressCard对象. 1.对数组调用 sortUsingSelector方法 -(void) sortByName { ...
- 寻找数列中第k大的数算法分析
问题描述:给定一系列数{a1,a2,...,an},这些数无序的,现在求第k大的数. 看到这个问题,首先想到的是先排序,然后直接输出第k大的数,于是得到啦基于排序的算法 算法一: #include&l ...
- js正则语法
整数或者小数:^[0-9]+\.{0,1}[0-9]{0,2}$只能输入数字:"^[0-9]*$".只能输入n位的数字:"^\d{n}$".只能输入至少n位的数 ...
- 托管服务帐号(Managed Service Account)
托管服务帐号是绑定到单独的机器上,并且仅用于服务所用,所以不能用来登录. 创建托管帐号,不需要指定密码,密码会由活动目录自动管理.并且根据密码策略(默认30天)自动刷新,期间不影响服务. 在创建完之后 ...
- java Serialization and Deserializaton
This article from JavaTuturial Java provides a mechanism, called object serialization where an objec ...
- 运用Python语言编写获取Linux基本系统信息(一):获得Linux版本、内核、当前时间
申请博客有一段时间了,然而到现在还一篇没有写过..... 主要因为没有想到需要写些什么,最近在学习Python语言,照着书上看了看最基础的东西,发现根本看不进去,而且光看的话今天看了觉得都理解懂了,过 ...