ZOJ 1649 Rescue(有敌人迷宫BFS)
题意 求迷宫中从a的位置到r的位置须要的最少时间 经过'.'方格须要1s 经过‘x’方格须要两秒 '#'表示墙
因为有1s和2s两种情况 须要在基础迷宫bfs上加些推断
令到达每一个点的时间初始为无穷大 当从一个点到达该点用的时间比他本来的时间小时 更新这个点的时间并将这个点入队 扫描全然图就得到答案咯
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std; const int N = 205;
char mat[N][N];
int time[N][N], sx, sy;
int dx[4] = {0, 0, -1, 1};
int dy[4] = { -1, 1, 0, 0};
struct grid
{
int x, y;
grid(int xx = 0, int yy = 0): x(xx), y(yy) {}
}; void bfs()
{
memset(time, 0x3f, sizeof(time));
time[sx][sy] = 0;
queue<grid> g;
g.push(grid(sx, sy)); while(!g.empty())
{
grid cur = g.front();
g.pop();
int cx = cur.x, cy = cur.y, ct = time[cx][cy];
for(int i = 0; i < 4; ++i)
{
int nx = cx + dx[i], ny = cy + dy[i];
if(mat[nx][ny] && mat[nx][ny] != '#')
{
int tt = ct + 1;
if(mat[cx][cy] == 'x') ++tt;
if(tt < time[nx][ny])
{
time[nx][ny] = tt;
g.push(grid(nx, ny));
}
}
}
}
} int main()
{
int n, m, ex, ey;
while (~scanf("%d%d", &n, &m))
{
memset(mat, 0, sizeof(mat));
for(int i = 1; i <= n; ++i)
scanf("%s", mat[i] + 1);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
if(mat[i][j] == 'a') sx = i, sy = j;
else if(mat[i][j] == 'r') ex = i, ey = j;
bfs();
if(time[ex][ey] != time[0][0])
printf("%d\n", time[ex][ey]);
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
} return 0;
}
ZOJ 1649 Rescue(有敌人迷宫BFS)的更多相关文章
- zoj 1649 Rescue (BFS)(转载)
又是类似骑士拯救公主,不过这个是朋友拯救天使的故事... 不同的是,天使有多个朋友,而骑士一般单枪匹马比较帅~ 求到达天使的最短时间,杀死一个护卫1 units time , 走一个格子 1 unit ...
- zoj 1649 Rescue
BFS..第一次使用C++ STL的队列来写广搜. #include<stdio.h> #include<string.h> #include<math.h> #i ...
- BFS zoj 1649
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 //hnldyhy(303882171) 11:12:46 // z ...
- ZOJ 1649:Rescue(BFS)
Rescue Time Limit: 2 Seconds Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...
- HDU 1242 Rescue(BFS),ZOJ 1649
题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...
- ZOJ 649 Rescue(优先队列+bfs)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- zoj 1649 bfs
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M ...
- 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 ...
随机推荐
- python修改植物僵尸
import win32process#进程模块 import win32con#系统定义 import win32api#调用系统模块 import ctypes#C语言类型 import win3 ...
- POJ 3083 BFS+DFS 40行
题意:给你一个迷宫. 先输出当左转优先的时候走的路程长度,再输出当右转优先时走的路程长度,最后输出从起点到终点的最短路程长度. 嗯嗯 奴哥活跃气氛的题.随便写了写.. 此题 知道了思路以后就是水题了. ...
- SyntaxError: EOL while scanning string literal的解决
2281 python中字符串的最后一个字符是斜杠会导致出错:SyntaxError: EOL while scanning string literal [背景] Python 2.7.2 中想要通 ...
- 【sqli-labs】 less41 GET -Blind based -Intiger -Stacked(GET型基于盲注的堆叠查询整型注入)
整型的不用闭合引号 http://192.168.136.128/sqli-labs-master/Less-41/?id=1;insert into users(id,username,passwo ...
- Arduino LM35温度检测
一. 接线原理图 二.实物图 三.代码例子
- Shiro Shiro Web Support and EnvironmentLoaderListener
Shiro Shiro Web Support 主要参考: http://shiro.apache.org/web.html 还有涛哥的 作为资源控制访问的事情,主要使用在网络后台方面,所以了解了本地 ...
- javaee 用Buffered进行对象的写入和读取
package Zjshuchu; import java.io.Serializable; public class Dog implements Serializable{ private ...
- eas之创建一个UI界面并对其操作
private void BranchAddNew(ActionEvent e) { UIContext uiContext = new UIContext(this); ui ...
- C#第八节课
for的穷举迭代.while.do while using System;using System.Collections.Generic;using System.Linq;using Syste ...
- [luogu3244 SHOI2016] 黑暗前的幻想乡(容斥原理+矩阵树定理)
传送门 Description 给出 n 个点和 n−1 种颜色,每种颜色有若干条边.求这张图多少棵每种颜色的边都出现过的生成树,答案对 109+7 取模. Input 第一行包含一个正整数 N(N& ...