描写叙述

这有一个迷宫,有0~8行和0~8列:

1,1,1,1,1,1,1,1,1

 1,0,0,1,0,0,1,0,1

 1,0,0,1,1,0,0,0,1

 1,0,1,0,1,1,0,1,1

 1,0,0,0,0,1,0,0,1

 1,1,0,1,0,1,0,0,1

 1,1,0,1,0,1,0,0,1

 1,1,0,1,0,0,0,0,1

 1,1,1,1,1,1,1,1,1

0表示道路。1表示墙。

如今输入一个道路的坐标作为起点,再如输入一个道路的坐标作为终点,问最少走几步才干从起点到达终点?

(注:一步是指从一坐标点走到其上下左右相邻坐标点。如:从(3。1)到(4,1)。)

输入
第一行输入一个整数n(0<n<=100),表示有n组測试数据;

随后n行,每行有四个整数a,b,c,d(0<=a,b,c,d<=8)分别表示起点的行、列。终点的行、列。
输出
输出最少走几步。
例子输入
2
3 1 5 7
3 1 6 7
例子输出
12
11

#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std; int map[9][9] = {
1,1,1,1,1,1,1,1,1,
1,0,0,1,0,0,1,0,1,
1,0,0,1,1,0,0,0,1,
1,0,1,0,1,1,0,1,1,
1,0,0,0,0,1,0,0,1,
1,1,0,1,0,1,0,0,1,
1,1,0,1,0,1,0,0,1,
1,1,0,1,0,0,0,0,1,
1,1,1,1,1,1,1,1,1
} ;
int vis[9][9], dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
struct Node
{
int x, y;
int num;
};
queue<Node>q;
int x1,x2,y1,y2; void bfs()
{
while(!q.empty())
{
Node temp, node;
node = q.front();
q.pop(); //赋值后抛去首元素
for(int i = 0; i < 4; i++)
{
temp.num = node.num + 1;
temp.x = node.x + dir[i][0];
temp.y = node.y + dir[i][1];
if(vis[temp.x][temp.y] || map[temp.x][temp.y] ) //推断当前节点是否被訪问过,是否有路
continue;
if(temp.x == x2 && temp.y == y2)
{
cout<<temp.num<<endl;
return ;
}
vis[temp.x][temp.y] = 1;
q.push(temp); //该节点没被訪问过且有路,插入队列
}
}
} int main()
{
int n;
//freopen("d:\\test.txt","r",stdin);
cin>>n;
while(n--)
{
while(!q.empty()) q.pop();
memset(vis, 0, sizeof(vis));
cin>>x1>>y1>>x2>>y2;
if(x1 == x2 && y1 == y2)
{
cout<<"0"<<endl;
continue ;
}
Node node;
node.x = x1;
node.y = y1;
node.num = 0;
q.push(node); //(x1,y1)入队
bfs();
}
//fclose(stdin);
return 0;
}

NYOJ_58最少步数(queue+BFS)的更多相关文章

  1. 最少步数(bfs)

    最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...

  2. 最少步数(dfs + bfs +bfs优化)

    最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...

  3. 最少步数(bfs)

    最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...

  4. POJ 3126 Prime Path【从一个素数变为另一个素数的最少步数/BFS】

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26475 Accepted: 14555 Descript ...

  5. ACM 最少步数

    最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...

  6. [ACM_搜索] ZOJ 1103 || POJ 2415 Hike on a Graph (带条件移动3盘子到同一位置的最少步数 广搜)

    Description "Hike on a Graph" is a game that is played on a board on which an undirected g ...

  7. NYOJ 58 最少步数

    最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...

  8. nyoj 1022 最少步数【优先队列+广搜】

    最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...

  9. T1330 最少步数(#Ⅱ- 8)(广度优先搜索)

    [题目描述] 在各种棋中,棋子的走法总是一定的,如中国象棋中马走“日”.有一位小学生就想如果马能有两种走法将增加其趣味性,因此,他规定马既能按“日”走,也能如象一样走“田”字.他的同桌平时喜欢下围棋, ...

随机推荐

  1. dubbo之服务容器

    服务容器是一个standalone的启动程序,因为后台服务不需要Tomcat或JBoss等Web容器的功能,如果硬要用Web容器去加载服务提供方,增加复杂性,也浪费资源. 服务容器只是一个简单的Mai ...

  2. (转)Hibernate框架基础——一对多关联关系映射

    http://blog.csdn.net/yerenyuan_pku/article/details/52746413 上一篇文章Hibernate框架基础——映射集合属性详细讲解的是值类型的集合(即 ...

  3. context switch

    In computing, a context switch is the process of storing and restoring the state (more specifically, ...

  4. bzoj3940 censoring 题解(AC自动机)

    题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they h ...

  5. 日常开发需要掌握的Maven知识

    文章来自:https://www.jianshu.com/p/e224a6dc8f20和https://www.jianshu.com/p/20b39ab6a88c Maven出现之前 jar包默认都 ...

  6. go new() 和 make() 的区别

    看起来二者没有什么区别,都在堆上分配内存,但是它们的行为不同,适用于不同的类型. new(T) 为每个新的类型T分配一片内存,初始化为 0 并且返回类型为*T的内存地址:这种方法 返回一个指向类型为 ...

  7. S-HR之代码创建临时表并插入数据

    ... private String tempTab1 = null; //临时表EcirrWithPPTempTable public String getTempTable() { String ...

  8. 洛谷——P3373 【模板】线段树 2&& B 数据结构

    P3373 [模板]线段树 2 题目描述 如题,已知一个数列,你需要进行下面三种操作: 1.将某区间每一个数乘上x 2.将某区间每一个数加上x 3.求出某区间每一个数的和 线段树维护区间乘法 1.如何 ...

  9. Luogu P4014 「 网络流 24 题 」分配问题

    解题思路 还是建立超级源点和超级汇点,又因为题目给出规定一个人只能修一个工件,所以建图的时候还要讲容量都设为$1$. 人的编号是$1\rightarrow n$,工件的编号是$n+1\rightarr ...

  10. C/C++格式化输入,输出

    C/C++格式化输入,输出 1.C语言 1. 语言函数 scanf(); printf(); sscanf() --> 不安全 sscanf_s() ---> 安全 sprintf() - ...