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的顺序买 ...
随机推荐
- SpringBoot解决ajax跨域问题
一.第一种方式: 1.编写一个支持跨域请求的 Configuration import org.springframework.context.annotation.Configuration; im ...
- Core知识整理
概述 Commond-Line ASP.NET结构文件 Startup 配置文件 中间件和依赖注入 依赖注入原理 框架自带的依赖注入(IServiceCollection) 依赖注入生命周期 依赖注入 ...
- 49.Linux-wpa_cli使用之WIFI开启,扫描热点,连接热点,断开热点,WIFI关闭(49)
本章学习内容: 1.WIFI如何开启 2.扫描热点 3.连接热点 4. 断开热点 5.关闭WIFI 本节使用的是wpa_supplicant工具,它主要包含wpa_supplicant(命令行模式)与 ...
- oracle的Date类型遇到MyBatis产生的坑
坑描述: 公司的订单表数据量巨大(亿级),在进行查询的时候,发现一个慢查询. 背景: 数据库:oracle 表:T_order 索引字段:create_date (字段类型 date) 慢查询sql ...
- elasticsearch概念
1.elasticsearch的核心概念 (1)Near Realtime(NRT):近实时,从写入数据到数据可以被搜索到有一个小延迟(大概1秒):基于es执行搜索和分析可以达到秒级 (2)Clust ...
- Android view显示在软键盘上方
给EditText外加一个ScrollView,将高度设置统一,并给ScrollView设置属性 android:fillViewport="true". 注:ScrollVie ...
- CA 工作流程
散列函数 Hash 常见的有 MD5, SHA1, SHA256, 该类函数特点是函数单向不可逆,对输入非常敏感,输出长度固定,针对数据的任何修改都会改变散列函数的结果,用于防止信息篡改并验证数据的完 ...
- git pull遇到错误:error: Your local changes to the following files would be overwritten by merge:
方法1:如果你想保留刚才本地修改的代码,并把git服务器上的代码pull到本地(本地刚才修改的代码将会被暂时封存起来) git stash git pull origin master git sta ...
- Spider-four
计算机速度比较: CPU -> 寄存器 -> 缓存L1/L2/L3 -> 内存 -> 硬盘 -> 网卡 -> BIOS LMAP: Linux + MySQL + ...
- Python 捕捉traceback异常栈信息
捕捉traceback异常栈信息 by:授客 QQ:1033553122 相关函数简介 sys.exc_info() 返回包含3个元素(type, value, traceback)的元组,提供关 ...