[kuangbin带你飞]专题一 简单搜索 Find a way HDU - 2612
InputThe 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 OutputFor 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 开始的时候想都没想就先找出所有的中间点,然后计算每个中间点,Y和M到他们的最短距离,然后求出最短的。
果然超时
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 40005
int n,m,dx[]={,-,,},dy[]={,,,-},num,sum[maxn],vis[][];
char mapn[][];
struct point{
int x,y;
};
point p[maxn];
struct node{
int x,y,step;
};
void bfs(int a,int b,int c,int d,int cnt){
node p;
p.x = a,p.y = b,p.step = ;
queue<node> q;
q.push(p);
while(!q.empty()){
node tmp = q.front();
q.pop();
if(tmp.x == c && tmp.y == d){
sum[cnt] += tmp.step;
return;
}
for(int i=;i<;i++){
int xx = tmp.x + dx[i];
int yy = tmp.y + dy[i];
if(xx> && xx<=n && yy> && yy<=m && !vis[xx][yy] && mapn[xx][yy]!='#'){
vis[xx][yy] = ;
node tp;
tp.x = xx,tp.y = yy,tp.step = tmp.step + ;
q.push(tp);
}
}
}
}
int main()
{
while(cin >> n >> m){
num = ;
memset(sum,,sizeof(sum));
if(!n || !m){
break;
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
cin >> mapn[i][j];
if(mapn[i][j] == '@'){
p[num].x = i,p[num].y = j,num++;
}
}
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mapn[i][j] == 'Y' || mapn[i][j] == 'M'){
for(int k=;k<num;k++){
memset(vis,,sizeof(vis));
bfs(i,j,p[k].x,p[k].y,k);
}
}
}
}
sort(sum,sum+num);
cout << sum[] * << endl;
}
return ;
}
后面是先计算好Y,M到每个点的最短距离,这样只需要循环Y,M的个数次数就行,不需再套上中间点个数的循环。
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 40005
#define inf 0x1f1f1f1f
int n,m,dx[]={,-,,},dy[]={,,,-},num,sum[][][],vis[][],z;
char mapn[][];
struct node{
int x,y,step;
};
void bfs(int a,int b){
memset(vis,,sizeof(vis));//在这里重置vis数组
node p;
p.x = a,p.y = b,p.step = ;
vis[p.x][p.y] = ;
queue<node> q;
q.push(p);
while(!q.empty()){
node tmp = q.front();
q.pop();
for(int i=;i<;i++){
int xx = tmp.x + dx[i];
int yy = tmp.y + dy[i];
if(xx> && xx<=n && yy> && yy<=m && !vis[xx][yy] && mapn[xx][yy]!='#'){
vis[xx][yy] = ;
node tp;
tp.x = xx,tp.y = yy,tp.step = tmp.step + ;
sum[tp.x][tp.y][z] = tp.step;
q.push(tp);
}
}
}
}
int main()
{
while(cin >> n >> m){
num = ;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
sum[i][j][] = inf;
sum[i][j][] = inf;//注意记录距离的sum数组一定要初始化为一个很大的数
}
}
if(!n || !m){
break;
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
cin >> mapn[i][j];
}
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mapn[i][j] == 'Y'){
z = ;
bfs(i,j);
}
if(mapn[i][j] == 'M'){
z = ;
bfs(i,j);
}
}
}
int ans = inf;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mapn[i][j] == '@'){
ans = min(ans,sum[i][j][] + sum[i][j][]);
}
}
}
cout << ans* << endl;
}
return ;
}
[kuangbin带你飞]专题一 简单搜索 Find a way HDU - 2612的更多相关文章
- [kuangbin带你飞]专题一 简单搜索 题解报告
又重头开始刷kuangbin,有些题用了和以前不一样的思路解决.全部题解如下 点击每道题的标题即可跳转至VJ题目页面. A-棋盘问题 棋子不能摆在相同行和相同列,所以我们可以依此枚举每一行,然后标记每 ...
- [kuangbin带你飞]专题一 简单搜索(回顾)
A - 棋盘问题 POJ - 1321 注意条件:不能每放一个棋子,就标记一行和一列,我们直接枚举每一行就可以了. AC代码: #include<iostream> #include< ...
- [kuangbin带你飞]专题一 简单搜索 - E - Find The Multiple
//Memory Time //2236K 32MS #include<iostream> using namespace std; ]; //保存每次mod n的余数 //由于198的余 ...
- [kuangbin带你飞]专题一 简单搜索 棋盘问题
题来:链接https://vjudge.net/problem/OpenJ_Bailian-132 J - 棋盘问题 1.题目: 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别. ...
- [kuangbin带你飞]专题一 简单搜索
ID Origin Title 454 / 1008 Problem A POJ 1321 棋盘问题 328 / 854 Problem B POJ 2251 Dungeon Ma ...
- [kuangbin带你飞]专题一 简单搜索 回顾总结
第二题:bfs,忘了将queue清空. 第三题:bfs,记得使用vis数组,防止重复入队
- 迷宫问题 POJ - 3984 [kuangbin带你飞]专题一 简单搜索
定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...
- Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...
- Dungeon Master POJ - 2251 [kuangbin带你飞]专题一 简单搜索
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
随机推荐
- 自定义SWT控件三之搜索功能下拉框
3.搜索功能下拉弹出框 package com.view.control.select; import java.util.ArrayList; import java.util.LinkedList ...
- 代码生成java连接数据库的所需代码(超详细)
开始学习: round 1:(一开始学习当然还是要一步一步学习的啦,哪有什么一步登天!!!) a.准备工作:1.eclipse,mysql(这两个软件肯定要的啦,不然学什么把它们连接起来) 2.加载驱 ...
- mysql docker 主从配置
主从复制相关 前置条件: docker安装的mysql是5.7.26版本 1. 编排docker-compose文件如下: version: '3' services: mysql-master: v ...
- SpringBoot 使用JPA时解决no session的方法
1.在application.yml中添加 spring.jpa.open-in-view: true 2.在使用查询的方法添加 @Transactional
- Java连载16-++传参&关系运算符
一.++再举例 int a = 10; System.out.print(a++);//这里会打印出10,因为他们内部这个print函数有参数相当于参数x=a++ System.out.println ...
- JavaWeb购物车
一.类关系 最近又把JavaWeb方面的知识(Servlet.jsp等)过了一遍,发现以前还是接触的太窄太浅.加上才转到IntelliJ IDEA 上故而想用这个项目练练,就当熟悉熟悉IntelliJ ...
- Docker 方式部署 Solo 博客系统总结
此篇为Docker部署方式,另有Tomcat部署方式,请参考文章<Tomcat 方式部署 Solo 博客系统总结> 最近搭建了一个博客系统,作为自己的主页,方便记录一些平时所见所闻 ...
- React 多副本问题
Element ref was specified as a string (MySider) but no owner was set. This could happen for one of t ...
- 一文看懂ConstraintLayout的用法
ConstraintLayout 相对于 RelativeLayout来说性能更好,布局上也更加灵活.在最新的Google Android开发文档中是推荐使用 ConstraintLayout的,下面 ...
- CF553C Love Triangles(二分图)
Tyher推的好题. 题意就是给你一些好边一些坏边,其他边随意,让你求符合好坏坏~,或者只包含好好好的三元环的无向图个数. 坏坏的Tyher的题意是这样的. 再翻译得更加透彻一点就是:给你一些0(好边 ...