Tranvick

Maria

Sep 24th, 2014
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3.  
  4. using namespace std;
  5.  
  6. const int N = 555;
  7. const int INF = 1e9;
  8.  
  9. int map[N][N], d[N][N], n, m, k, xs, ys, xf, yf, dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
  10.  
  11. void bfs() {
  12.     queue<int> qx, qy;
  13.     for (int i = 0; i < n; ++i) {
  14.         for (int j = 0; j < m; ++j) {
  15.             d[i][j] = INF;
  16.         }
  17.     }
  18.     d[xf][yf] = 0;
  19.     qx.push(xf);
  20.     qy.push(yf);
  21.     while (!qx.empty()) {
  22.         int x = qx.front(); qx.pop();
  23.         int y = qy.front(); qy.pop();
  24.         for (int t = 0; t < 4; ++t) {
  25.             for (int l = 1;;++l) {
  26.                 int tx = x + dx[t] * l;
  27.                 int ty = y + dy[t] * l;
  28.                 if (tx < 0 || ty < 0 || tx >= n || ty >= m || map[tx][ty]) {
  29.                     break;
  30.                 }
  31.                 if (d[tx][ty] > d[x][y] + 1) {
  32.                     d[tx][ty] = d[x][y] + 1;
  33.                     qx.push(tx);
  34.                     qy.push(ty);
  35.                 }
  36.             }
  37.         }
  38.     }
  39. }
  40.  
  41. void brut(int x, int y) {
  42.     if (x == xf && y == yf) {
  43.         cout << yf << " " << xf << endl;
  44.     }
  45.     for (int t = 0; t < 4; ++t) {
  46.         for (int l = 1;;++l) {
  47.             int tx = x + dx[t] * l;
  48.             int ty = y + dy[t] * l;
  49.             if (tx < 0 || ty < 0 || tx >= n || ty >= m || map[tx][ty]) {
  50.                 break;
  51.             }
  52.             if (d[tx][ty] == d[x][y] - 1) {
  53.                 if (x == xs && y == ys) {
  54.                     cout << d[xs][ys] << endl;
  55.                 }
  56.                 cout << y << " " << x << endl;
  57.                 brut(tx, ty);
  58.             }
  59.         }
  60.     }
  61. }
  62.  
  63. int main() {
  64.     // freopen("input.txt", "r", stdin);
  65.     // freopen("output.txt", "w", stdout);
  66.     cin >> m >> n >> k;
  67.     for (int i = 0; i < k; ++i) {
  68.         int x, y;
  69.         cin >> x >> y;
  70.         map[y][x] = 1;
  71.     }
  72.     cin >> ys >> xs >> yf >> xf;
  73.     bfs();
  74.     brut(xs, ys);
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment