意甲冠军:不解释。

策略:如果;

这个问题也可以用深宽搜索搜索中使用。我曾经写过,使用深层搜索。最近的学校范围内的搜索,拿这个问题来试试你的手。

代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using std::queue;
bool vis[20][20];
const int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};//方向
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,
};
struct Node{
int pos[2];//pos[0] = x, pos[1] = y
int step;
};
Node st, en;
bool match(Node a, Node b) //推断是不是到达终点
{
return (a.pos[0] == b.pos[0]&&a.pos[1] == b.pos[1]);
}
int bfs()
{
queue<Node> q;
int i, j;
memset(vis, 0, sizeof(vis));
q.push(st);
vis[st.pos[0]][st.pos[1]] = 1;
int ans = 0x3f3f3f3f; //初始化
while(!q.empty()){
Node u = q.front();
if(match(u, en)){ //wa了一次是由于没有推断终点是不是起点
ans = u.step;
break;
}
for(i = 0; i < 4; i ++){
Node v;
v.pos[0] = u.pos[0]+dir[i][0];
v.pos[1] = u.pos[1]+dir[i][1];
v.step = u.step+1;
if(match(v, en)){
if(v.step < ans)
ans = v.step;
}
else if(!vis[v.pos[0]][v.pos[1]]&&!map[v.pos[0]][v.pos[1]]){
q.push(v);
vis[v.pos[0]][v.pos[1]] = 1;
}
}
q.pop();
}
return ans;
}
int main()
{
int t;
scanf("%d", &t);
while(t --){
scanf("%d%d%d%d", &st.pos[0], &st.pos[1], &en.pos[0], &en.pos[1]);
st.step = 0;
printf("%d\n", bfs());
}
return 0;
}

主题链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=58

NYOJ 58 步数最少 【BFS】的更多相关文章

  1. SDUT OJ 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)

    数据结构实验之图论五:从起始点到目标点的最短步数(BFS) Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss P ...

  2. SDUT-2139_从起始点到目标点的最短步数(BFS)

    数据结构实验之图论五:从起始点到目标点的最短步数(BFS) Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 在古老的魔兽 ...

  3. 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 ...

  4. 最少步数(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 ...

  5. NYOJ_58最少步数(queue+BFS)

    描写叙述 这有一个迷宫,有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   ...

  6. hdu 1245 Saving James Bond 策画几何+最短路 最短路求步数最少的路径

    #include<stdio.h> #include<string.h> #include<math.h> #define inf 0x3fffffff #defi ...

  7. nyoj 21-三个水杯(BFS)

    21-三个水杯 内存限制:64MB 时间限制:1000ms Special Judge: No accepted:7 submit:18 题目描述: 给出三个水杯,大小不一,并且只有最大的水杯的水是装 ...

  8. nyoj 483 Nightmare【bfs+优先队列】

    Nightmare 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 Ignatius had a nightmare last night. He found him ...

  9. nyoj三个水杯(bfs)

    三个水杯 时间限制:1000 ms  |           内存限制:65535 KB 难度:4   描述 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子.三个水杯之间相互 ...

随机推荐

  1. ASP.Net中使用XMLDataSource

    在Web开发中,程序和数据库打交道是常有的事情.在平时使用过程中,使用较多的是MS SQLSERVER,因此经常用到SQLDataSource将数据绑定的数据控件上.有时数据量较小,无需要在数据库中创 ...

  2. CIC 抽取滤波器 Verilog Code

    采用流水线结构的CIC 抽取滤波器结构如下: // 三级CIC抽取器实例:cic3_decimator.V module cic3_decimator(clk, x_in, y_out); param ...

  3. DTD验证XML(转)

    1.内部DTD       最简单的使用DTD的方法是在XML文件的序言部分加入一个DTD描述,加入的位置是紧接在XML处理指示之后.一个包含DTD的XML文件的结构为:    <?xml ve ...

  4. Ansible@一个有效的配置管理工具--Ansible configure management--翻译(十)

    未经书面许可,.请勿转载 Custom Modules Until now we have been working solely with the tools provided to us by A ...

  5. 【转】angularjs指令中的compile与link函数详解

    这篇文章主要介绍了angularjs指令中的compile与link函数详解,本文同时诉大家complie,pre-link,post-link的用法与区别等内容,需要的朋友可以参考下   通常大家在 ...

  6. Python 基于学习 网络小爬虫

    <span style="font-size:18px;"># # 百度贴吧图片网络小爬虫 # import re import urllib def getHtml( ...

  7. decimal system 2016

    Problem Description As we know , we always use the decimal system in our common life, even using the ...

  8. CQRS

    CQRS 2015-06-04 15:33 by 敏捷的水, 177 阅读, 0 评论, 收藏, 编辑 CQRS是Command Query Responsibility Seperation(命令查 ...

  9. hdu Text Reverse

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062 单词翻转! 代码: #include <stdio.h> #include < ...

  10. Java高效编程(2) -- Creating and Destroying Objects

    Item 1: Consider static factory methods instead of constructors Advantage: One advantage of static f ...