Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- using namespace std;
- const int N = 555;
- const int INF = 1e9;
- int map[N][N], d[N][N], n, m, k, xs, ys, xf, yf, dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
- void bfs() {
- queue<int> qx, qy;
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j) {
- d[i][j] = INF;
- }
- }
- d[xf][yf] = 0;
- qx.push(xf);
- qy.push(yf);
- while (!qx.empty()) {
- int x = qx.front(); qx.pop();
- int y = qy.front(); qy.pop();
- for (int t = 0; t < 4; ++t) {
- for (int l = 1;;++l) {
- int tx = x + dx[t] * l;
- int ty = y + dy[t] * l;
- if (tx < 0 || ty < 0 || tx >= n || ty >= m || map[tx][ty]) {
- break;
- }
- if (d[tx][ty] > d[x][y] + 1) {
- d[tx][ty] = d[x][y] + 1;
- qx.push(tx);
- qy.push(ty);
- }
- }
- }
- }
- }
- void brut(int x, int y) {
- if (x == xf && y == yf) {
- cout << yf << " " << xf << endl;
- }
- for (int t = 0; t < 4; ++t) {
- for (int l = 1;;++l) {
- int tx = x + dx[t] * l;
- int ty = y + dy[t] * l;
- if (tx < 0 || ty < 0 || tx >= n || ty >= m || map[tx][ty]) {
- break;
- }
- if (d[tx][ty] == d[x][y] - 1) {
- if (x == xs && y == ys) {
- cout << d[xs][ys] << endl;
- }
- cout << y << " " << x << endl;
- brut(tx, ty);
- }
- }
- }
- }
- int main() {
- // freopen("input.txt", "r", stdin);
- // freopen("output.txt", "w", stdout);
- cin >> m >> n >> k;
- for (int i = 0; i < k; ++i) {
- int x, y;
- cin >> x >> y;
- map[y][x] = 1;
- }
- cin >> ys >> xs >> yf >> xf;
- bfs();
- brut(xs, ys);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment