《挑战程序设计》 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. NGUI系列教程五(角色信息跟随)

    在一些网络游戏中,我们常常可以看到角色的上方显示着角色的名称,等级,血量等信息.它们可以跟随角色移动,并且可以显示和隐藏.今天我们就来学习一下这些功能的实现方法.1. 新建unity工 程,导入NGU ...

  2. Oracle中的触发器

    创建触发器的语法: Create trigger 触发器的名字 after insert/update/delete/(select是没有触发器的) on 表名字 declare begin dbms ...

  3. jquery ashx

    http://www.cnblogs.com/wzcheng/archive/2010/05/20/1739810.html http://www.cnblogs.com/yyl8781697/arc ...

  4. HDU 2986 Ballot evaluation(精度问题)

    点我看题目 题意 : 给你n个人名,每个名后边跟着一个数,然后m个式子,判断是否正确. 思路 :算是一个模拟吧,但是要注意浮点数容易丢失精度,所以要好好处理精度,不知道多少人死在精度上,不过我实在是不 ...

  5. CF Codeforces Round #231 (Div. 2)

    http://codeforces.com/contest/394 话说这次CF做的超级不爽,A题一开始交过了,我就没再管,B题还没看完呢,就死困死困的,后来觉得B题枚举一下估计能行,当时是觉得可以从 ...

  6. 158. Read N Characters Given Read4 II - Call multiple times

    题目: The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the ...

  7. Android 在AlertDialog里添加布局控件

    android里很多时候需要在弹出的AlertDialog里有自己的控件,填写信息,比如弹出一个登陆对话框 那么首先你就要创建这么一个布局的inputphonenum.xml文件了 <?xml ...

  8. 【HDOJ】3480 Division

    斜率dp+滚动数组. /* 3480 */ #include <iostream> #include <sstream> #include <string> #in ...

  9. 如何kill掉TaobaoProtect.exe

    C:\Users\Administrator\AppData\Roaming\TaobaoProtect TaobaoProtect.exe https://technet.microsoft.com ...

  10. JAVA - Blowfish加密出现java.security.InvalidKeyException: Illegal key size 解决方案

    最近用java进行一个blowfish的加密算法,但是在我们的eclipse上报出Illegal key size的错误.google后发现原因是:ymmetricDS加密symmetric.prop ...