AOJ 0558 广度优先搜索
题意:在 H * W 的地图里有 N 个工厂,每个工厂分别生产硬度为1-N 的奶酪,有一只老鼠准备把所有奶酪都吃完。老鼠的初始体力值为1,每吃一个奶酪体力值加 1。已知老鼠不能吃硬度大于当前体力值的奶酪,老鼠只能向上下左右四个方向走,求吃完所有奶酪老鼠需要经过的最小步数。
分析:简单迷宫问题。不同的是,老鼠需要按1-N 的顺序把奶酪吃完。用广度优先搜索很容易求出起点到终点的最小步数。初始时,求起点到硬度值为 1 的奶酪的最小步数;接着将起点重置为此位置,继续求此位置到达硬度值为 2 的奶酪;如此类推。因此这里只需做N 次广度优先搜索,并累计其值即可。
C++代码:
#include <cstdio>
#include <iostream>
#include <queue>
#include <algorithm> using namespace std; typedef pair<int, int> P; //first := x, second := y const int INF = ;
const int MAX_H = ;
const int MAX_W = ;
const int MAX_N = ; int H, W, N;
char maze[MAX_H][MAX_W + ]; int sx, sy; //start
int d[MAX_H][MAX_W]; //steps const int dx[] = {-, , , };
const int dy[] = {, , -, }; int bfs(char c){
//init
for(int i = ; i < H; i ++){
fill(d[i], d[i] + W, INF);
}
d[sx][sy] = ;
queue<P> que;
que.push(P(sx, sy)); while(!que.empty()){
P p = que.front();
que.pop();
//arrive
if(maze[p.first][p.second] == c){
//reset
sx = p.first;
sy = p.second;
break;
} for(int i = ; i < ; i ++){
int nx = p.first + dx[i], ny = p.second + dy[i]; if( <= nx && nx < H && <= ny && ny < W && maze[nx][ny] != 'X' && d[nx][ny] == INF){
que.push(P(nx, ny));
d[nx][ny] = d[p.first][p.second] + ;
}
}
}
return d[sx][sy];
} void solve(){
//start
for(int i = ; i < H; i ++){
for(int j = ; j < W; j ++){
if(maze[i][j] == 'S'){
sx = i;
sy = j;
break;
}
}
}
//bfs for 1-N
int ans = ;
for(int i = ; i <= N; i ++){
ans += bfs('' + i);
}
printf("%d\n", ans);
} int main(int argc, char const *argv[]){ scanf("%d %d %d", &H, &W, &N);
for(int i = ; i < H; i ++){
scanf("%s", maze[i]);
}
solve(); return ;
}
AOJ 0558 广度优先搜索的更多相关文章
- AOJ 0121 广度优先搜索
题意:7数码问题.在2×4的棋盘上,摆有7个棋子,每个棋子上标有1至7的某一数字,不同棋子上标的数字不相同.棋盘上还有一个空格(用0表示),与空格相邻(上下左右)的棋子可以移到空格中,该棋子原先位置成 ...
- 图的广度优先搜索(BFS)
把以前写过的图的广度优先搜索分享给大家(C语言版) #include<stdio.h> #include<stdlib.h> #define MAX_VERTEX_NUM 20 ...
- 广度优先搜索(BFS)
定义 维基百科:https://en.wikipedia.org/wiki/Breadth-first_search 给定图G=(V,E)和一个可识别的源结点s,广度优先搜索对图G中的边进行系统性的探 ...
- 总结A*,Dijkstra,广度优先搜索,深度优先搜索的复杂度比较
广度优先搜索(BFS) 1.将头结点放入队列Q中 2.while Q!=空 u出队 遍历u的邻接表中的每个节点v 将v插入队列中 当使用无向图的邻接表时,复杂度为O(V^2) 当使用有向图的邻接表时, ...
- BFS AOJ 0558 Chess
AOJ 0558 Chess http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0558 在H * W的地图上有N个奶酪工厂,每个 ...
- ACM题目————图的广度优先搜索
题目描述 图的广度优先搜索类似于树的按层次遍历,即从某个结点开始,先访问该结点,然后访问该结点的所有邻接点,再依次访问各邻接 点的邻接点.如此进行下去,直到所有的结点都访问为止.在该题中,假定所有的结 ...
- SDUT 2141 【TEST】数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历
数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem ...
- HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
随机推荐
- C#敏感关键词过滤代码
System.Text.StringBuilder sb = new System.Text.StringBuilder(text.Length); string filter ...
- 数据类型的转换String
x.toString(): 无法转换null和undefined 不过String()却是万能的,其中的原理如下 function String(x){ if(x===undefined){ retu ...
- 【COGS495】窗口
[问题描述] 给你一个长度为N的数组,一个长为K的滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右移动一位,如下表: Window position Min value Max val ...
- How to scroll the window using JQuery $.scrollTo() function
$('html, body').animate({scrollTop: $("#page").offset().top}, 2000); http://stackoverflow. ...
- 激活office 2013
1.下载office 2013激活工具:microsoft toolkit 2.解压文件,运行Microsoft Toolkit.exe,选择office,即箭头标识处
- (转载)用css来实现十字的布局
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- DropDownList自动生成年月日
DropDownList自动生成年月日 aspx页面上有三个dropdownlist控件, dropdownlist1 表示年,dropdownlist2表示月,dropdownlist3表示天: 注 ...
- ThinkPHP 使用极光推送给ios推送消息
HTML <div id="wrap"><a href="<{:U('Push/pushData')}>">推送</a ...
- fedora 安装nginx+php+mysql
环境 fedora 最新版 20 参考:http://www.cnblogs.com/beceo/archive/2012/08/21/2648378.html ------------------- ...
- JavaScript错误处理
JavaScript 错误 - Throw.Try 和 Catch JavaScript 测试和捕捉 try 语句允许我们定义在执行时进行错误测试的代码块. catch 语句允许我们定义当 try 代 ...