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

本文探讨了一个关于寻找从起点到终点最短时间的问题。主人公可以在一秒内向四个方向之一移动1到k米,需要避开障碍物。通过优化的广度优先搜索算法,解决了这一问题并提供了AC代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 from 1 to k 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 nm 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 ≤ n1 ≤ 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.


题目大意:

每秒主人公可以朝向一个方向走(1~k)步,问从起点走到终点最少需要多少秒。


思路:


直接Bfs的话肯定是水题。但是直接Bfs会TLE掉。我们考虑优化,对应我们如果走一条线的时候,如果出现了前方有一个位子(x,y),使得从起点走到这个点的时间小于当前点走到这个点的时间的话,无需继续向那个方向继续走了。因为那个方向的取优方案可以通过点(x,y)来更新,所以多余步骤可以删除。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
struct node
{
    int x,y,step;
}now,nex;
char a[1050][1050];
int dist[1050][1050];
int vis[1050][1050];
int n,m,k;
int sx,sy,ex,ey;
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};
void Slove()
{
    sx--;sy--;ex--;ey--;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            dist[i][j]=0x3f3f3f3f;
            vis[i][j]=0;
        }
    }
    queue<node>s;
    now.x=sx,now.y=sy,now.step=0;
    dist[sx][sy]=0;
    s.push(now);
    while(!s.empty())
    {
        now=s.front();s.pop();
        vis[now.x][now.y]=0;
        for(int i=0;i<4;i++)
        {
            for(int j=1;j<=k;j++)
            {
                nex.x=now.x+fx[i]*j;
                nex.y=now.y+fy[i]*j;
                nex.step=now.step+1;
                if(nex.x<0||nex.x>=n||nex.y<0||nex.y>=m||a[nex.x][nex.y]=='#'||(dist[nex.x][nex.y]!=0x3f3f3f3f&&dist[nex.x][nex.y]<now.step+1))break;
                else
                {
                    if(nex.step<dist[nex.x][nex.y]&&vis[nex.x][nex.y]==0)
                    {
                        vis[nex.x][nex.y]=1;
                        dist[nex.x][nex.y]=nex.step;
                        s.push(nex);
                    }
                }
            }
        }
    }
    if(dist[ex][ey]==0x3f3f3f3f)dist[ex][ey]=-1;
    printf("%d\n",dist[ex][ey]);
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        for(int i=0;i<n;i++)scanf("%s",a[i]);
        scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
        Slove();
    }
}











### Codeforces Problem 1014D 解答与解释 当前问题并未提供关于 **Codeforces Problem 1014D** 的具体描述或相关背景信息。然而,基于常见的竞赛编程问题模式以及可能涉及的主题领域(如数据结构、算法优化等),可以推测该问题可能属于以下类别之一: #### 可能的解法方向 如果假设此问题是典型的计算几何或者图论类题目,则通常会涉及到如下知识点: - 图遍历(DFS 或 BFS) - 贪心策略的应用 - 动态规划的状态转移方程设计 由于未给出具体的输入输出样例和约束条件,这里无法直接针对Problem 1014D 提供精确解答。但是可以根据一般性的解决思路来探讨潜在的方法。 对于类似的复杂度较高的题目,在实现过程中需要注意边界情况处理得当,并且要充分考虑时间效率的要求[^5]。 以下是伪代码框架的一个简单例子用于说明如何构建解决方案逻辑流程: ```python def solve_problem(input_data): n, m = map(int, input().split()) # 初始化必要的变量或数组 graph = [[] for _ in range(n)] # 构建邻接表或其他形式的数据表示方法 for i in range(m): u, v = map(int, input().split()) graph[u].append(v) result = [] # 执行核心算法部分 (比如 DFS/BFS 遍历) visited = [False]*n def dfs(node): if not visited[node]: visited[node] = True for neighbor in graph[node]: dfs(neighbor) result.append(node) for node in range(n): dfs(node) return reversed(result) ``` 上述代码仅为示意用途,实际应用需依据具体题目调整细节参数设置及其功能模块定义[^6]。 #### 关键点总结 - 明确理解题意至关重要,尤其是关注特殊测试用例的设计意图。 - 对于大规模数据集操作时应优先选用高效的时间空间性能表现良好的技术手段。 - 结合实例验证理论推导过程中的每一步骤是否合理有效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值