Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) C

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.

Input

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.

思路:这道题有点难受,我题读错了,以为是可以建无数条隧道。。

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
int n, r1, c1, r2, c2, vis[100][100], mp[100][100], sum, ans = INF;
char arr[100][100];
int net[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
struct node {
    int x, y;
    node(int _x, int _y): x(_x), y(_y) {}
};
int check(int x, int y) {
    if(x < 0 || y < 0 || x >= n || y >= n || arr[x][y] == '1' || vis[x][y])
        return 0;
    return 1;
}
int dis(int x1, int y1, int x2, int y2) {
    return (x2 - x1) * (x2 - x1) + (y1 - y2) * (y1 - y2);
}
void bfs(int x, int y, int k) {
    queue<node>q;
    vis[x][y] = 1;
    q.push(node(x, y));
    while(!q.empty()) {
        node p = q.front();
        q.pop();
        mp[p.x][p.y] = k;
        for(int i = 0; i < 4; i++) {
            int a = p.x + net[i][1], s = p.y + net[i][0];
            if(check(a, s)) {
                vis[a][s] = 1;
                q.push(node(a, s));
            }
        }
    }
}
int main() {
    cin >> n >> r1 >> c1 >> r2 >> c2;
    r1--, r2--, c1--, c2--;
    for(int i = 0; i < n; i++)
        cin >> arr[i];
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            if(!vis[i][j] && arr[i][j] == '0')
                bfs(i, j, ++sum);
    if (mp[r1][c1] == mp[r2][c2]) {
        cout << "0" << endl;
        return 0;
    }
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            if(arr[i][j] == '0' && mp[i][j] == mp[r1][c1])
                for(int q = 0; q < n; q++)
                    for(int p = 0; p < n; p++)
                        if(arr[q][p] == '0' && mp[q][p] == mp[r2][c2])
                            ans = min(ans, dis(i, j, q, p));
    cout << ans;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值