hdu 2612 Find a way【BFS】

2023-10-06 18:59
文章标签 bfs find hdu 2612 way

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612
题意:y和m要在kfc见面,问你能否选择一家kfc使得y和m到那里的距离最短
解析:两个人都跑一遍bfs,然后枚举每家kfc,找出最优的即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int inf = 0x7fffffff;
int dx[] = {0,1,-1,0};
int dy[] = {1,0,0,-1};
int vis[205][205];
int step1[205][205];
int step2[205][205];
int n,m;
char a[205][205];
struct node
{int x,y;int step;node() {}node(int _x,int _y,int _step){x = _x;y = _y;step = _step;}
};
void bfs(int sx,int sy,int step[205][205])
{memset(vis,0,sizeof(vis));queue<node>q;q.push(node(sx,sy,0));while(!q.empty()){node now = q.front();q.pop();if(a[now.x][now.y]=='@'){if(step[now.x][now.y])step[now.x][now.y] = min(step[now.x][now.y],now.step);elsestep[now.x][now.y] = now.step;}for(int i=0;i<4;i++){int tx = now.x+dx[i];int ty = now.y+dy[i];if(tx<0 || tx>=n || ty<0 || ty>=m)continue;if(a[tx][ty]=='#' || vis[tx][ty])continue;vis[tx][ty] = 1;q.push(node(tx,ty,now.step+1));}}
}
int main()
{while(~scanf("%d %d",&n,&m)){int x1,y1,x2,y2;for(int i=0;i<n;i++){scanf("%s",a[i]);for(int j=0;j<m;j++){if(a[i][j]=='Y'){x1 = i;y1 = j;}if(a[i][j]=='M'){x2 = i;y2 = j;}}}memset(step1,0,sizeof(step1));memset(step2,0,sizeof(step2));bfs(x1,y1,step1);bfs(x2,y2,step2);int ans = inf;for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(a[i][j] == '@'){if(step1[i][j] && step2[i][j])ans = min(ans,step1[i][j]+step2[i][j]);}}}printf("%d\n",ans*11);}return 0;
}

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



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

相关文章

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