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 ...
随机推荐
- 【原创】大数据基础之ORC(1)简介
https://orc.apache.org Optimized Row Columnar (ORC) file 行列混合存储 层次结构: file -> stripes -> row g ...
- Nginx配置https证书
目前的大趋势是升级HTTP为HTTPS 本章介绍怎样实装HTTPS证书 # 如果报 ssl 错误是Nginx安装时未安装ssl 请重新编译nginx 可以参考我之前的博客 申请/获取https 这里就 ...
- Windows Internals 笔记——进程的权限
1.大多数用户都用一个管理员账户来登录Windows,在Vista之前,这样的登录会创建一个安全令牌.每当有代码试图使用一个受保护的安全资源时,操作系统就会出示这个令牌.从包括Windows资源管理器 ...
- python全栈开发day112-CBV、flask_session、WTForms
1.Flask 中的 CBV class Index(views.MethodView): # methods = ["POST"] # decorators = [war,nei ...
- PostgreSQL自学笔记:3 数据库的基本操作
3 数据库的基本操作 3.1 创建数据库 3.1.1 使用对象浏览器创建数据库 [Server] -> PostgreSQL 9.6 -> 数据库,右击 -> 创建 通常: 数据库: ...
- composer install 时遇到 Composer\Downloader\TransportException ...
安装错误 [Composer\Downloader\TransportException] Invalid credentials for 'https://packagist.phpcomposer ...
- RAID 0 ~ RAID 7
一.RAID解析1.RAID 0(1)将几块磁盘并行组合,横向写数据(2)并发IO,写数据最快缺点:不提供数据冗余,如果其中一块磁盘废掉,则数据全毁详细:条带深度大时,一次IO只能从一块磁盘上读取,无 ...
- DWM1000 巧用Status 快速Debug
在Debug DWM1000 的时候,可以巧用Status 加快Debug,例如如下代码 if (status_reg & SYS_STATUS_RXFCG) { …… } else { sp ...
- C# ToString()和Convert.ToString()的区别【转】
一.一般用法说明 ToString()是Object的扩展方法,所以都有ToString()方法;而Convert.ToString(param)(其中param参数的数据类型可以是各种基本数据类型, ...
- 对于服务器AdminServer, 与计算机Machine-0相关联的节点管理器无法访问
控制台启动server时报"对于服务器server-1与计算机machin<!--StartFragment -->对于服务器AdminServer, 与计算机Machine-0 ...