【CodeForces - 821D Okabe and City】 最短路spfa

E - Okabe and City


 

Okabe likes to be able to walk through his city on a path lit by street lamps. That way, he doesn't get beaten up by schoolchildren.

Okabe's city is represented by a 2D grid of cells. Rows are numbered from 1 to nfrom top to bottom, and columns are numbered 1 to m from left to right. Exactly kcells in the city are lit by a street lamp. It's guaranteed that the top-left cell is lit.

Okabe starts his walk from the top-left cell, and wants to reach the bottom-right cell. Of course, Okabe will only walk on lit cells, and he can only move to adjacent cells in the up, down, left, and right directions. However, Okabe can also temporarily light all the cells in any single row or column at a time if he pays 1coin, allowing him to walk through some cells not lit initially.

Note that Okabe can only light a single row or column at a time, and has to pay a coin every time he lights a new row or column. To change the row or column that is temporarily lit, he must stand at a cell that is lit initially. Also, once he removes his temporary light from a row or column, all cells in that row/column not initially lit are now not lit.

Help Okabe find the minimum number of coins he needs to pay to complete his walk!

Input

The first line of input contains three space-separated integers nm, and k (2 ≤ n, m, k ≤ 104).

Each of the next k lines contains two space-separated integers ri and ci (1 ≤ ri ≤ n,1 ≤ ci ≤ m) — the row and the column of the i-th lit cell.

It is guaranteed that all k lit cells are distinct. It is guaranteed that the top-left cell is lit.

Output

Print the minimum number of coins Okabe needs to pay to complete his walk, or -1 if it's not possible.

Example
Input
4 4 5
1 1
2 1
2 3
3 3
4 3
Output
2
Input
5 5 4
1 1
2 1
3 1
3 2
Output
-1
Input
2 2 4
1 1
1 2
2 1
2 2
Output
0
Input
5 5 4
1 1
2 2
3 3
4 4
Output
3
Note

In the first sample test, Okabe can take the path , paying only when moving to (2, 3) and(4, 4).

In the fourth sample, Okabe can take the path  , paying when moving to (1, 2)(3, 4), and (5, 4).


题意:给你一个,n*m的二维平面,其中有k个格子被路灯点亮,现在Okabe要从点(1,1)走到点(n,m),他只能走被点亮的格子,而Okabe自己可以点亮一行或者一列的所有格子(在经过后会熄灭),问他最少多少次点亮格子可以走到点(n,m)。


分析:将所有被点亮的格子记录,如果两个格子相邻,则把两点间的边权设为0,如果两个格子行或列之差为1但不相邻,则把两点间的边权设为1,最后计算最短路就行了。需要注意的是,如果终点没有被点亮,那么需要设点(n+1,m+1)为终点,并加入到点亮的点集合中。


代码如下:

#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>

#define LL long long
#define INF 0x3f3f3f3f

using namespace std;
const int MX = 1e4 + 5;
int n, m, k;
struct node{
    int x, y;
}lit[MX];
int dis[MX];
int inq[MX];

int spfa(){
    for(int i = 1; i <= k; i++){
        dis[i] = INF;
    }
    queue<int> q;
    q.push(1);
    inq[1] = 1;
    dis[1] = 0;
    while(!q.empty()){
        int u = q.front();
        q.pop();
        inq[u] = 0;
        for(int i = 1; i <= k; i++){
            if(u == i)  continue;
            int val = INF;
            int dx = abs(lit[u].x - lit[i].x);
            int dy = abs(lit[u].y - lit[i].y);
            if(dx+dy == 1){
                val = 0;
            }
            else if(dx <= 2 || dy <= 2){
                val = 1;
            }
            if(dis[i] > dis[u] + val){
                dis[i] = dis[u] + val;
                if(!inq[i]){
                    q.push(i);
                    inq[i] = 1;
                }
            }
        }
    }
    if(dis[k] != INF)   return dis[k];
    return -1;
}


int main(){
    scanf("%d%d%d", &n, &m, &k);
    int flag = 0;
    for(int i = 1; i <= k; i++){
        int u, v;
        scanf("%d%d", &lit[i].x, &lit[i].y);
        if(lit[i].x == n && lit[i].y == m)  flag = 1;
    }
    if(!flag){
        lit[++k].x = n+1;
        lit[k].y = m+1;
    }
    cout << spfa() << endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值