【CodeForces

来源:互联网 发布:心动网络工资 编辑:程序博客网 时间:2024/05/30 22:50

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 51 12 12 33 34 3
Output
2
Input
5 5 41 12 13 13 2
Output
-1
Input
2 2 41 11 22 12 2
Output
0
Input
5 5 41 12 23 34 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 0x3f3f3f3fusing 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;}