poj1915
#include<iostream>
using namespace std;
#define SIZE 305 int vis[SIZE][SIZE];
int sx,sy,ex,ey;
int dx[]={-,-,-,-,,,,};
int dy[]={-,-,,,,,-,-};
int n,case_num;
typedef struct node
{
int x;
int y;
int step;
}node;
node queue[SIZE*SIZE]; void bfs()
{
int head,tail;
head=tail=;
node startnode;
startnode.x=sx;
startnode.y=sy;
startnode.step=;
queue[tail++]=startnode;
vis[startnode.x][startnode.y]=;
node cur,next;
while(head!=tail)
{ int nx,ny;
cur=queue[head++];
if(cur.x==ex&&cur.y==ey)
{
cout<<cur.step<<endl;
return;
}
for(int i=;i<;i++)
{
nx=cur.x+dx[i];
ny=cur.y+dy[i];
if(nx>=&&nx<n&&ny>=&&ny<n&&vis[nx][ny]==)
{
next.x=nx;
next.y=ny;
next.step=cur.step+;
queue[tail++]=next;
vis[next.x][next.y]=;
}
}
}
}
int main()
{
//freopen("input.txt","r",stdin);
cin>>case_num;
while(case_num--)
{
cin>>n;
cin>>sx>>sy>>ex>>ey;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
vis[i][j]=;
bfs();
}
return ;
}
poj1915的更多相关文章
- poj1915 Knight Moves(BFS)
题目链接 http://poj.org/problem?id=1915 题意 输入正方形棋盘的边长.起点和终点的位置,给定棋子的走法,输出最少经过多少步可以从起点走到终点. 思路 经典bfs题目. 代 ...
- poj1915(双向bfs)
题目链接:https://vjudge.net/problem/POJ-1915 题意:求棋盘上起点到终点最少的步数. 思路:双向广搜模板题,但玄学的是我的代码G++会wa,C++过了,没找到原因QA ...
- poj1915 BFS
D - 广搜 基础 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:30000KB 64bi ...
- 超超超简单的bfs——POJ-1915
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 26102 Accepted: 12305 De ...
- POJ-1915 Knight Moves (BFS)
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 26952 Accepted: 12721 De ...
- BFS广度优先搜索 poj1915
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 25909 Accepted: 12244 Descri ...
- poj2243+poj1915骑士问题
2243是骑士问题,八个格子的,BFS,因为要最短路经,所以没有用A*,A*跑不出来,太慢了,因为要搜索到所有解啊!一直更新最优,而BFS,一层一层搜索,第一次得到的便是最短的了!300格子,标记的话 ...
- POJ1915 BFS&双向BFS
俩月前写的普通BFS #include <cstdio> #include <iostream> #include <cstring> #include <q ...
- poj练习题的方法
poj1010--邮票问题 DFSpoj1011--Sticks dfs + 剪枝poj1020--拼蛋糕poj1054--The Troublesome Frogpoj1062--昂贵的聘礼poj1 ...
随机推荐
- 7系列FPGA的时钟资源——UG472
时钟架构总览 7系的FPGA使用了专用的全局(Global)和区域(Regional)IO和时钟资源来管理设计中各种的时钟需求.Clock Management Tiles(CMT)提供了时钟合成(C ...
- (转)http authorization 基本认证
转:https://www.cnblogs.com/chenrong/articles/5818498.html http协议是无状态的, 浏览器和web服务器之间可以通过cookie来身份识别. 桌 ...
- Java_变量类型
目录 主要是为了复习java相关知识,本文主要内容来自于 http://www.runoob.com/java 一.局部变量 局部变量声明在方法.构造方法或语句块中 在方法.构造方法.语句块被执行的时 ...
- 《剑指offer》连续子数组的最大和
本题来自<剑指offer> 反转链表 题目: 思路: C++ Code: Python Code: 总结:
- Python判断水仙花数
水仙花数 水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI).自恋数.自幂数.阿姆斯壮数或阿姆斯特朗数( ...
- vuejs使用jsx语法
想要vuejs项目支持jsx语法,需要一些插件 babel-plugin-transform-vue-jsx Babel plugin for Vue 2.0 JSX 使用方法: 安装 npm ins ...
- php操作数据库获取到的结果集mysql_result
判断取出的结果集是否为空集: $sql="select adminPwd from adminaccount"; //判断查询是否有数据 if(mysqli_num_rows($r ...
- line-height:1.5和line-height:150%的区别
1. 给你个数,孩子自己算吧 line-height:1.5 父元素设置line-height:1.5会直接继承给子元素,子元素根据自己的font-size再去计算子元素自己的line-height ...
- NEL程序员专用轻钱包 进入0.01状态了
这个轻钱包能干什么,现在就能在测试网看个余额,转个帐,调用个合约. 而且功能非常程序员化 你会说是不是没啥用 但是他有非常有用,因为他可以很容易的拼出NEOGUI拼不出来的交易 比如参与ICO交易 ...
- Torch功能点记录
1. Numpy矩阵转换Tensor: tensor_num = torch.from_numpy(numpy_arr)