最少步数

时间限制:3000 ms  |  内存限制:65535 KB
难度:4

->   Link  <-

这个题深搜广搜都是可以的,迷宫已经给出了,就看怎么做了;一般起点终点确定用广搜求最短路径问题;

广搜就用到队列了,将起点周围的可行的点都加入队列,在从队列中选取点又重复刚才的操作,直到找到终点;

可以用二维数组存起点到此点的最短路径,起点的路径为0;从队列里拿出一个点,其周围可行的点的路径便是这个点的路径加一,一直广搜到终点

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f;
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 sx,sy,gx,gy;
typedef pair<int,int>P;
int d[9][9];
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
void init()
{
for(int i=0;i<=8;i++)
for(int j=0;j<=8;j++)
d[i][j]=INF;
}
int bfs()
{
queue<P>q;
q.push(P(sx,sy));
d[sx][sy]=0;
while(!q.empty())
{
P p=q.front();
q.pop();
if(p.first==gx&&p.second==gy) break;
for(int i=0;i<4;i++)
{
int xx=p.first+dx[i];
int yy=p.second+dy[i];
if(xx>=0&&xx<=8&&yy>=0&&yy<=8&&!MAP[xx][yy]&&d[xx][yy]==INF)
{
q.push(P(xx,yy));
d[xx][yy]=d[p.first][p.second]+1;
}
}
}
return d[gx][gy];
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d",&sx,&sy,&gx,&gy);
init();
printf("%d\n",bfs());
}
return 0;
}

NYOJ-58最少步数,广搜思想!的更多相关文章

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

  2. [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 ...

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

  4. nyoj 613 免费馅饼 广搜

    免费馅饼 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...

  5. NYOJ 53 最少步数

    题      目    http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=58 思路借鉴   DFS-Deep First Search-深度优先 ...

  6. ny 58 最少步数 (BFS)

    题目:http://acm.nyist.net/JudgeOnline/problem.php?pid=58 就是一道简单的BFS 练习练习搜索,一次AC #include <iostream& ...

  7. 南阳理工 58 最少步数 (DFS)

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

  8. NYOJ 483 Nightmare 【广搜】+【无标记】

    版权声明:长风原创 https://blog.csdn.net/u012846486/article/details/31032479 Nightmare 时间限制:1000 ms  |  内存限制: ...

  9. NYOJ 284 坦克大战 (广搜)

    题目链接 描述 Many of us had played the game "Battle city" in our childhood, and some people (li ...

随机推荐

  1. (二)python高级特性

    一.切片 >>> L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack'] 对这种经常取指定索引范围的操作,用循环十分繁琐,因此,Python ...

  2. 人工智能-深度学习(2)TensorFlow安装及基本使用(学习笔记)

    一.TensorFlow 简介 TensorFlow 是 Google 开源的一款人工智能学习系统.为什么叫这个名字呢? Tensor 的意思是张量,代表 N 维数组:Flow 的意思是流,代表基于数 ...

  3. 题解报告:hdu 2072 单词数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072 题目: Problem Description lily的好朋友xiaoou333最近很空,他想 ...

  4. AFNetworking2.5使用-转

    来自:http://blog.csdn.net/daiyelang/article/details/38434023 官网下载2.5版本:http://afnetworking.com/ 此文章是基于 ...

  5. Maximum Subsequence Sum 最大子序列和的进击之路

    本文解决最大子序列和问题,有两个题目组成,第二个题目比第一个要求多一些(其实就是要求输出子序列首尾元素). 01-复杂度1 最大子列和问题   (20分) 给定KK个整数组成的序列{ N1​​, N2 ...

  6. jquery 序列化form表单

    1.为什么要将form表单序列化? ajax上传form表单的原始方式,是将form表单中所需要的键值对先获取,然后再组装成数据(两种方式:http:localhost:8080/test.do?pe ...

  7. redis 可视化管理工具

    Redis Desktop Manager 下载地址:http://redisdesktop.com/download 支持: Windows 7+, Mac OS X 10.10+, Ubuntu ...

  8. CentOS 7 samba server 配置

    samba是linux上的文件共享服务软件,相当与Windows上的共享文件夹,当然也是要在同一网段上的. 当前用的版本是4.4.4,好吧!下面介绍怎么去安装配置它,here we go! 1. 安装 ...

  9. leetcode_654. Maximum Binary Tree

    https://leetcode.com/problems/maximum-binary-tree/ 给定数组A,假设A[i]为数组最大值,创建根节点将其值赋为A[i],然后递归地用A[0,i-1]创 ...

  10. Android(java)学习笔记188:学生信息管理系统案例(SQLite + ListView)

    1.首先说明一个知识点,通常我们显示布局文件xml都是如下: setContentView(R.layout.activity_main): 其实每一个xml布局文件就好像一个气球,我们可以使用Vie ...