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. angularjs通信以及postmessage与iframe通信

    这篇文章是用markdown工具写的,有需要的可以使用vscode打开   # angularjs 控制器.组件之间的通信 ## 一.基于事件的方式 此方式下,主要通过 angularjs 内置指令` ...

  2. .net持续集成测试篇之Nunit常见断言

    系列目录 Nunit测试基础之简单断言 在开始本篇之前需要补充一些内容,通过前面搭建Nunit测试环境我们知道要使一个方法成为单元测试方法首先要在此方法所在类加上TestFixture注解,并且在该方 ...

  3. FTP服务端部署

    FTP服务端搭建(本地用户登入:使用本地用户和密码登入)1.文件配置:vsftpd.conf: 主配置文件ftpusers: 指定哪些用户不能访问FTP服务器user_list: 指定的用户是否可以访 ...

  4. Spring Security (CORS)跨域资源访问配置

    1.CORS介绍 CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing).它允许浏览器向跨源(协议 + 域名 + 端口)服务 ...

  5. 关于JVM内存溢出的原因分析及解决方案探讨

    前言:JVM中除了程序计数器,其他的区域都有可能会发生内存溢出. 0.什么是内存溢出 当程序需要申请内存的时候,由于没有足够的内存,此时就会抛出OutOfMemoryError,这就是内存溢出. 1. ...

  6. 转载 | CSS实现单行、多行文本溢出显示省略号(…)

    本文引自:https://www.cnblogs.com/wyaocn/p/5830364.html 首先,要知道css的三条属性. overflow:hidden; //超出的文本隐藏 text-o ...

  7. IDEA+maven搭建scala开发环境(spark)(半转载)

    以下内容部分来自于https://zhuanlan.zhihu.com/p/23141509,我尝试了一遍,然后添加了一些图片.. 其实我觉得在IDEA中使用scala插件然后创建project的时候 ...

  8. Java并发编程实战笔记—— 并发编程4

    1.同步容器类 同步容器类都是线程安全的,但在某些情况下可能需要额外的客户端加锁保护复合操作. 容器上常见的复合操作包括但不限于:迭代(反复访问数据,直到遍历完容器中所有的元素为止).跳转(根据指定顺 ...

  9. django实现自定义manage命令的扩展

    在Django开发过程中我们都用过django-admin.py和manage.py命令. django-admin.py是一个命令行工具,可以执行一些管理任务,比如创建Django项目.而manag ...

  10. .netcore持续集成测试篇之测试方法改造

    系列目录 通过前面两节讲解,我们的测试类中已经有两个测试方法了,总体上如下 public class mvc20 { private readonly HttpClient _client; publ ...