Advertisement
a53

GigelAjungeAcasa

a53
Oct 14th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #include <fstream>
  2.  
  3. using namespace std;
  4.  
  5. bool okay (unsigned short int i, unsigned short int j);
  6.  
  7. unsigned short int N, M, K;
  8. unsigned short int X1, Y1, X2, Y2;
  9. unsigned short int XY, YY, XB, YB;
  10. unsigned short int X[14001], Y[14001];
  11.  
  12. const short int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
  13. const short int dy[] = { 0, 1, 1, 1, 0, -1, -1, -1};
  14. short int matrix[1001][1001];
  15. unsigned int i, j, k, nextI, nextJ;
  16.  
  17. struct Queue
  18. {
  19. unsigned short int x;
  20. unsigned short int y;
  21. };
  22. Queue Q[1000010];
  23. unsigned int first, last;
  24.  
  25. unsigned short int D;
  26.  
  27. int main ()
  28. {
  29. ifstream fin ("gigelajungeacasa.in");
  30. fin >> N >> M >> K;
  31. fin >> X1 >> Y1 >> X2 >> Y2;
  32. fin >> XY >> YY >> XB >> YB;
  33. for (i=1; i<=K; i++)
  34. fin >> X[i] >> Y[i];
  35. fin.close();
  36. matrix[XY][YY] = -1;
  37. matrix[XB][YB] = -1;
  38. for (i=1; i<=K; i++)
  39. matrix[X[i]][Y[i]] = -1;
  40. Q[first].x = X2;
  41. Q[first].y = Y2;
  42. while (first <= last)
  43. {
  44. i = Q[first].x;
  45. j = Q[first].y;
  46. first++;
  47. for (k=0; k<8; k++)
  48. {
  49. nextI = i + dx[k];
  50. nextJ = j + dy[k];
  51. if (okay(nextI,nextJ) == 1 && matrix[nextI][nextJ] == 0)
  52. {
  53. matrix[nextI][nextJ] = matrix[i][j] + 1;
  54. last++;
  55. Q[last].x = nextI;
  56. Q[last].y = nextJ;
  57. }
  58. }
  59. }
  60. D = matrix[X1][Y1];
  61. ofstream fout ("gigelajungeacasa.out");
  62. fout << D;
  63. fout.close();
  64. return 0;
  65. }
  66.  
  67. bool okay (unsigned short int i, unsigned short int j)
  68. {
  69. if (i<1 || j<1 || i>N || j>M)
  70. return 0;
  71. return 1;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement