Language: C++

#include<iostream>
#include<queue>
#include<stdio.h>
#include<utility>

using namespace std;

#define MAX_N 100000
#define MAX_M 100000
#define INF 1000000

typedef pair<int, int> coord; //??§?¨?
typedef pair<coord, int> P; //??§?¨??????????

bool s[MAX_M][MAX_N];  //????????????????????????????????? 
int M, N, K;
int d[MAX_M][MAX_N][2]; // ???????????¢, ????????????, ????????±?\?

int main()
{
	scanf("%d %d %d", &M, &N, &K);
	//s[][], d[][][]????????????
	for (int i = 0; i < M ; i++) {
		for (int j = 0; j < N; j++) {
			s[i][j] = false;
			d[i][j][0] = INF;
			d[i][j][1] = INF;
		}
	}
	for (int i = 0; i < K; i++) {
		int x, y;
		scanf("%d %d", &x, &y);
		s[x - 1][y - 1] = true;
	}

	queue<P> que;
	que.push(P(coord(0, 0), 0));
	d[0][0][0] = 0;
	while (que.size()) {
		P p = que.front(); que.pop();
		int x = p.first.first;
		int y = p.first.second;
		if (p.second = 0) {
			if (s[x][y] == true && d[x][y][1] == INF) {
				d[x][y][1] = d[x][y][0] + 1;
				que.push(P(coord(x, y), 1));
			}
			if (y > 0 && d[x][y - 1][0] == INF) {
				d[x][y - 1][0] = d[x][y][0] + 1;
				que.push(P(coord(x, y - 1), 0));
			}
			if (y < N - 1 && d[x][y + 1][0] == INF) {
				d[x][y + 1][0] = d[x][y][0] + 1;
				que.push(P(coord(x, y + 1), 0));
			}
		}
		else {
			if (s[x][y] == true && d[x][y][0] == INF) {
				d[x][y][0] = d[x][y][1] + 1;
				que.push(P(coord(x, y), 0));
			}
			if (x > 0 && d[x - 1][y][1] == INF) {
				d[x - 1][y][1] = d[x - 1][y][1] + 1;
				que.push(P(coord(x - 1, y), 1));
			}
			if (x < M - 1 && d[x + 1][y][1] == INF) {
				d[x + 1][y][1] = d[x][y][1] + 1;
				que.push(P(coord(x + 1, y), 1));
			}
		}
	}
	int ans = d[M - 1][N - 1][0];
	if (ans > d[M - 1][N - 1][1]) ans = d[M - 1][N -1][1];
	if (ans == INF) ans = -1;
	printf("%d\n", ans);
	return 0;
}

Compiler output:
/tmp/cc1Q4eA1.o: in function `main':
a.cc:(.text+0xf): relocation truncated to fit: R_X86_64_PC32 against symbol `K' defined in .bss section in /tmp/cc1Q4eA1.o
a.cc:(.text+0x19): relocation truncated to fit: R_X86_64_PC32 against symbol `N' defined in .bss section in /tmp/cc1Q4eA1.o
a.cc:(.text+0x23): relocation truncated to fit: R_X86_64_PC32 against symbol `M' defined in .bss section in /tmp/cc1Q4eA1.o
a.cc:(.text+0x95): relocation truncated to fit: R_X86_64_PC32 against symbol `d' defined in .bss section in /tmp/cc1Q4eA1.o
a.cc:(.text+0xc0): relocation truncated to fit: R_X86_64_PC32 against symbol `d' defined in .bss section in /tmp/cc1Q4eA1.o
a.cc:(.text+0xd1): relocation truncated to fit: R_X86_64_PC32 against symbol `N' defined in .bss section in /tmp/cc1Q4eA1.o
a.cc:(.text+0xe4): relocation truncated to fit: R_X86_64_PC32 against symbol `M' defined in .bss section in /tmp/cc1Q4eA1.o
a.cc:(.text+0x153): relocation truncated to fit: R_X86_64_PC32 against symbol `K' defined in .bss section in /tmp/cc1Q4eA1.o
a.cc:(.text+0x1e4): relocation truncated to fit: R_X86_64_PC32 against symbol `d' defined in .bss section in /tmp/cc1Q4eA1.o
a.cc:(.text+0x2b1): relocation truncated to fit: R_X86_64_PC32 against symbol `d' defined in .bss section in /tmp/cc1Q4eA1.o
a.cc:(.text+0x2e9): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status