Codeforces Round #542 C. Connect 搜索
1 second
256 megabytes
standard input
standard output
Alice lives on a flat planet that can be modeled as a square grid of size n×nn×n , with rows and columns enumerated from 11 to nn . We represent the cell at the intersection of row rr and column cc with ordered pair (r,c)(r,c) . Each cell in the grid is either land or water.
An example planet with n=5n=5 . It also appears in the first sample test.
Alice resides in land cell (r1,c1)(r1,c1) . She wishes to travel to land cell (r2,c2)(r2,c2) . At any moment, she may move to one of the cells adjacent to where she is—in one of the four directions (i.e., up, down, left, or right).
Unfortunately, Alice cannot swim, and there is no viable transportation means other than by foot (i.e., she can walk only on land). As a result, Alice's trip may be impossible.
To help Alice, you plan to create at most one tunnel between some two land cells. The tunnel will allow Alice to freely travel between the two endpoints. Indeed, creating a tunnel is a lot of effort: the cost of creating a tunnel between cells (rs,cs)(rs,cs) and (rt,ct)(rt,ct) is (rs−rt)2+(cs−ct)2(rs−rt)2+(cs−ct)2 .
For now, your task is to find the minimum possible cost of creating at most one tunnel so that Alice could travel from (r1,c1)(r1,c1) to (r2,c2)(r2,c2) . If no tunnel needs to be created, the cost is 00 .
The first line contains one integer nn (1≤n≤501≤n≤50 ) — the width of the square grid.
The second line contains two space-separated integers r1r1 and c1c1 (1≤r1,c1≤n1≤r1,c1≤n ) — denoting the cell where Alice resides.
The third line contains two space-separated integers r2r2 and c2c2 (1≤r2,c2≤n1≤r2,c2≤n ) — denoting the cell to which Alice wishes to travel.
Each of the following nn lines contains a string of nn characters. The jj -th character of the ii -th such line (1≤i,j≤n1≤i,j≤n ) is 0 if (i,j)(i,j) is land or 1 if (i,j)(i,j) is water.
It is guaranteed that (r1,c1)(r1,c1) and (r2,c2)(r2,c2) are land.
Print an integer that is the minimum possible cost of creating at most one tunnel so that Alice could travel from (r1,c1)(r1,c1) to (r2,c2)(r2,c2) .
5
1 1
5 5
00001
11111
00111
00110
00110
10
3
1 3
3 1
010
101
010
8
In the first sample, a tunnel between cells (1,4)(1,4) and (4,5)(4,5) should be created. The cost of doing so is (1−4)2+(4−5)2=10(1−4)2+(4−5)2=10 , which is optimal. This way, Alice could walk from (1,1)(1,1) to (1,4)(1,4) , use the tunnel from (1,4)(1,4) to (4,5)(4,5) , and lastly walk from (4,5)(4,5) to (5,5)(5,5) .
In the second sample, clearly a tunnel between cells (1,3)(1,3) and (3,1)(3,1) needs to be created. The cost of doing so is (1−3)2+(3−1)2=8(1−3)2+(3−1)2=8 .
就是简单的搜索,用dfs求出连通块,然后就遍历起点的所有连通块到终点的连通块,求出最短距离。
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = ;
char s[][];
int n, sx, sy, gx, gy,ans=inf;
int dx[] = { ,,-, };
int dy[] = { ,,,- };
bool vis[][];
struct node
{
int x, y;
node(int x = , int y = ) :x(x), y(y) {}
};
int cnt = ;
void dfs(int x, int y, node *d)
{
for (int i = ; i < ; i++)
{
int tx = x + dx[i];
int ty = y + dy[i]; if (vis[tx][ty]) continue;
if (s[tx][ty] == '') continue;
if (tx< || ty< || tx>n || ty>n) continue; vis[tx][ty] = true;
d[cnt++] = node(tx, ty);
dfs(tx, ty, d);
}
}
int min_(int a,int b)
{
return a < b ? a : b;
} int main()
{
cin >> n >> sx >> sy >> gx >> gy;
for (int i = ; i <= n; i++) cin >> s[i] + ;
node a[maxn], b[maxn];
a[] = node(sx, sy); b[] = node(gx, gy);
vis[sx][sy] = true;cnt = ;
dfs(sx, sy, a);
if(vis[gx][gy])
{
printf("0\n");
return ;
}
int tot = cnt;cnt = ;
vis[gx][gy] = ;
dfs(gx, gy, b);
//printf("%d %d\n", cnt, tot);
for(int i=;i<tot;i++)
{
for(int j=;j<cnt;j++)
{
//printf("%d %d %d %d\n", a[i].x, a[i].y, b[j].x, b[j].y);
int exa = (a[i].x - b[j].x)*(a[i].x - b[j].x) + (a[i].y - b[j].y)*(a[i].y - b[j].y);
//printf("%d\n", exa);
ans = min_(ans, exa);
}
}
printf("%d\n", ans);
return ;
}
Codeforces Round #542 C. Connect 搜索的更多相关文章
- Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题解
Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题目链接:https://codeforces.com/contest/1130 ...
- Codeforces Round #542 题解
Codeforces Round #542 abstract I决策中的独立性, II联通块染色板子 IIIVoronoi diagram O(N^2 logN) VI环上距离分类讨论加取模,最值中的 ...
- Codeforces Round 542 (Div. 2)
layout: post title: Codeforces Round 542 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- Codeforces Round #542(Div. 2) C.Connect
链接:https://codeforces.com/contest/1130/problem/C 题意: 给一个n*n的图,0表示地面,1表示水,给出起点和终点, 现要从起点到达终点,有一次在两个坐标 ...
- Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) A - D2
A. Be Positive 链接:http://codeforces.com/contest/1130/problem/A 题意: 给一段序列,这段序列每个数都除一个d(−1e3≤d≤1e3)除完后 ...
- Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2)
A. Be Positive 题意:给出一个数组 每个树去除以d(d!=0)使得数组中大于0的数 大于ceil(n/2) 求任意d 思路:数据小 直接暴力就完事了 #include<bits/s ...
- Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 1) C(二分+KMP)
http://codeforces.com/contest/1129/problem/C #include<bits/stdc++.h> #define fi first #define ...
- Codeforces Round #542(Div. 2) CDE 思维场
C https://codeforces.com/contest/1130/problem/C 题意 给你一个\(n*m\)(n,m<=50)的矩阵,每个格子代表海或者陆地,给出在陆地上的起点终 ...
- Codeforces Round #542(Div. 2) B.Two Cakes
链接:https://codeforces.com/contest/1130/problem/B 题意: 给定n和 2 * n个数,表示i位置卖ai层蛋糕, 有两个人在1号,必须严格按照1-n的顺序买 ...
随机推荐
- 分部视图(Partial View)及Html.Partial和Html.Action差异
参考资料: https://www.cnblogs.com/Leon-Hu/p/5575311.html
- linux系统mysql主从配置
一.原理 mysql主从配置的流程大体如图: 1)master会将变动记录到二进制日志里面: 2)master有一个I/O线程将二进制日志发送到slave; 3) slave有一个I/O线程把mast ...
- TS学习随笔(二)->类型推论,联合类型
这篇内容指南: -----类型推论 -----联合类型 类型推论 第一篇中我们看了TS的基本使用和基本数据类型的使用,知道了变量在使用的时候都得加一个类型,那我们可不可以不加呢,这个嘛 ...
- maven 继承
一个 maven 项目可以继承另一个 maven 的依赖, 称为子项目 父项目 使用场景: 多个子项目都需要某些依赖, 就可以把子项目共同的依赖抽取到父项目中, 子项目通过继承得到这些依赖, 这样也更 ...
- linux 安装 redsi
下载.解压.编译 wget http://download.redis.io/releases/redis-4.0.10.tar.gz tar xzf redis-4.0.10.tar.gz cd r ...
- Vue 组件之间传值
一.父组件向子组件传递数据 在 Vue 中,可以使用 props 向子组件传递数据. 子组件部分: 这是 header.vue 的 HTML 部分,logo 是在 data 中定义的变量. 如果需要从 ...
- 实战 | Android中文图混排时文图的居中对齐 FontMetrics以及自定义ImageSpan实现
这个标题有点长,乍一看这么个标题你可能没明白啥意思,且听我慢慢道来. 公司的项目中新增了一个“心动” 的功能,用户初次使用时需要给一个引导页,就是下面图中的这个样子(这就是做完之后的效果了). 在上图 ...
- JMeter 监听器之保存响应到文件
监听器之保存响应到文件 by:授客 QQ:1033553122 测试环境 apache-jmeter-2.13 1. 保存结果到响应文件 说明: 文件名称前缀:设置响应文件所在路径(路径必须已存在 ...
- AndroBench手机性能测试
AndroBench是一个基准测试应用程序,可以衡量你的Android设备的存储性能. AndroBench提供两种方式,第一种可以快速与其他设备的存储进行比较. 第二种 SQLite可以查询数据库表 ...
- 电脑端支付宝支付 -前端获取支付宝返回的form 以及submit 调用支付扫码页面
前端调取支付宝支付接口(后台进行封装,没有直接调取支付宝接口),调用返回的数据中,将会有一串的form表单数据返回,我们需要将此表单在当前调用页面submit下,以跳转到支付扫码页: 支付宝返回的fo ...