《挑战程序设计》 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)的更多相关文章

  1. ZOJ 1649 Rescue(有敌人迷宫BFS)

    题意 求迷宫中从a的位置到r的位置须要的最少时间  经过'.'方格须要1s  经过'x'方格须要两秒  '#'表示墙 因为有1s和2s两种情况  须要在基础迷宫bfs上加些推断 令到达每一个点的时间初 ...

  2. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  3. poj 1383 Labyrinth【迷宫bfs+树的直径】

    Labyrinth Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 4004   Accepted: 1504 Descrip ...

  4. hdu_1728_逃离迷宫(bfs)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1728 题意:走迷宫,找最小的拐角 题解:对BFS有了新的理解,DFS+剪枝应该也能过,用BFS就要以拐 ...

  5. hdu 1728 逃离迷宫 (BFS)

    逃离迷宫 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...

  6. HDU1728-逃离迷宫-BFS

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. 迷宫-BFS

    迷宫问题 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Descript ...

  8. HDU 1026(迷宫 BFS+打印)

    题意是要穿过一个迷宫并且将每一步打印出来. 用宽搜的方法找到路径,在 vis 中存一下方向,只是这题被看到的一种不太对的运算符重载坑了很久...... 代码如下: #include <bits/ ...

  9. Applese走迷宫-bfs

    链接:https://ac.nowcoder.com/acm/contest/330/C来源:牛客网 题目描述 精通程序设计的 Applese 双写了一个游戏. 在这个游戏中,它被困在了一个 n×mn ...

  10. hdu 1728 逃离迷宫 bfs记转向

    题链:http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Mem ...

随机推荐

  1. hadoop完全分布式安装(转)

    1 安装Vmware WorkStation软件 有些人会问,为何要安装这个软件,这是一个VM公司提供的虚拟机工作平台,后面需要在这个平台上安装linux操作系统.具体安装过程网上有很多资料,这里不作 ...

  2. sencha touch

    download http://www.sencha.com/products/touch/thank-you/ Developer Center http://developer.sencha.co ...

  3. JS数据类型&&typeof&&其他

    1. 5种基本数据类型: 1. String 2. Number 3. Boolean 4. Undefined 5. Null 2. 1种复杂数据类型:Object 3. 检测变量的数据类型:typ ...

  4. bzoj 4004: [JLOI2015]装备购买 拟阵 && 高消

    4004: [JLOI2015]装备购买 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 337  Solved: 139[Submit][Status ...

  5. [Gauss]POJ1753 Flip Game

    题意:给4×4的棋盘的初始状态,b代表黑,w代表白. 要求变成全黑或者全白 最少需要几步. 简单的做法 可以暴搜 状压bfs 不再赘述 主要学习Gauss做法 同样是01方程组 用异或解 注意全黑或全 ...

  6. 【Linux远程管理】Telnet远程连接管理

    Telnet,命令行界面下的远程管理工具,因为其历史非常悠久,几乎所有的操作系统都有该工具, 但是,Telnet在传输数据是是通过明文传输的,没有加密,所以现在几乎不会使用Telnet进行管理了. ( ...

  7. 关于checkbox的checked属性和change事件

    jquery中的attr和prop有什么区别? To retrieve and change DOM properties such as the checked, selected, or disa ...

  8. Java 输入流读取文本文件换行符问题

    一问题 在学习流编程的过程中,我遇到了一下问题.首先来看一下我写的java源程序: package StreamLearn; import java.io.*; public class TestFi ...

  9. BZOJ3166: [Heoi2013]Alo

    3166: [Heoi2013]Alo Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 394  Solved: 204[Submit][Status] ...

  10. [转] acmer必看的26个对acm态度

    acmer必看的26个对acm态度   转载自:http://www.cppblog.com/Darren/archive/2009/08/03/92099.html Attempt Keep on ...