题目连接

简单bfs搜索


  1. #include <set>
  2. #include <map>
  3. #include <cmath>
  4. #include <queue>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <iostream>
  8. #include <algorithm>
  9. using namespace std;
  10. typedef pair<int, int> pa;
  11. typedef long long LL;
  12. int dir[8][2]={2,1,2,-1,-2,1,-2,-1,1,2,1,-2,-1,2,-1,-2};
  13. struct node
  14. {
  15. int x;
  16. int y;
  17. int step;
  18. };
  19. int vis[10][10];//标记数组
  20. int st,sd,et,ed;
  21. queue<node>que;
  22. string c1,c2;
  23. void init()
  24. {
  25. for(int i=0;i<=8;i++)
  26. for(int j=0;j<=8;j++)
  27. vis[i][j]=0;
  28. }
  29. void dfs()
  30. {
  31. node now,next;
  32. now.x=st;
  33. now.y=sd;
  34. vis[st][sd]=1;
  35. now.step=0;
  36. while(!que.empty())
  37. que.pop();
  38. que.push(now);
  39. while(!que.empty())
  40. {
  41. now=que.front();
  42. que.pop();
  43. if(now.x==et&&now.y==ed)
  44. {
  45. cout<<"To get from "<<c1<<" to "<<c2<<" takes "<<now.step<<" knight moves."<<endl;
  46. break;
  47. }
  48. for(int i=0;i<8;i++)
  49. {
  50. int X=now.x+dir[i][0];
  51. int Y=now.y+dir[i][1];
  52. if(X>=1&&X<=8&&Y>=1&&Y<=8&&!vis[X][Y])
  53. {
  54. next.x=X;
  55. next.y=Y;
  56. next.step=now.step+1;
  57. vis[X][Y]=1;
  58. que.push(next);
  59. }
  60. }
  61. }
  62. }
  63. int main ()
  64. {
  65. while(cin>>c1>>c2)
  66. {
  67. init();
  68. st=c1[0]-'a'+1;
  69. sd=c1[1]-'1'+1;
  70. et=c2[0]-'a'+1;
  71. ed=c2[1]-'1'+1;
  72. dfs();
  73. }
  74. return 0;
  75. }

HDU<1372>/bfs的更多相关文章

  1. hdu 1372 BFS

    A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the sh ...

  2. hdu 4531 bfs(略难)

    题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...

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

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

  4. HDU 1372 Knight Moves(bfs)

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

  5. HDU 1372 Knight Moves (bfs)

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

  6. HDU 1372 Knight Moves【BFS】

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

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

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

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

  9. (step4.2.1) hdu 1372(Knight Moves——BFS)

    解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...

随机推荐

  1. 创建透明CEdit控件

    1.在资源编辑器中将Edit控件的Border设为none(当然也可以在代码中修改窗口样式) 2.在重载CEdit的类中响应MSG_OCM_CTLCOLOREDIT消息,在处理函数中将背景模式设为透明 ...

  2. lucene索引的创建与搜索

    package com.cs.multi; import java.io.File;import java.io.IOException; import org.apache.lucene.analy ...

  3. Pivot Index--Google

    Return the pivot index of the given array of numbers. The pivot index is the index where the sum of ...

  4. 浅谈 zookeeper 原理,安装和配置

    当前云计算流行, 单一机器额的处理能力已经不能满足我们的需求,不得不采用大量的服务集群.服务集群对外提供服务的过程中,有很多的配置需要随时更新,服务间需要协调工作,那么这些信息如何推送到各个节点?并且 ...

  5. SQL Server删除表信息的三种方法

    1.使用DELETE实现SQL Server删除表信息 (1)删除表中的全部信息 USE student GO DELETE student      --不加where条件,删除表中的所有记录 go ...

  6. Codeforces Round #375 (Div. 2)A. The New Year: Mee

    A. The New Year: Meeting Friends time limit per test 1 second memory limit per test 256 megabytes in ...

  7. 制作、烧写根文件系统,使用NFS,编译使用驱动程序

    制作YAFFS2.JFFS2文件系统映象文件制作文件系统映象非常简单.① 制作YAFFS2文件系统映象文件对于YAFFS2,要用到mkyaffs2image工具,它在/work/linux/tools ...

  8. HttpUtil工具类

    HttpUtil工具类 /** * 向指定URL发送GET方法的请求 * * @param url * 发送请求的URL * @param params * 请求参数,请求参数应该是name1=val ...

  9. MFC中获取系统当前时间

    1.使用CTime类 CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime(); str=tm.Format("现在时间是%Y年% ...

  10. 第十一节,编辑器软件PyCharm 5.0.3

    编辑器软件PyCharm 5.0.3 设置 主题方案 字体大小 行距 文件模板 文件编码 版本切换