cf 877D Olya and Energy Drinks

TI TPS54824芯片烧了5片才搞懂的事:AGND和PGND到底该怎么接? 本文通过TI TPS54824芯片烧毁案例,深入解析DCDC电路中AGND(模拟地)和PGND(功率地)的正确连接方式。作者分享了从芯片短路到稳定工作的完整排查过程,揭示了地平面处理不当导致的典型问题,并给出关键布局优化方案,为硬件工程师提供实用的DCDC设计指南。 阅读详情

一 原题

D. Olya and Energy Drinks

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks.

Formally, her room can be represented as a field of n × m cells, each cell of which is empty or littered with cans.

Olya drank a lot of energy drink, so now she can run k meters per second. Each second she chooses one of the four directions (up, down, left or right) and runs from1 tok meters in this direction. Of course, she can only run through empty cells.

Now Olya needs to get from cell (x1, y1) to cell (x2, y2). How many seconds will it take her if she moves optimally?

It's guaranteed that cells (x1, y1) and (x2, y2) are empty. These cells can coincide.

Input

The first line contains three integers n, m and k (1 ≤ n, m, k ≤ 1000) — the sizes of the room and Olya's speed.

Then n lines follow containing m characters each, the i-th of them contains on j-th position "#", if the cell(i, j) is littered with cans, and "." otherwise.

The last line contains four integers x1, y1, x2, y2 (1 ≤ x1, x2 ≤ n,1 ≤ y1, y2 ≤ m) — the coordinates of the first and the last cells.

Output

Print a single integer — the minimum time it will take Olya to get from (x1, y1) to (x2, y2).

If it's impossible to get from (x1, y1) to (x2, y2), print -1.

Examples
Input
3 4 4
....
###.
....
1 1 3 1
Output
3
Input
3 4 1
....
###.
....
1 1 3 1
Output
8
Input
2 2 1
.#
#.
1 1 2 2
Output
-1
Note

In the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second.

In second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds.

Olya does not recommend drinking energy drinks and generally believes that this is bad.

二 分析

题意:给定一个n*m的迷宫,迷宫有一些障碍,每一步可以选择向某一个方向(上、下、左、右)走1-k格,给定起点和终点,求最少需要几步。

分析:裸的BFS复杂度是O(n*m*k)的,会TLE。需要加上两个剪枝,对应代码bfs函数中唯一的那一行break:1). 在沿一个方向尝试1,2,3...,k格的时候,如果遇到一个非法点,则后续点无需继续;2). 同样在上述过程中,在用队列头的点(x, y)做更新时,如果遇到一个点(nx, ny),起点到它的当前步数dis(nx, ny)满足dis(nx, ny) < dis(x, y) + 1,则后续点无需继续,因为后面的点要么已经被点(nx, ny)更新,要么接下来会被(nx, ny)更新(此时dis(nx, ny) == dis(x, y))

(附)这道题另一种优化方法是为每个点用set存储当前行、列还没有被访问过的点。这样复杂度可以确保是O(n*m*log(n)),详见第二份代码。


三 代码

BFS+剪枝:

#include<iostream>
#include<string>
#include<queue>
using namespace std;

#define PII pair<int, int>
#define mp make_pair

const int maxn = 1005;
const int inf = 1000 * 1000 + 5;
const int dx[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

int n, m, k, x1, y1, x2, y2, dis[maxn][maxn];
string s[maxn];

inline bool ok(int x, int y) {
	return (x >= 0) && (x < n) && (y >= 0) && (y < m) && (s[x][y] == '.');
}

void bfs() {
	queue<PII> q;
	q.push(mp(x1, y1));
	dis[x1][y1] = 0;
	while(!q.empty()) {
		int x = q.front().first;
		int y = q.front().second;
		q.pop();
		for(int i = 0; i < 4; i++) {
			for(int j = 1; j <= k; j++) {
				int nx = x + j * dx[i][0];
				int ny = y + j * dx[i][1];
				if(!ok(nx, ny) || dis[nx][ny] < dis[x][y] + 1) break;
				if(dis[nx][ny] == inf)
				    dis[nx][ny] = dis[x][y] + 1, q.push(mp(nx, ny));
			}
		}
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin >> n >> m >> k;
	for(int i = 0; i < n; i++)
		cin >> s[i];
	cin >> x1 >> y1 >> x2 >> y2;
	x1--; y1--; x2--; y2--;
	
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			dis[i][j] = inf;
	bfs();

	if(dis[x2][y2] == inf)
		cout << "-1" << endl;
	else
		cout << dis[x2][y2] << endl;
	return 0;
}

cf上Farhod_Farmon用set的解法:

#include <bits/stdc++.h>

#define fi first
#define se second
#define fin(s) freopen( s, "r", stdin );
#define fout(s) freopen( s, "w", stdout );

const long long N = 1010;
const long long Q = 100100;
const long long mod = 998244353;
const long long block = 500;

using namespace std;

int n, m, k;
char c[N][N];
set < int > a[N], b[N];
queue < pair < int, pair < int, int > > > q;

void add(int x, int y, int g)
{
        q.push({g, {x, y}});
        a[x].erase(y);
        b[y].erase(x);
}

void solve()
{
        cin >> n >> m >> k;
        for(int i = 1; i <= n; i++){
                for(int j = 1; j <= m; j++){
                        cin >> c[i][j];
                        a[i].insert(j);
                        b[j].insert(i);
                }
        }
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        add(x1, y1, 0);
        while(!q.empty()){
                int x = q.front().se.fi, y = q.front().se.se, f = q.front().fi;
                q.pop();
                if(x == x2 && y == y2){
                        cout << f << "\n";
                        return;
                }
                auto it = a[x].lower_bound(y);
                while(it != a[x].end() && *it - y <= k && c[x][*it] == '.'){
                        add(x, *it, f + 1);
                        it = a[x].lower_bound(y);
                }
                it = a[x].lower_bound(y);
                while(it != a[x].begin() && y - *(--it) <= k && c[x][*it] == '.'){
                        add(x, *it, f + 1);
                        it = a[x].lower_bound(y);
                }
                it = b[y].lower_bound(x);
                while(it != b[y].end() && *it - x <= k && c[*it][y] == '.'){
                        add(*it, y, f + 1);
                        it = b[y].lower_bound(x);
                }
                it = b[y].lower_bound(x);
                while(it != b[y].begin() && x - *(--it) <= k && c[*it][y] == '.'){
                        add(*it, y, f + 1);
                        it = b[y].lower_bound(x);
                }
        }
        cout << -1 << "\n";
}


bool mtest = false; int main()
{
        ios_base::sync_with_stdio(0);
        int TE = 1;
        if(mtest)
                cin >> TE;
        while(TE--)
                solve();
        return 0;
}


13 万字 C 语言从入门到精通保姆级教程2021 年版 13 万字C语言保姆级教程,从入门到精通。 阅读详情

相关推荐

ros2 humble cartographer 2D激光雷达建图

Cartographer是一个支持多平台和传感器配置的SLAM的系统。 本文主要讲述在ros2 humble系统上cartographer的安装方法,以及如何配置参数实现自己的ros2 bag包的离线建图。

weixin_43087302的博客 6938

Codeforces 877D Olya and Energy Drinks【思维优化Bfs】

D. Olya and Energy Drinks time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Olya loves energy drinks. She lov

mengxiang000000 1368

Linux 桌面虚拟化技术 KVM

Linux 桌面虚拟化技术 KVM

高飞的博客 36万+

D. Olya and Energy Drinks (状压BFS)

http://codeforces.com/problemset/problem/877/D 题意:就是一般的迷宫问题的模型,但不过有一个条件就是一次可以往一个方向最多走k步 做法:错得原因可能是vis标记上面,正确的做法应该是记录x,y这个点的四个方向来的情况,如果他在东方面的点来过了,下次就再从东方面来,就不需要了。 #include "bits/stdc++.h" using na...

KXL5180的博客 387

(超详细)Qt入门和配置Visual Studio的Qt环境

内容写了Qt入门,创建并编写第一个Qt程序,以及配置VS编写Qt程序的环境。(超级详细)

LY549826LY549264的博客 7678

大专非科班拿下汇丰外包Java岗:我的TEKSystem面试全流程复盘(含IKM笔试真题)

本文详细记录了一位非科班大专生成功通过TEKSystem面试,拿下汇丰银行Java外包岗位的全过程。从IKM全英文笔试到技术面试,作者分享了7个关键转折点的实战经验,包括简历优化、技术考点、英语面试策略及薪酬谈判技巧,特别适合初级Java开发者参考。

weixin_30074763的博客 393

D. Olya and Energy Drinks【BFS变形】

D. Olya and Energy Drinks题意:每次可以直着走1~k步,问从(x1,y1)出发到(x2,y2)至少需要多少步思路:直接BFS,但有一点要注意的是,判断一点(nx,ny)是不是可以转移是判dis[nx][ny]&gt;dis[x][y]。前一个写法是用vis标记,然而发现WA49.看了数据才知道,可能存在某种情况,使得真正的转移达不到。BFS貌似这样写,也比较好,用dis判转...

Haipai1998的博客 367

3、无限维系统理论中的算子与半群:概念、定理及应用

本博文深入探讨了无限维系统理论中的两个核心概念:里茨谱算子与卢默-菲利普斯定理。文章首先介绍了里茨基的定义与性质,分析了谱、预解集及特征值在无限维系统中的特性,并详细阐述了里茨谱算子的条件及其生成C0-半群的能力,结合阻尼欧拉-伯努利梁模型展示了其应用。随后,文章介绍了卢默-菲利普斯定理及其推论,通过多个偏微分方程实例(如一阶方程、柔性梁、波动方程和扩散问题)说明如何利用该定理判断算子是否生成压缩半群。此外,还强调了状态空间选择和边界条件对系统适定性的重要影响,并总结了相关问题的解决步骤与方法。

flink9streamer的博客 146

[codeforces] 877D. Olya and Energy Drinks (BFS)

[codeforces] 877D. Olya and Energy Drinks (BFS)题目链接: D. Olya and Energy Drinks 题目大意: 给一个N * M的矩阵,给定起点和终点坐标,k代表沿同一个方向1s可以走1~k步, 问从起点走到终点最少需要多少秒,不能到达输出-1。 ‘#’表示墙, ‘.’表示路。数据范围: (1 ≤ n, m, k ≤ 1000)(1 ≤

ZZZZone的博客 522

【异常】飞书OpenClaw机器人 HTTP 401: Invalid Authentication 报错排查与解决方案

HTTP 401 是HTTP协议标准定义的未授权(Unauthorized)状态码,核心含义为请求方身份认证无效,服务端拒绝执行本次请求。飞书开放平台服务端对自建机器人的全链路鉴权校验失败。无论是机器人接收飞书事件推送的上行请求,还是机器人主动调用飞书开放平台API的下行请求,只要身份凭证无效、鉴权逻辑校验不通过,飞书服务端就会返回该报错,并最终透传到飞书客户端会话窗口中。

奶爸的编程之路,也就一周冷个三天 3759

【Bug已解决】codex: rate limit 429 / Too many requests / RPM exceeded — CodeX CLI 速率限制解决方案

CodeX CLI 触发 429 速率限制(RPM/TPM/并发)的解决方案,含请求间隔、自动重试、便宜模型、Tier 升级与多 Key 轮换。

博客 4891

Codeforces Round #442 (Div. 2) D. Olya and Energy Drinks(BFS+剪枝)好题

题目链接 题意:要你判断从起点到终点的最小步数,每次可以从上下左右四个方向沿着同一方向最多走k部。 思路:标准的bfs走迷宫问题,可以略有不同的是每次可以沿同一方向最多走k步,直接bfs的话会tle,考虑一下剪枝,那怎么剪呢?我们可以设一个d数组表示从起点到终点的最小步数,设当前点为(x,y)可以沿着右方向(x,y+1),(x,y+2).。。。(x,y+k),假设我们求出d【x】【y】+1&g...

qq_42479630的博客 228

Cesium 场景特效之【全球海洋】GL2兼容版

Cesium 场景特效之【全球海洋】GL2兼容版

喜欢钻研前端和分享技术 1278

CF877D Olya and Energy Drinks

一、题目 点此看题 二、解法 考察搜索的优化,我们把访问标记设置成每个位置是从哪个方向来的,而不能直接标记,这是为了使kkk那部分的移动是连续的(一旦访问过就不用再做了,这样每个位置只能被访问一次),考虑下图: 如此图,他们在bfsbfsbfs的同一层,假设红先访问并标记,如果我们的标记不考虑方向那么就不满足上述的性质。 #include <cstdio> #include <queue> using namespace std; const int M = 1005; int r

C202044zxy的博客 957

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Codeforces Round #442 D. Olya and Energy Drinks

题目链接:http://codeforces.com/contest/877/problem/D 题意:求两点之间最短路啦,每次可以选择上下左右四种方向,可以走1~k步 思路:BFS+剪枝,如果某个方向上的点已经被同方向访问过了,那么以当前点为起点的这个方向的行动都可以用之前的代替,剪枝 AC代码: #include using namespace std; const in

可我只是梦呓,半生半生的无力 241

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

codeforces #442 div2 Olya and Energy Drinks

D. Olya and Energy Drinks time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Olya loves energy drinks. She loves them so much that her room i

zstu_zy的博客 384

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Codeforces 877D - Olya and Energy Drinks 【简单BFS变形】

Olya loves energy drinks. She loves them so much that her roomis full of empty cans from energy drinks. Formally, her room can be represented as a field of n × m cells, each cell of which is empty or littered with cans. Olya drank a lot of energy drink, so

Dust_heart的博客 2010

Codeforces Round #442(Div.2)Problem D Olya and Energy Drinks(BFS)

D. Olya and Energy Drinks time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Olya loves energy drinks. She lov

记录一些思考的过程。 774

D - Olya and Energy Drinks (CodeForces - 877D)

题目链接https://codeforces.com/contest/877/problem/D Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks. Formally, her room can be represented...

aqiangdi1264的博客 338

Olya and Energy Drinks【Codeforces 877D】【BFS+思维+剪枝】

Codeforces Round #442 (Div. 2) D 这天给学弟学妹们出了这道题,没想到背锅了……感觉要0A了…… QAQ,确实,今天我再次写的时候也WA了好几发,哎,这锅背了…… 看到有些的代码code:访问过的点都标记为mp[x][y] = '#',但是这样是不可以的,这样反驳: ……(前面进行了很多步,进行到这里了:) ##.# .... ##.# 这只是...

既然弱小,就只顾变强就是了 380

Codeforces 877 D Olya and Energy Drinks

题目地址 题意:给你一个地图,‘.’是可以走的路。‘#’是不能走的地方,然后她每分钟能走1~k个格子,告诉你起点和终点,问你要多少分钟从起点到终点。 思路:就是简单的广搜,每次都要遍历下这个方向的1~k个格子,然后因为是广搜,之后到这个点的耗时要不是等于这个时间要不就更大。然后如果遇到了墙或者出去了就直接找过一个方向。

不要錯過才珍惜 822

Codeforces877 D. Olya and Energy Drinks(bfs+set)

题意: 解法: 直接bfs的复杂度是O(n*m*k)的, 但是如果我们能保证每个点都只被搜一次,那么就可以变成O(n*m)的. 可以用set存为访问过的节点, 这样的话,在bfs找后继的时候就不会访问已经走过的节点了, 总复杂度为O(n*m*log). code: #include<bits/stdc++.h> #define PI pair<int,int> using namespace std; const int maxm=1e3+5; set<int>R[

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 231

Olya and Energy Drinks CodeForces - 877D(BFS+剪枝)

题目链接 题意: 一张网格图,有障碍物,每秒能向上下左右四个方向走1~k步,问从起点到终点的最短时间是多少,不能到达则输出-1. 分析:裸的BFS,但不剪枝会T。 剪枝1: 扩展时就判断是否到达终点 936ms/2000ms 物美价廉的剪枝 #include<bits/stdc++.h> using namespace std; const int N=10...

skyline的分享专栏 201

Olya and Energy Drinks(bfs)

D. Olya and Energy Drinks time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Olya loves energy drinks. She loves them ...

weixin_34217773的博客 229

Codeforces Round #442 (Div. 2)-广搜&剪枝&技巧&好题-D. Olya and Energy Drinks

http://codeforces.com/contest/877/problem/D 给定一个点,问你从x点到y点,需要的最少时间。 没秒可以走小于等于k步。 暴力的bfs也是没谁了。。怎么可能过。 后来又写了一种方案,枚举每个点往前后左右的最大可移动距离,还是t (再51nod见过一道类似这种方法的。) 百度之,好多方法。 ① 剪枝,相同方向剪,维护局部最小距离剪。 ② 把vis

天与云与山与水 283

Codeforces 877D Olya and Energy Drinks(BFS+剪枝)

Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks. Formally, her room can be represented as a field of n × m cells, each cell of which is empty or...

weixin_33845477的博客 167
上一篇: cf 895C Square Subsets
下一篇: usaco6.3.3 Cowcycles
max_kibble
博客等级 码龄11年 2粉丝 110原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值