1. #include<iostream>
  2. #include<stdio.h>
  3. #define MAXN 350
  4. #include"queue"
  5. using namespace std;
  6. bool mark[MAXN][MAXN];
  7. struct point
  8. {
  9. int x;
  10. int y;
  11. int step;
  12. point()
  13. {
  14. step = ;
  15. }
  16. };
  17. bool bfs(point p);
  18. point b;
  19. int num;
  20. point e;
  21. point tem;
  22. queue<point> coll;
  23. int main()
  24. {
  25. //freopen("acm.acm","r",stdin);
  26. int test;
  27. int ans;
  28. cin>>test;
  29. while(test --)
  30. {
  31. //memset(road,0,sizeof(road));
  32. memset(mark,false,sizeof(mark));
  33. cin>>num;
  34. cin>>b.x>>b.y;
  35. cin>>e.x>>e.y;
  36. if(b.x == e.x && b.y == e.y)
  37. {
  38. cout<<<<endl;
  39. continue;
  40. }
  41. coll.push(b);
  42. mark[b.x][b.y] = true;
  43. // cout<<"----------"<<endl;
  44. while(!coll.empty() && !bfs(coll.front()) )
  45. {
  46. //cout<<"-================"<<endl;
  47. // cout<<coll.front().x<<endl;
  48. // cout<<coll.front().y<<endl;
  49. //break;
  50. coll.pop();
  51. }
  52. while(!coll.empty())
  53. {
  54. coll.pop();
  55. }
  56. }
  57. }
  58.  
  59. bool bfs(point p)
  60. {
  61. if(p.x - >= )
  62. {
  63. if(p.y - >= && !mark[p.x - ][p.y - ])
  64. {
  65. tem.x = p.x - ;
  66. tem.y = p.y - ;
  67. tem.step = p.step + ;
  68. mark[tem.x][tem.y] = true;
  69. if(tem.x == e.x && tem.y == e.y)
  70. {
  71. cout<<tem.step<<endl;
  72. return true;
  73. }
  74. else
  75. coll.push(tem);
  76. }
  77. if(p.y + < num && !mark[p.x - ][p.y + ])
  78. {
  79. tem.x = p.x - ;
  80. tem.y = p.y + ;
  81. tem.step = p.step + ;
  82. mark[tem.x][tem.y] = true;
  83. if(tem.x == e.x && tem.y == e.y)
  84. {
  85. cout<<tem.step<<endl;
  86. return true;
  87. }
  88. else
  89. coll.push(tem);
  90. }
  91. }
  92. ////////////////
  93. if(p.x + < num)
  94. {
  95. if(p.y - >= && !mark[p.x + ][p.y - ])
  96. {
  97. tem.x = p.x + ;
  98. tem.y = p.y - ;
  99. tem.step = p.step + ;
  100. mark[tem.x][tem.y] = true;
  101. if(tem.x == e.x && tem.y == e.y)
  102. {
  103. cout<<tem.step<<endl;
  104. return true;
  105. }
  106. else
  107. coll.push(tem);
  108. }
  109. if(p.y + < num && !mark[p.x + ][p.y + ])
  110. {
  111. tem.x = p.x + ;
  112. tem.y = p.y + ;
  113. tem.step = p.step + ;
  114. mark[tem.x][tem.y] = true;
  115. if(tem.x == e.x && tem.y == e.y)
  116. {
  117. cout<<tem.step<<endl;
  118. return true;
  119. }
  120. else
  121. coll.push(tem);
  122. }
  123. }
  124.  
  125. ///////////////////
  126. if(p.y - >= )
  127. {
  128. if(p.x - >= && !mark[p.x - ][p.y - ])
  129. {
  130. tem.x = p.x - ;
  131. tem.y = p.y - ;
  132. tem.step = p.step + ;
  133. mark[tem.x][tem.y] = true;
  134. if(tem.x == e.x && tem.y == e.y)
  135. {
  136. cout<<tem.step<<endl;
  137. return true;
  138. }
  139. else
  140. coll.push(tem);
  141. }
  142. if(p.x + < num && !mark[p.x + ][p.y - ])
  143. {
  144. tem.x = p.x + ;
  145. tem.y = p.y - ;
  146. tem.step = p.step + ;
  147. mark[tem.x][tem.y] = true;
  148. if(tem.x == e.x && tem.y == e.y)
  149. {
  150. cout<<tem.step<<endl;
  151. return true;
  152. }
  153. else
  154. coll.push(tem);
  155. }
  156. }
  157. //////////////////////
  158. if(p.y + < num)
  159. {
  160. if(p.x - >= && !mark[p.x - ][p.y + ])
  161. {
  162. tem.x = p.x - ;
  163. tem.y = p.y + ;
  164. tem.step = p.step + ;
  165. mark[tem.x][tem.y] = true;
  166. if(tem.x == e.x && tem.y == e.y)
  167. {
  168. cout<<tem.step<<endl;
  169. return true;
  170. }
  171. else
  172. coll.push(tem);
  173. }
  174. if(p.x + < num && !mark[p.x + ][p.y + ])
  175. {
  176. tem.x = p.x + ;
  177. tem.y = p.y + ;
  178. tem.step = p.step + ;
  179. mark[tem.x][tem.y] = true;
  180. if(tem.x == e.x && tem.y == e.y)
  181. {
  182. cout<<tem.step<<endl;
  183. return true;
  184. }
  185. else
  186. coll.push(tem);
  187. }
  188. }
  189. return false;
  190. }

关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。

技术网站地址: vmfor.com

POJ 1915的更多相关文章

  1. POJ 1915 Knight Moves

    POJ 1915 Knight Moves Knight Moves   Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 29 ...

  2. OpenJudge/Poj 1915 Knight Moves

    1.链接地址: http://bailian.openjudge.cn/practice/1915 http://poj.org/problem?id=1915 2.题目: 总Time Limit: ...

  3. poj 1915 http://poj.org/problem?id=1915

    /**< */#include <stdio.h> #include <string.h> #include <stdlib.h> #include < ...

  4. POJ 1915 Knight Moves(BFS+STL)

     Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20913   Accepted: 9702 ...

  5. POJ 1915 经典马步 双向bfs

    拿这个经典题目开刀...........可是双向时间优势在这题上的效果不太明显 #include <iostream> #include <algorithm> #includ ...

  6. poj 1915 KnightMoves(bfs)

    Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24094   Accepted: 11364 De ...

  7. 论深度优先(DFS)和广度优先搜索(BF)的优点及不足(更新ing)

    例题: POJ 1915 Knight Moves 骑士遍历问题(跳马问题) 在一个m*m的棋盘上,从任意一个给定的位置(sx , sy)出发,为象棋中的马找一条路通过最少的步数到达另一位置(ex , ...

  8. BFS(三):双向广度优先搜索

    所谓双向广度搜索指的是搜索沿两个方向同时进行:(1)正向搜索:从初始结点向目标结点方向搜索:(2)逆向搜索:从目标结点向初始结点方向搜索:当两个方向的搜索生成同一子结点时终止此搜索过程. 广度双向搜索 ...

  9. POJ 3488 &amp; HDU 1915 Arne Saknussemm(模拟)

    题目链接: POJ:http://poj.org/problem? id=3488 HDU:pid=1915">http://acm.hdu.edu.cn/showproblem.ph ...

随机推荐

  1. c++之RTTI介绍

    本文介绍c++的RTTI的基本用法,并初步研究RTTI的实现原理. 1. 什么是RTTI RTTI即运行时类型识别(runtime type identification),用于判断指针或引用所绑定对 ...

  2. Vim保存文件命令 ":wq" 与 ":x" 的区别

    CSDN转载 [1] Vim是Unix/Linux系统最常用的编辑器之一,在保存文件时,我通常选择":wq",因为最开始学习vim的时候,就只记住了几个常用的命令:也没有细究命令的 ...

  3. Ubuntu下第一个C程序的成功运行

    对于每个新手来说,进入Ubuntu最想做的事莫过于在终端(Terminal)里运行自己的第一个C/C++程序"hello.c/hello.cpp"了. 很多语言书籍都是默认搭载好运 ...

  4. c# 取得ip地址和网关

    /// <summary> /// 得到本机IP /// </summary> private string GetLocalIP() { //本机IP地址 string st ...

  5. 用jQuery解析复杂的xml结构文件

    一个晚上的心血 <?xml version="1.0" encoding="UTF-8"?> <weibo><wbContent& ...

  6. wordpress修改固定链接及修改链接后链接提示404错误的解决办法

    wordpress默认的url实在是不好看又不好记忆,而且还不利于SEO.因此,我就捣鼓着把url做一个自定义.自定义的方式如下: 建议使用/%postname%的形式,这样利于SEO. 修改之后,l ...

  7. 重拾C,一天一点点_5

    switch(表达式){    case 整型常量表达式:语句序列    case 整型常量表达式:语句序列    default:语句序列} while(表达式)    语句 for(表达式1; 表 ...

  8. C#中gridView常用属性和技巧介绍

    .隐藏最上面的GroupPanel gridView1.OptionsView.ShowGroupPanel=false; .得到当前选定记录某字段的值 sValue=Table.Rows[gridV ...

  9. Basic Vlan Concepts

    1.  Vlan Benefit ·To reduce CPU overhead on each device by reducing the number of devices that recei ...

  10. IE中出现 "Stack overflow at line" 错误的解决方法

    在做网站时遇到一个问题,网站用的以前的程序,在没有改过什么程序的情况下,页面总是提示Stack overflow at line 0的错误,而以前的网站都正常没有出现过这种情况,在网上找了一下解决办法 ...