POJ 1915
- #include<iostream>
- #include<stdio.h>
- #define MAXN 350
- #include"queue"
- using namespace std;
- bool mark[MAXN][MAXN];
- struct point
- {
- int x;
- int y;
- int step;
- point()
- {
- step = ;
- }
- };
- bool bfs(point p);
- point b;
- int num;
- point e;
- point tem;
- queue<point> coll;
- int main()
- {
- //freopen("acm.acm","r",stdin);
- int test;
- int ans;
- cin>>test;
- while(test --)
- {
- //memset(road,0,sizeof(road));
- memset(mark,false,sizeof(mark));
- cin>>num;
- cin>>b.x>>b.y;
- cin>>e.x>>e.y;
- if(b.x == e.x && b.y == e.y)
- {
- cout<<<<endl;
- continue;
- }
- coll.push(b);
- mark[b.x][b.y] = true;
- // cout<<"----------"<<endl;
- while(!coll.empty() && !bfs(coll.front()) )
- {
- //cout<<"-================"<<endl;
- // cout<<coll.front().x<<endl;
- // cout<<coll.front().y<<endl;
- //break;
- coll.pop();
- }
- while(!coll.empty())
- {
- coll.pop();
- }
- }
- }
- bool bfs(point p)
- {
- if(p.x - >= )
- {
- if(p.y - >= && !mark[p.x - ][p.y - ])
- {
- tem.x = p.x - ;
- tem.y = p.y - ;
- tem.step = p.step + ;
- mark[tem.x][tem.y] = true;
- if(tem.x == e.x && tem.y == e.y)
- {
- cout<<tem.step<<endl;
- return true;
- }
- else
- coll.push(tem);
- }
- if(p.y + < num && !mark[p.x - ][p.y + ])
- {
- tem.x = p.x - ;
- tem.y = p.y + ;
- tem.step = p.step + ;
- mark[tem.x][tem.y] = true;
- if(tem.x == e.x && tem.y == e.y)
- {
- cout<<tem.step<<endl;
- return true;
- }
- else
- coll.push(tem);
- }
- }
- ////////////////
- if(p.x + < num)
- {
- if(p.y - >= && !mark[p.x + ][p.y - ])
- {
- tem.x = p.x + ;
- tem.y = p.y - ;
- tem.step = p.step + ;
- mark[tem.x][tem.y] = true;
- if(tem.x == e.x && tem.y == e.y)
- {
- cout<<tem.step<<endl;
- return true;
- }
- else
- coll.push(tem);
- }
- if(p.y + < num && !mark[p.x + ][p.y + ])
- {
- tem.x = p.x + ;
- tem.y = p.y + ;
- tem.step = p.step + ;
- mark[tem.x][tem.y] = true;
- if(tem.x == e.x && tem.y == e.y)
- {
- cout<<tem.step<<endl;
- return true;
- }
- else
- coll.push(tem);
- }
- }
- ///////////////////
- if(p.y - >= )
- {
- if(p.x - >= && !mark[p.x - ][p.y - ])
- {
- tem.x = p.x - ;
- tem.y = p.y - ;
- tem.step = p.step + ;
- mark[tem.x][tem.y] = true;
- if(tem.x == e.x && tem.y == e.y)
- {
- cout<<tem.step<<endl;
- return true;
- }
- else
- coll.push(tem);
- }
- if(p.x + < num && !mark[p.x + ][p.y - ])
- {
- tem.x = p.x + ;
- tem.y = p.y - ;
- tem.step = p.step + ;
- mark[tem.x][tem.y] = true;
- if(tem.x == e.x && tem.y == e.y)
- {
- cout<<tem.step<<endl;
- return true;
- }
- else
- coll.push(tem);
- }
- }
- //////////////////////
- if(p.y + < num)
- {
- if(p.x - >= && !mark[p.x - ][p.y + ])
- {
- tem.x = p.x - ;
- tem.y = p.y + ;
- tem.step = p.step + ;
- mark[tem.x][tem.y] = true;
- if(tem.x == e.x && tem.y == e.y)
- {
- cout<<tem.step<<endl;
- return true;
- }
- else
- coll.push(tem);
- }
- if(p.x + < num && !mark[p.x + ][p.y + ])
- {
- tem.x = p.x + ;
- tem.y = p.y + ;
- tem.step = p.step + ;
- mark[tem.x][tem.y] = true;
- if(tem.x == e.x && tem.y == e.y)
- {
- cout<<tem.step<<endl;
- return true;
- }
- else
- coll.push(tem);
- }
- }
- return false;
- }
关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。
技术网站地址: vmfor.com
POJ 1915的更多相关文章
- POJ 1915 Knight Moves
POJ 1915 Knight Moves Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 29 ...
- OpenJudge/Poj 1915 Knight Moves
1.链接地址: http://bailian.openjudge.cn/practice/1915 http://poj.org/problem?id=1915 2.题目: 总Time Limit: ...
- poj 1915 http://poj.org/problem?id=1915
/**< */#include <stdio.h> #include <string.h> #include <stdlib.h> #include < ...
- POJ 1915 Knight Moves(BFS+STL)
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20913 Accepted: 9702 ...
- POJ 1915 经典马步 双向bfs
拿这个经典题目开刀...........可是双向时间优势在这题上的效果不太明显 #include <iostream> #include <algorithm> #includ ...
- poj 1915 KnightMoves(bfs)
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24094 Accepted: 11364 De ...
- 论深度优先(DFS)和广度优先搜索(BF)的优点及不足(更新ing)
例题: POJ 1915 Knight Moves 骑士遍历问题(跳马问题) 在一个m*m的棋盘上,从任意一个给定的位置(sx , sy)出发,为象棋中的马找一条路通过最少的步数到达另一位置(ex , ...
- BFS(三):双向广度优先搜索
所谓双向广度搜索指的是搜索沿两个方向同时进行:(1)正向搜索:从初始结点向目标结点方向搜索:(2)逆向搜索:从目标结点向初始结点方向搜索:当两个方向的搜索生成同一子结点时终止此搜索过程. 广度双向搜索 ...
- POJ 3488 & HDU 1915 Arne Saknussemm(模拟)
题目链接: POJ:http://poj.org/problem? id=3488 HDU:pid=1915">http://acm.hdu.edu.cn/showproblem.ph ...
随机推荐
- c++之RTTI介绍
本文介绍c++的RTTI的基本用法,并初步研究RTTI的实现原理. 1. 什么是RTTI RTTI即运行时类型识别(runtime type identification),用于判断指针或引用所绑定对 ...
- Vim保存文件命令 ":wq" 与 ":x" 的区别
CSDN转载 [1] Vim是Unix/Linux系统最常用的编辑器之一,在保存文件时,我通常选择":wq",因为最开始学习vim的时候,就只记住了几个常用的命令:也没有细究命令的 ...
- Ubuntu下第一个C程序的成功运行
对于每个新手来说,进入Ubuntu最想做的事莫过于在终端(Terminal)里运行自己的第一个C/C++程序"hello.c/hello.cpp"了. 很多语言书籍都是默认搭载好运 ...
- c# 取得ip地址和网关
/// <summary> /// 得到本机IP /// </summary> private string GetLocalIP() { //本机IP地址 string st ...
- 用jQuery解析复杂的xml结构文件
一个晚上的心血 <?xml version="1.0" encoding="UTF-8"?> <weibo><wbContent& ...
- wordpress修改固定链接及修改链接后链接提示404错误的解决办法
wordpress默认的url实在是不好看又不好记忆,而且还不利于SEO.因此,我就捣鼓着把url做一个自定义.自定义的方式如下: 建议使用/%postname%的形式,这样利于SEO. 修改之后,l ...
- 重拾C,一天一点点_5
switch(表达式){ case 整型常量表达式:语句序列 case 整型常量表达式:语句序列 default:语句序列} while(表达式) 语句 for(表达式1; 表 ...
- C#中gridView常用属性和技巧介绍
.隐藏最上面的GroupPanel gridView1.OptionsView.ShowGroupPanel=false; .得到当前选定记录某字段的值 sValue=Table.Rows[gridV ...
- Basic Vlan Concepts
1. Vlan Benefit ·To reduce CPU overhead on each device by reducing the number of devices that recei ...
- IE中出现 "Stack overflow at line" 错误的解决方法
在做网站时遇到一个问题,网站用的以前的程序,在没有改过什么程序的情况下,页面总是提示Stack overflow at line 0的错误,而以前的网站都正常没有出现过这种情况,在网上找了一下解决办法 ...