Alex_tz307

Lee 3D

Sep 14th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin ("traseu.in");
  6. ofstream fout ("traseu.out");
  7.  
  8. int a[105][105][105];
  9. short N, M, niv_start, lin_start, col_start, niv_stop, lin_stop, col_stop, x, y, z;
  10. const short dniv[] = {1, 0, 0, 0, 0, -1}, dlin[] = {0, 1, 0, 0, -1, 0}, dcol[] = {0, 0, 1, -1, 0, 0};
  11. vector < tuple < short , short , short > > sol;
  12.  
  13. void Read () {
  14.   fin >> N >> M;
  15.   fin >> niv_start >> lin_start >> col_start >> niv_stop >> lin_stop >> col_stop;
  16.   while (M --) {
  17.     fin >> x >> y >> z;
  18.     a[x][y][z] = -1;
  19.   }
  20. }
  21.  
  22. void Border () {
  23.   for (short i = 0; i <= N + 1; i ++)
  24.     for (short j = 0; j <= N + 1; j ++)
  25.       a[0][i][j] = a[N + 1][i][j] = a[i][j][0] = a[i][j][N + 1] = a[i][0][j] = a[i][N + 1][j] = -1;
  26. }
  27.  
  28. void Lee () {
  29.   queue < tuple < short , short , short > > Q;
  30.   Q.push (tie (niv_start , lin_start , col_start));
  31.   a[niv_start][lin_start][col_start] = 1;
  32.   while (a[niv_stop][lin_stop][col_stop] == 0) {
  33.     tie(x, y, z) = Q.front();
  34.     Q.pop();
  35.     for (short k = 0; k < 6; k ++) {
  36.       short xv = x + dniv[k], yv = y + dlin[k], zv = z + dcol[k];
  37.       if (a[xv][yv][zv] == 0) {
  38.         a[xv][yv][zv] = a[x][y][z] + 1;
  39.         Q.push (tie (xv, yv, zv));
  40.       }
  41.     }
  42.   }
  43. }
  44.  
  45. int main () {
  46.   Read ();
  47.   Border ();
  48.   Lee ();
  49.   fout << a[niv_stop][lin_stop][col_stop] << '\n';
  50.   sol.push_back (tie (niv_stop , lin_stop , col_stop));
  51.   while (a[niv_stop][lin_stop][col_stop] > 1) {
  52.     for (short k = 0; k < 6; k ++) {
  53.       short xv = niv_stop - dniv[k], yv = lin_stop - dlin[k], zv = col_stop - dcol[k];
  54.       if (a[xv][yv][zv] == a[niv_stop][lin_stop][col_stop] - 1) {
  55.         niv_stop = xv, lin_stop = yv, col_stop = zv;
  56.         break;
  57.       }
  58.     }
  59.     sol.push_back (tie (niv_stop , lin_stop , col_stop));
  60.   }
  61.   reverse (sol.begin(), sol.end());
  62.   for (auto it : sol) {
  63.     tie (x, y, z) = it;
  64.     fout << x << ' ' << y << ' ' << z << '\n';
  65.   }
  66.   return 0;
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment