链接:https://codeforces.com/contest/1130/problem/C

题意:

给一个n*n的图,0表示地面,1表示水,给出起点和终点,

现要从起点到达终点,有一次在两个坐标间创立隧道的机会,消耗为(x1 - x2)^2 + (y1 - y1)^2。

求出最小的消耗,如果直接能走到,则消耗为0。

思路:

bfs求出起点和终点分别能到达的点,枚举每一种情况,取最小值即可。

代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

int Next[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
const int MAXN = 50 + 5;
char Map[MAXN][MAXN];
int vis[MAXN][MAXN];
int n, r1, r2, c1, c2; int Bfs()
{
queue<pair<int, int>> que;
vector<pair<int, int>> start;
vector<pair<int, int>> ends;
que.push(make_pair(r1, c1));
vis[r1][c1] = 1;
while (!que.empty())
{
int x = que.front().first;
int y = que.front().second;
start.push_back(make_pair(x, y));
if (x == r2 && y == c2)
return 0;
for (int i = 0;i < 4;i++)
{
int nx = x + Next[i][0];
int ny = y + Next[i][1];
if (nx < 1 || nx > n || ny < 1 || ny > n)
continue;
if (vis[nx][ny] || Map[nx][ny] == '1')
continue;
vis[nx][ny] = 1;
que.push(make_pair(nx, ny));
}
que.pop();
}
que.push(make_pair(r2, c2));
vis[r2][c2] = 1;
while (!que.empty())
{
int x = que.front().first;
int y = que.front().second;
ends.push_back(make_pair(x, y));
for (int i = 0;i < 4;i++)
{
int nx = x + Next[i][0];
int ny = y + Next[i][1];
if (nx < 1 || nx > n || ny < 1 || ny > n)
continue;
if (vis[nx][ny] || Map[nx][ny] == '1')
continue;
vis[nx][ny] = 1;
que.push(make_pair(nx, ny));
}
que.pop();
}
int res = 9999;
/*
for (int i = 0;i < start.size();i++)
cout << start[i].first << ' ' << start[i].second << endl;
cout << endl;
for (int i = 0;i < ends.size();i++)
cout << ends[i].first << ' ' << ends[i].second << endl;
cout << endl;
*/
for (int i = 0;i < start.size();i++)
{
for (int j = 0;j < ends.size();j++)
{
int cx = start[i].first - ends[j].first;
int cy = start[i].second - ends[j].second;
int cost = cx * cx + cy * cy;
res = min(res, cost);
}
}
return res;
} int main()
{
cin >> n;
cin >> r1 >> c1 >> r2 >> c2;
for (int i = 1;i <= n;i++)
{
for (int j = 1; j <= n; j++)
cin >> Map[i][j];
}
cout << Bfs() << endl; return 0;
}

  

Codeforces Round #542(Div. 2) C.Connect的更多相关文章

  1. Codeforces Round 542 (Div. 2)

    layout: post title: Codeforces Round 542 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  2. Codeforces Round #542(Div. 2) CDE 思维场

    C https://codeforces.com/contest/1130/problem/C 题意 给你一个\(n*m\)(n,m<=50)的矩阵,每个格子代表海或者陆地,给出在陆地上的起点终 ...

  3. Codeforces Round #542(Div. 2) B.Two Cakes

    链接:https://codeforces.com/contest/1130/problem/B 题意: 给定n和 2 * n个数,表示i位置卖ai层蛋糕, 有两个人在1号,必须严格按照1-n的顺序买 ...

  4. Codeforces Round #542(Div. 2) A.Be Positive

    链接:https://codeforces.com/contest/1130/problem/A 题意: 给n个数,找出一个非0整数d,使所有n个数除以整数d后,数组中正数的数量>= n/2. ...

  5. Codeforces Round #542(Div. 2) D1.Toy Train

    链接:https://codeforces.com/contest/1130/problem/D1 题意: 给n个车站练成圈,给m个糖果,在车站上,要被运往某个位置,每到一个车站只能装一个糖果. 求从 ...

  6. Codeforces Round #542 (Div. 1) 题解

    开学了住校了打不了深夜场 好难受啊QwQ A 显然对于每个起点,我们只需要贪心记录这个起点出发出去的糖果数量以及离自己最近的糖果 因为这个起点最后一次装载糖果一定是装载终点离自己最近的那个糖果 $ O ...

  7. Codeforces Round #542 Div. 1

    A:显然对于起点相同的糖果,应该按终点距离从大到小运.排个序对每个起点取max即可.读题花了一年还wa一发,自闭了. #include<iostream> #include<cstd ...

  8. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题解

    Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题目链接:https://codeforces.com/contest/1130 ...

  9. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

随机推荐

  1. Redis java使用

    直接应用redis.clients:jedis的jar包到项目中,然后直接就可以使用,具体对五种类型的数据操作方法,可以翻代码找到. 连接到 redis 服务 实例 import redis.clie ...

  2. VS2005断点失效的问题

    VS2005下使用VC,部分断点无效,显示『当前不会命中断点.还没有为该文档加载任何符号』. 试过以下一些方法: 1.无效断点所在的项目和启动项目的设置:项目->属性->配置属性-> ...

  3. Spring Boot 动态数据源(多数据源自己主动切换)

    本文实现案例场景: 某系统除了须要从自己的主要数据库上读取和管理数据外.另一部分业务涉及到其它多个数据库,要求能够在不论什么方法上能够灵活指定详细要操作的数据库. 为了在开发中以最简单的方法使用,本文 ...

  4. Class.forName("java.lang.String")的作用?

    返回字节码: 返回的方式有2种: 第一种是这个类的字节码已经加载到内存里面来了,现在想要取到它的字节码,我直接找到那份字节码把他返回: 第二种是我去得到这个类的字节码,结果在虚拟机里面还没有这个类的字 ...

  5. ln mv 发挥一个物体的元作用

    # tar xf ~/tools/mongodb-linux-x86_64-3.4.0.tgz -C /usr/local/; ln -sf /usr/local/mongodb-linux-x86_ ...

  6. i MySQL 查看约束,添加约束,删除约束

    查看表的字段信息:desc 表名; 查看表的所有信息:show create table 表名; 添加主键约束:alter table 表名 add constraint 主键 (形如:PK_表名) ...

  7. POJ2251 Dungeon Master —— BFS

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  8. POJ1797 Heavy Transportation —— 最短路变形

    题目链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K T ...

  9. JS基础篇--JS的event.srcElement与event.target(触发事件对象)

    IE下,event对象有srcElement属性,但是没有target属性; Firefox下,event对象有target属性,但是没有srcElement属性.但他们的作用是相当的,即: fire ...

  10. 使用pyinstaller----python转exe

    自己写了一个定时清理日志的脚步,因为服务器是Windows,不想部署Python环境,就考虑到转成exe文件即可. 从网上了解到python转exe方法比较多,我选用了pyinstaller,过程比较 ...