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. Django配置MySQL数据库

    一.在settings.py中配置 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # 数据库引擎 'NAME': ' ...

  2. 【Java例题】2.7找零钱

    7.为顾客找零钱时,希望选用的纸币张数最少. 例如73元,希望零钱的面值为五十元1张,二十元1张,一元3张. 设零钱面值有五十元.二十元.十元.五元和一元, 请编写程序,用户输入100以下的数, 计算 ...

  3. 【Java例题】2.4求函数

    4.输入x,编程试求函数 y=sin(x^2)/(1-cosx)的值. 这里的"^"表示乘方. package study; import java.util.Scanner; p ...

  4. java并发编程(九)----(JUC)CyclicBarrier

    上一篇我们介绍了CountDownlatch,我们知道CountDownlatch是"在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待",即CountDownL ...

  5. vue-cli中的跨域之proxytable

    为什么会有跨域? 浏览器有一个叫做同源策略的东西.同源策略限制了从同一个源加载的文档或脚本如何与来自另一个源的资源进行交互.这是一个用于隔离潜在恶意文件的重要安全机制. 同源策略规定了如果两个页面的协 ...

  6. scrapy 自学入门demo分享

    [toc] 本文基于python 3.7.0,win10平台: 2018-08 完整项目代码:https://github.com/NameHewei/python-scrapy 安装 安装pytho ...

  7. 使用mybatis实现分页查询示例代码分析

    *******************************************分页查询开始*************************************************** ...

  8. php Basic HTTP与Digest HTTP 应用

    Basic HTTP 认证范例 <?php //Basic HTTP 认证 if (!isset($_SERVER['PHP_AUTH_USER'])) { header('WWW-Authen ...

  9. Kafka 系列(五)—— 深入理解 Kafka 副本机制

    一.Kafka集群 Kafka 使用 Zookeeper 来维护集群成员 (brokers) 的信息.每个 broker 都有一个唯一标识 broker.id,用于标识自己在集群中的身份,可以在配置文 ...

  10. Kali Linux无法访问网络的问题

    首先 ping www.baidu.com ping: unkown host www.baidu.com 然后 ping 8.8.8.8 connect:network is unreachable ...