cf 877D Olya and Energy Drinks

17 篇文章 0 订阅

一 原题

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;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值