1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| #include <iostream> #include <cstring> #include <algorithm> #include <queue>
using namespace std;
const int N = 110, M = 310;
int ts[N][N]; int te[N][N]; typedef pair<pair<int, int>, int> PII;
bool is_safe(int x, int y, int time) { if (ts[x][y] == -1) return true; if (time >= ts[x][y] && time <= te[x][y]) { return false; } return true; }
int n, m, t; int dist[N][N][M];
int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0};
void bfs() { queue<PII> q; q.push({{1, 1}, 0}); dist[1][1][0] = 0; while(q.size()) { auto t = q.front(); q.pop(); int x = t.first.first, y = t.first.second; int time = t.second; if (x == n && y == m) return; for (int i = 0; i < 4; i ++) { int nx = x + dx[i], ny = y + dy[i]; int ntime = time + 1; if (nx < 1 || nx > n || ny < 1 || ny > m) continue; if (is_safe(nx, ny, ntime)) { if (dist[nx][ny][ntime] > dist[x][y][time] + 1) { dist[nx][ny][ntime] = dist[x][y][time] + 1; q.push({{nx,ny} , ntime}); } } } } }
int main() { memset(dist, 0x3f, sizeof dist); memset(ts, -1, sizeof ts); cin >> n >> m >> t; while(t --) { int r, c, a, b; scanf("%d%d%d%d", &r, &c, &a, &b); ts[r][c] = a, te[r][c] = b; } bfs(); int res = 2e9; for (int i = 0; i < M; i ++) { res = min(res, dist[n][m][i]); } cout << res << endl; return 0; }
|