迷宫 (BFS)
《挑战程序设计》 P34
第一次使用pair
1.头文件:<utility>
2.成员:mypair.first, mypair.second
3.运算符:<、>、<=、>=、==、!=,其规则是先比较first,first相等时再比较second
4.相关函数:make_pair 例如:p=make_pair(1,1); q.push(make_pair(1,1));
#include <iostream>
#include <utility>
#include <cstdio>
#include <queue>
using namespace std;
const int INF = 100000000;
typedef pair<int, int> P;
const int N = 100;
char maze[N][N + 1];
int n, m;
int sx, sy;
int gx, gy;
int d[N][N]; //记录到各个位置的最短距离
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
int bfs()
{
queue<P> que;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
d[i][j] = INF;
que.push(P(sx, sy));
d[sx][sy] = 0;
while (que.size()) {
P p = que.front();
que.pop();
if (p.first == gx && p.second == gy) break;
for (int i = 0; i < 4; ++i) {
int nx = p.first + dx[i];
int ny = p.second + dy[i];
if (nx < n && nx >= 0 && ny < m && ny >= 0 &&
maze[nx][ny] != '#' && d[nx][ny] == INF) {
d[nx][ny] = d[p.first][p.second] + 1;
que.push(P(nx, ny));
}
}
}
return d[gx][gy];
}
void solve()
{
scanf("%d%d", &n, &m);
for (int i = 0; i < n; ++i) {
getchar();
for (int j = 0; j < m; ++j) {
scanf("%c", &maze[i][j]);
if (maze[i][j] == 'S') sx = i, sy = j;
if (maze[i][j] == 'G') gx = i, gy = j;
}
}
int res = bfs();
printf("%d\n", res);
}
int main()
{
solve();
return 0;
}
/****************
Input:
10 10
#S######.#
......#..#
.#.##.##.#
.#........
##.##.####
....#....#
.#######.#
....#.....
.####.###.
....#...G#
Output:
22
*****************/
迷宫 (BFS)的更多相关文章
- ZOJ 1649 Rescue(有敌人迷宫BFS)
题意 求迷宫中从a的位置到r的位置须要的最少时间 经过'.'方格须要1s 经过'x'方格须要两秒 '#'表示墙 因为有1s和2s两种情况 须要在基础迷宫bfs上加些推断 令到达每一个点的时间初 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- poj 1383 Labyrinth【迷宫bfs+树的直径】
Labyrinth Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 4004 Accepted: 1504 Descrip ...
- hdu_1728_逃离迷宫(bfs)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1728 题意:走迷宫,找最小的拐角 题解:对BFS有了新的理解,DFS+剪枝应该也能过,用BFS就要以拐 ...
- hdu 1728 逃离迷宫 (BFS)
逃离迷宫 Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissi ...
- HDU1728-逃离迷宫-BFS
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 迷宫-BFS
迷宫问题 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Descript ...
- HDU 1026(迷宫 BFS+打印)
题意是要穿过一个迷宫并且将每一步打印出来. 用宽搜的方法找到路径,在 vis 中存一下方向,只是这题被看到的一种不太对的运算符重载坑了很久...... 代码如下: #include <bits/ ...
- Applese走迷宫-bfs
链接:https://ac.nowcoder.com/acm/contest/330/C来源:牛客网 题目描述 精通程序设计的 Applese 双写了一个游戏. 在这个游戏中,它被困在了一个 n×mn ...
- hdu 1728 逃离迷宫 bfs记转向
题链:http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Mem ...
随机推荐
- 一步步学习NHibernate(1)——NHibernate介绍
请注明转载地址:http://www.cnblogs.com/arhat 第十五章 从本章开始,老魏将给大家一起学习NHibernate这个流行的ORM框架,本来老魏想要和大家一起探讨微软的EF框架的 ...
- 蓝桥杯——FJ字符串
FJ在沙盘上写了这样一些字符串: A1 = "A"1 A2 = "ABA"3 A3 = "ABACABA"7 A4 = "ABAC ...
- win2003 sp2+iis 6.0上部署.net 2.0和.net 4.0网站的方法
网站环境 IIS6.0,操作系统Windows server2003 sp2,服务器之前已经部署了.net 2.0和asp的网站,现在要部署新开发的.net 4.0网站.本来认为很简单,却遇到了很多问 ...
- 读取Excel列内容
http://blog.sina.com.cn/s/blog_6e001be701016yi8.html
- opencv 操作本地摄像头实现录像
直接上代码: // demo1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> usin ...
- portlet初学习及HelloWorld例子
1. 在myeclipse中新建一个web project,在src中新建如下类: package com.yoyo.portlet; import java.io.IOException; impo ...
- chmod,chown和chgrp的区别
1.chgrp(转变文件所属用户组)change group chgrp 用户组 文件名 ###便是这个格了.若是整个目次下的都改,则加-R参数用于递归. 如:chgrp -R user smb.co ...
- django的url的name参数的意义(转发)
http://bio.rusaer.com/archives/288 Django一个比较隐含的函数url 阅读量(5010) | 发表 于 2010-03-09 14:26:18 Djang ...
- 用PYTHON硬写SOCKET
这个文章的系列很有意思,练练~~: http://python.jobbole.com/82763/ :) 第一步,最简单的SERVER和CLIENT.感觉和写JAVA,C的最简单的一样一样的,,看来 ...
- 169. Majority Element
题目: Given an array of size n, find the majority element. The majority element is the element that ap ...