最少步数

时间限制: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
 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 <vector>
#include <cstring>
#include <utility>
#include <queue>
using namespace std;
typedef pair<int,int> Point; const int maze[][] ={
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
,,,,,,,,,
};
bool visit[][];
const int dx[] = {,,,-};
const int dy[] = {,,-,}; int bfs(Point startP,Point endP){
queue<Point> p;
p.push(startP);
visit[startP.first][startP.second] = true;
int res = ,cnt = ,newCnt = ;
while(!p.empty()){
cnt = newCnt;
newCnt = ;
while(cnt--){
Point tmp = p.front(); p.pop();
if(tmp.first == endP.first && tmp.second == endP.second) return res;
else{
for(int i = ; i < ; ++ i){
int newx = tmp.first + dx[i], newy = tmp.second + dy[i];
if(!visit[newx][newy] && !maze[newx][newy]){
p.push(Point(newx,newy));
visit[newx][newy] = true;
newCnt++;
}
}
}
}
++res;
}
return -;
} int main(){
int n;
cin >> n;
while(n--){
int a,b,c,d;
cin >> a >> b >> c >> d;
memset(visit,false,sizeof(visit));
cout<<bfs(Point(a,b),Point(c,d))<<endl; }
}

ACM 最少步数的更多相关文章

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

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

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

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

  8. NYOJ-58最少步数,广搜思想!

    最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 ->   Link  <- 这个题深搜广搜都是可以的,迷宫已经给出了,就看怎么做了:一般起点终点确定用广搜 ...

  9. 最少步数&P1443 马的遍历

      1330:[例8.3]最少步数 s数组:记录(1,1)到达每一点需要的最少步数 s[1][1]自然为 0,其余初始化为 -1 que数组:que[#][1] 表示(1,1)可到达点的 x 坐标 q ...

随机推荐

  1. max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]

    sh- /etc/sysctl.conf vm.max_map_count = #在/etc/sysctl.conf追加上面一条 #并执行命令: sysctl -p

  2. jQuery each用法及each解析json

    $(function(){ $("button").click( function(){ var a1=""; var a2=""; var ...

  3. SpringBoot Jms

    https://dzone.com/articles/spring-boot-example-of-spring-integration-and-acti

  4. 与你相遇好幸运,Sails.js安装

    官网: http://sailsjs.org Github:https://github.com/balderdashy/sails 开发文档: http://sailsjs.org/document ...

  5. IBM Rational AppScan 无法记录登录序列 分类: 数据安全 2015-03-18 16:46 158人阅读 评论(0) 收藏

    为了测试漏洞,我在本地部署了一个站点,为http://localhost/app,并且有登录页面. 但是尝试多次,都无法记录登录页面.此时尝试了在hosts文件中,自定义了一个域名 127.0.0.1 ...

  6. C#中var和dynamic

    var与dynamic这两个关键字,只是看起来很相似,仅此而已!var表示“变量的类型是在编译时决定的”,但是dynamic表 示“变量的类型是在运行时决定的”.因此,dynamic与var具有截然不 ...

  7. 使用MulticastSocket实现多点广播

    原文链接:http://hbiao68.iteye.com/blog/1943354 使用MulticastSocket实现多点广播 DatagramSocket只允许数据报发送给指定的目标地址,而M ...

  8. 删除表数据drop、truncate和delete的用法

    说到删除表数据的关键字,大家记得最多的可能就是delete了 然而我们做数据库开发,读取数据库数据.对另外的两兄弟用得就比较少了 现在来介绍另外两个兄弟,都是删除表数据的,其实也是很容易理解的 老大- ...

  9. LoadRunner 脚本学习 -- 读取文件内容

    随便创建个txt文档  输入点内容,例如 读取文件内前N个字符: Action() { long myfile; ; ]; char *filename = "E:\\kkk.txt&quo ...

  10. Uva10328 dp(递推+高精度)

    题目链接:http://vjudge.net/contest/136499#problem/F 题意:给你一个硬币,抛掷n次,问出现连续至少k个正面向上的情况有多少种. 一个比较好理解的题解:原题中问 ...