CodeForces 590C Three States BFS
题解:
以3个大陆为起点,都dfs一遍,求出该大陆到其他点的最小距离是多少, 然后枚举每个点作为3个大陆的路径交点。
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define ep emplace_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 1e3 + ;
int n, m;
LL dis[N][N][];
char s[N][N];
int dx[] = {, -, , };
int dy[] = {, , -, };
deque<pll> q;
void bfs(int k){
for(int i = ; i <= n; ++i)
for(int j = ; j <= m; ++j)
dis[i][j][k] = inf;
char kk = k + '' + ;
for(int i = ; i <= n; ++i){
for(int j = ; j <= m; ++j){
if(s[i][j] == kk){
dis[i][j][k] = ;
q.push_back(pll(i,j));
}
}
}
while(!q.empty()){
pll t = q.front();
q.pop_front();
for(int _ = ; _ < ; ++_){
int nx = t.fi + dx[_];
int ny = t.se + dy[_];
if(nx < || nx > n || ny < || ny > m || s[nx][ny] == '#') continue;
int dd = dis[t.fi][t.se][k] + (s[nx][ny] == '.');
if(dd < dis[nx][ny][k]){
dis[nx][ny][k] = dd;
if(s[nx][ny] == '.') q.push_back(pll(nx,ny));
else q.push_front(pll(nx, ny));
}
}
}
}
int main(){ scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i)
scanf("%s", s[i]+);
for(int i = ; i < ; ++i) bfs(i);
LL ans = inf;
for(int i = ; i <= n; ++i){
for(int j = ; j <= m; ++j){
int f = ;
if(s[i][j] == '.') f = ;
ans = min(ans, dis[i][j][] + dis[i][j][] + dis[i][j][] - f); }
}
if(ans == inf) ans = -;
cout << ans << endl;
return ;
}
CodeForces 590C Three States BFS的更多相关文章
- codeforces 590C C. Three States(bfs+连通块之间的最短距离)
题目链接: C. Three States time limit per test 5 seconds memory limit per test 512 megabytes input standa ...
- Codeforces Round #327 (Div. 2) E. Three States BFS
E. Three States Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/probl ...
- Vladik and Favorite Game CodeForces - 811D (思维+BFS+模拟+交互题)
D. Vladik and Favorite Game time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces | CF1037D 【Valid BFS?】
题目大意:给定一个\(n(1\leq n\leq 2\cdot10^5)\)个节点的树的\(n-1\)条边和这棵树的一个\(BFS\)序\(a_1,a_2,\dots,a_n\),判断这个\(BFS\ ...
- Number Clicker CodeForces - 995E(双向bfs)
双向bfs 注意数很大 用map来存 然后各种难受....
- Valid BFS? CodeForces - 1037D(思维 bfs)
我真是一只菜狗......emm... 题意: 判断一个从1开始的队列是否可以按照bfs的顺序 进行遍历..必须从1开始...然后后边依次是bfs顺序 解析: 看代码能看懂吧...emm...就是把每 ...
- Fair CodeForces - 987D(巧妙bfs)
题意: 有n个城市 m条边,每条边的权值为1,每个城市生产一种商品(可以相同,一共k种),求出分别从每个城市出发获得s种商品时所走过路的最小权值 解析: 我们倒过来想,不用城市找商品,而是商品找城市, ...
- CodeForces 540C Ice Cave (BFS)
题意:给定 n * m的矩阵,让你并给定初始坐标和末坐标,你只能走'.',并且走过的'.'都会变成'X',然后问你能不能在末坐标是'X'的时候走进去. 析:这个题,在比赛时就是没做出来,其实是一个水题 ...
- codeforces 1072D Minimum path bfs+剪枝 好题
题目传送门 题目大意: 给出一幅n*n的字符,从1,1位置走到n,n,会得到一个字符串,你有k次机会改变某一个字符(变成a),求字典序最小的路径. 题解: (先吐槽一句,cf 标签是dfs题????) ...
随机推荐
- MyBatis 核心配置综述之StatementHandler
目录 MyBatis 核心配置综述之StatementHandler MyBatis 四大组件之StatementHandler StatementHandler 的基本构成 StatementHan ...
- 大型系列课程之-七夕告白之旅vbs篇
也许,世间所有的美好的东西,都是需要仪式感的,遇到了一年一度的七夕节,怎么过这个节日,成了很多心中有爱的人关注的事情,七夕不浪漫,人间不值得,七夕,发源于中国,这个美好的节日,来自动人的神话故事传说牛 ...
- spark shuffle写操作三部曲之BypassMergeSortShuffleWriter
前言 再上一篇文章 spark shuffle的写操作之准备工作 中,主要介绍了 spark shuffle的准备工作,本篇文章主要介绍spark shuffle使用BypassMergeSortSh ...
- java多线程基础(二)--java线程各状态关系
注意只有可运行(就绪态)和运行中(运行态)可以相互转换
- linux装OpenOffice后传---中文乱码的解决
上一篇的博客已经详细的介绍了linux系统上如何安装OpenOffice,安装之后使用发现转换的pdf出现中文乱码.后来发现是linux上没有中文对应的那个字体. 字体准备 在windows上的位置 ...
- C++实现多组数据合并输出
思路 假设有多组数据,每一组都是按从小到大的顺序输入的,设计如下数据结构 前面一列是每一组数据的首部,后面是真正的数据,首部的定义为: struct head { Node* next; head* ...
- (2019版本可用)【idea的安装,激活,设置,卸载】
前言 也差不多也可以使用简单快捷的idea软件了,相对于elicpse而言的话,idea是非常好用的,虽然现在涉及不是很广. 什么是idea? IDEA 全称IntelliJ IDEA,是用于java ...
- JAVA-SpringMVC 概述及组件介绍
一.SpringMVC概述 SpringMVC是一个WEB层.控制层框架,主要用来负责与客户端交互,业务逻辑的调用. SpringMVC是Spring家族中的一大组件,Spring整合SpringMV ...
- selenium中的setUp,tearDown与setUpClass,tearDownClass的区别
def setUpClass(cls): cls.driver = webdriver.Chrome() cls.driver.maximize_window() def setUp(self): s ...
- 读取某个目录下的所有图片并显示到pictureBox
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...