Three Statesy

题解:

以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的更多相关文章

  1. codeforces 590C C. Three States(bfs+连通块之间的最短距离)

    题目链接: C. Three States time limit per test 5 seconds memory limit per test 512 megabytes input standa ...

  2. 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 ...

  3. 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 ...

  4. Codeforces | CF1037D 【Valid BFS?】

    题目大意:给定一个\(n(1\leq n\leq 2\cdot10^5)\)个节点的树的\(n-1\)条边和这棵树的一个\(BFS\)序\(a_1,a_2,\dots,a_n\),判断这个\(BFS\ ...

  5. Number Clicker CodeForces - 995E(双向bfs)

    双向bfs  注意数很大  用map来存 然后各种难受....

  6. Valid BFS? CodeForces - 1037D(思维 bfs)

    我真是一只菜狗......emm... 题意: 判断一个从1开始的队列是否可以按照bfs的顺序 进行遍历..必须从1开始...然后后边依次是bfs顺序 解析: 看代码能看懂吧...emm...就是把每 ...

  7. Fair CodeForces - 987D(巧妙bfs)

    题意: 有n个城市 m条边,每条边的权值为1,每个城市生产一种商品(可以相同,一共k种),求出分别从每个城市出发获得s种商品时所走过路的最小权值 解析: 我们倒过来想,不用城市找商品,而是商品找城市, ...

  8. CodeForces 540C Ice Cave (BFS)

    题意:给定 n * m的矩阵,让你并给定初始坐标和末坐标,你只能走'.',并且走过的'.'都会变成'X',然后问你能不能在末坐标是'X'的时候走进去. 析:这个题,在比赛时就是没做出来,其实是一个水题 ...

  9. codeforces 1072D Minimum path bfs+剪枝 好题

    题目传送门 题目大意: 给出一幅n*n的字符,从1,1位置走到n,n,会得到一个字符串,你有k次机会改变某一个字符(变成a),求字典序最小的路径. 题解: (先吐槽一句,cf 标签是dfs题????) ...

随机推荐

  1. 几个linux下的命令

    sudo apt-get insall -f 修复依赖关系 sudo apt-get update    更新源 sudo apt-get upgrade 更新已经安装的包 sudo apt-get ...

  2. Java的编译原理

    概述 java语言的"编译期"分为前端编译和后端编译两个阶段.前端编译是指把*.java文件转变成*.class文件的过程; 后端编译(JIT, Just In Time Comp ...

  3. Java小白进阶之值传递-引用传递

    class ClassA{ int value;//成员变量 } public class TestClassA{ public static void main(String args[]){ in ...

  4. hadoop学习(一)----概念和整体架构

    程序员就得不停地学习啊,故步自封不能满足公司的业务发展啊!所以我们要有搞事情的精神.都说现在是大数据的时代,可以我们这些码农还在java的业务世界里面转悠呢.好不容易碰到一个可能会用到大数据技术的场景 ...

  5. Meta 用法汇总

    本文引自: http://blog.csdn.net/MR_LP/article/details/53607087 什么是 meta ? meta 是html语言head区的一个辅助性标签.也许你认为 ...

  6. SSM框架的详细解说

    文章转载自:http://blog.csdn.net/zhshulin 使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就 ...

  7. 洛谷 P3628 特别行动队

    洛谷题目页面传送门 题意见洛谷. 这题一看就是DP... 设\(dp_i\)表示前\(i\)个士兵的最大战斗力.显然,最终答案为\(dp_n\),DP边界为\(dp_0=0\),状态转移方程为\(dp ...

  8. React Native 混合开发与实现

    关于 微信公众号:前端呼啦圈(Love-FED) 我的博客:劳卜的博客 知乎专栏:前端呼啦圈 前言 随着 React 的盛行,其移动开发框架 React Native 也收到了广大开发者的青睐,以下简 ...

  9. (三)c#Winform自定义控件-有图标的按钮

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  10. websql操作类封装

    在之前,我写了一个websql的封装类库,代码如下: (function(win) { function smpWebSql(options){ options = options || {}; th ...