Language: C++

#include <stdio.h>
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <deque>
#include <tuple>

using namespace std;

#define FOR(i,n) for(int i = 0; i < (n); i++) 
#define sz(c) ((int)c.size())

typedef pair<int, int> Pii;

void compress(vector<int>& v) {
	sort(v.begin(), v.end());
	v.erase(unique(v.begin(), v.end()), v.end());
}

int w, h;
typedef pair<Pii, int> P;
vector<P> e[350][350];
int cost[350][350];

int solve(Pii s, Pii g) {
	memset(cost, 0x1f, sizeof(cost));
	deque<Pii> deq;
	deq.push_back(s);
	cost[s.first][s.second] = 0;

	while (!deq.empty()) {
		Pii cur = deq.front(); deq.pop_front();
		if (cur == g) 
            return cost[g.first][g.second];
		for (auto to : e[cur.first][cur.second]) {
			int nf, ns;
			tie(nf, ns) = to.first;
			int ncost = cost[cur.first][cur.second] + to.second;
			if (ncost < cost[nf][ns]) {
				cost[nf][ns] = ncost;
				if (to.second == 0) {
					deq.push_front(Pii(nf, ns));
				} else {
					deq.push_back(Pii(nf, ns));
				}
			}
		}
	}

	return -1;
}

int main() {
	int m; cin >> w >> h >> m;
	vector<int> cx, cy;
	cx.push_back(0);
	cx.push_back(1);
	cx.push_back(w - 1);
	cx.push_back(w);
	cy.push_back(0);
	cy.push_back(1);
	cy.push_back(h - 1);
	cy.push_back(h);
	vector < pair<Pii, Pii>> seg;

	FOR(i, m) {
		int pa, pb, qa, qb;
		cin >> pa >> pb >> qa >> qb;
		seg.emplace_back(Pii(pa, pb), Pii(qa, qb));
		if (pa > qa || pb > qb) swap(seg.back().first, seg.back().second);
		if(pa - 1 >= 0) cx.push_back(pa - 1);
		cx.push_back(pa);
		if (pa + 1 <= w) cx.push_back(pa + 1);
		if (qa - 1 >= 0) cx.push_back(qa - 1);
		cx.push_back(qa);
		if (qa + 1 <= w) cx.push_back(qa + 1);

		if (pb - 1 >= 0) cy.push_back(pb - 1);
		cy.push_back(pb);
		if (pb + 1 <= h) cy.push_back(pb + 1);
		if (qb - 1 >= 0) cy.push_back(qb - 1);
		cy.push_back(qb);
		if (qb + 1 <= h) cy.push_back(qb + 1);
	}
	compress(cx); compress(cy);
	auto co = [&cx, &cy](Pii xy) {
		int a = lower_bound(cx.begin(), cx.end(), xy.first) - cx.begin();
		int b = lower_bound(cy.begin(), cy.end(), xy.second) - cy.begin();
		return Pii(a, b);
	};
	tie(w, h) = co(Pii(w, h));

	FOR(i, m) {
		seg[i].first = co(seg[i].first);
		seg[i].second = co(seg[i].second);
	}

	FOR(x, sz(cx) - 2) FOR(y, sz(cy) - 1) {
		int nx = x + 1, ny = y;
		int c = 0;
		for (auto& s : seg) {
			if (s.first.first != nx) continue;
			if (s.second.first != nx) continue;
			if (s.first.second <= y && y < s.second.second) c = 1;
		}
		e[x][y].push_back(P(Pii(nx, ny), c));
		e[nx][ny].push_back(P(Pii(x, y), c));
	}
	FOR(x, sz(cx) - 1) FOR(y, sz(cy) - 2) {
		int nx = x , ny = y + 1;
		int c = 0;
		for (auto& s : seg) {
			if (s.first.second != ny) continue;
			if (s.second.second != ny) continue;
			if (s.first.first <= x && x < s.second.first) c = 1;
		}
		e[x][y].push_back(P(Pii(nx, ny), c));
		e[nx][ny].push_back(P(Pii(x, y), c));
	}

	int q; cin >> q;
	FOR(i, q) {
		int a, b, c, d; cin >> a >> b >> c >> d;
		Pii s(a, b), g(c, d);
		s = co(s); s.first--; s.second--;
		g = co(g); g.first--; g.second--;

		int ans = solve(s, g);
		cout << ans << endl;
	}
}

Compiler output:
a.cc: In function 'int solve(Pii, Pii)':
a.cc:27:9: error: 'memset' was not declared in this scope
   27 |         memset(cost, 0x1f, sizeof(cost));
      |         ^~~~~~
a.cc:7:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    6 | #include <deque>
  +++ |+#include <cstring>
    7 | #include <tuple>