N - Find a way HDU - 2612(BFS)

2024-04-16 03:38
文章标签 bfs find hdu 2612 way

本文主要是介绍N - Find a way HDU - 2612(BFS),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
Input
The input contains multiple test cases.
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’ express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
Output
For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
Sample Input
4 4
Y.#@

.#…
@…M
4 4
Y.#@

.#…
@#.M
5 5
Y…@.
.#…
.#…
@…M.
#…#
Sample Output
66
88
66

思路:基础BFS,BFS跑一遍算出到每个人到各个KFC的距离,再枚举距离求距离和最小。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>using namespace std;
const int maxn = 200 + 10;
int dr[] = {-1,1,0,0};
int dc[] = {0,0,-1,1};
int n,m;
int sx,sy,ex,ey;
char maze[maxn][maxn];
int dis[2][maxn][maxn];
int dirx[] = {1,-1,0,0};
int diry[] = {0,0,-1,1};
struct node
{int x,y;node(){}node(int x,int y):x(x),y(y){}
};void bfs(int x,int y,int type)
{memset(dis[type],-1,sizeof(dis[type]));queue<node>Q;Q.push(node(x,y));dis[type][x][y] = 0;while(!Q.empty()){node now = Q.front();Q.pop();for(int i = 0;i < 4;i++){int nx = now.x + dirx[i];int ny = now.y + diry[i];if(nx >= 0 && nx < n && ny >= 0 && ny < m && maze[nx][ny] != '#' && dis[type][nx][ny] == -1){dis[type][nx][ny] = dis[type][now.x][now.y] + 1;Q.push(node(nx,ny));}}}
}int main()
{while(~scanf("%d%d",&n,&m)){vector<node>KFC;for(int i = 0;i < n;i++){scanf("%s",maze[i]);for(int k = 0;k < m;k++){if(maze[i][k] == 'Y'){sx = i,sy = k;}else if(maze[i][k] == 'M'){ex = i,ey = k;}else if(maze[i][k] == '@'){KFC.push_back(node(i,k));}}}bfs(sx,sy,0);bfs(ex,ey,1);int minn = 1e8;for(int i = 0;i < KFC.size();i++){int len1 = dis[0][KFC[i].x][KFC[i].y];int len2 = dis[1][KFC[i].x][KFC[i].y];if(len1 == -1 || len2 == -1){continue;}int length = len1 + len2;minn = min(minn,length);}printf("%d\n",minn * 11);}return 0;
}

这篇关于N - Find a way HDU - 2612(BFS)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907709

相关文章

MySQL中FIND_IN_SET函数与INSTR函数用法解析

《MySQL中FIND_IN_SET函数与INSTR函数用法解析》:本文主要介绍MySQL中FIND_IN_SET函数与INSTR函数用法解析,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一... 目录一、功能定义与语法1、FIND_IN_SET函数2、INSTR函数二、本质区别对比三、实际场景案例分

Linux find 命令完全指南及核心用法

《Linuxfind命令完全指南及核心用法》find是Linux系统最强大的文件搜索工具,支持嵌套遍历、条件筛选、执行动作,下面给大家介绍Linuxfind命令完全指南,感兴趣的朋友一起看看吧... 目录一、基础搜索模式1. 按文件名搜索(精确/模糊匹配)2. 排除指定目录/文件二、根据文件类型筛选三、时间

hdu1254(嵌套bfs,两次bfs)

/*第一次做这种题感觉很有压力,思路还是有点混乱,总是wa,改了好多次才ac的思路:把箱子的移动当做第一层bfs,队列节点要用到当前箱子坐标(x,y),走的次数step,当前人的weizhi(man_x,man_y),要判断人能否将箱子推到某点时要嵌套第二层bfs(人的移动);代码如下:

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

hdu 3790 (单源最短路dijkstra)

题意: 每条边都有长度d 和花费p,给你起点s 终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。 解析: 考察对dijkstra的理解。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstrin