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题????) ... 
随机推荐
- 【Java】字符串空格相关
			1. 去掉首尾空格 [trim() 方法] String.trim() //去掉首尾空格 2. 替换多个空格为一个 [replaceAll() 方法] str.replaceAll(" + ... 
- oracle常用的一些sql命令
			//查看系统当前时间 HH24 24小时制 MI是正确的分钟 select to_char(sysdate,'yyyy-mm-dd HH24:MI:SS') from dual //HH非24 ... 
- 大型系列课程之-七夕告白之旅Electron篇
			上一篇分享了一下vbs的撩妹攻略,但细心的兄弟会发现,这种脚本式的攻城方案并不得心应手,有很多妹子害怕是病毒根本不敢点击,而且这个脚本界面风格也不漂亮,不能轻易打动妹子的心,怎么破,小编这次在为各位老 ... 
- codeforces1088D_Ehab and another another xor problem交互题
			传送门 一道考验思维的交互题 大致思路就是从最高的二进制位向下询问 代入例子比如: 5 6 6 5 7 4 6 4 讨论一下 交互题的重点学会推理和归纳 #include <bits/stdc+ ... 
- 每个程序员都可以「懂」一点 Linux
			提到 Linux,作为程序员来说一定都不陌生.但如果说到「懂」Linux,可能就没有那么多人有把握了.到底用 Linux 离懂 Linux 有多远?如果决定学习 Linux,应该怎么开始?要学到什么程 ... 
- sql语句优化:尽量使用索引避免全表扫描
			1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ... 
- 减谈迷宫C++
			今天老师让做了个迷宫问题,我一看到就发现和我之前写过的一个程序是一样 的,但是在后来编写的时候有一个地方搞错了,最后下课了我还是没有正确的编写好,然后今天回来之后自己有看了一下,现在已经解决了. #i ... 
- Gin + Vue全栈开发实战(一)
			Gin入门 本章概要 Gin简介 开发第一个Gin程序 1.1 Gin简介 Gin是用Go语言编写的一个轻量级Web应用框架,现在在各个公司包括字节跳动.bilibili等大互联网公司都得到了广泛的应 ... 
- 7.19 包 logging模块 hashlib模块 openpyxl模块 深浅拷贝
			包 包是什么 他是一系列文件的结合体,表现形式就是文件夹 包的本质还是模块 他通常会有__init__.py文件 我们首先回顾一下模块导入的过程 import module首次导入模块(.py文件) ... 
- java io读取性能对比
			背景 从最早bio的只支持阻塞的bio(同步阻塞) 到默认阻塞支持非阻塞nio(同步非阻塞+同步阻塞)(此时加入mmap类) 再到aio(异步非阻塞) 虽然这些api改变了调用模式,但真正执行效率上是 ... 
