YorKnEz

Untitled

Jan 25th, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. typedef struct Punct {
  8. int l;
  9. int c;
  10. } Punct;
  11.  
  12. int n, **a, x;
  13. Punct *coada, punct;
  14.  
  15. int dl[] = {-1, 0, 1 , 0},
  16. dc[] = {0, 1, 0 , -1};
  17.  
  18. bool oob (int l, int c) {
  19. if (l < 0 || l >= n) {
  20. return true;
  21. } else if (c < 0 || c >= n) {
  22. return true;
  23. } else return false;
  24. }
  25.  
  26. void init (Punct *coada) {
  27. for (int i = 0; i < n*n; i++) {
  28. coada[i].l = 0;
  29. coada[i].c = 0;
  30. }
  31. }
  32.  
  33. void lee () {
  34. int p = 0, u = 0;
  35. bool exit = false;
  36.  
  37. coada[p].l = 0;
  38. coada[p].c = 0;
  39.  
  40. while (p <= u && !exit) {
  41. if (a[coada[p].l][coada[p].c] == x) {
  42. punct.l = coada[p].l;
  43. punct.c = coada[p].c;
  44.  
  45. exit = true;
  46. }
  47.  
  48. for (int i = 0; i < 4; i++) {
  49. if (!oob(coada[p].l + dl[i], coada[p].c + dc[i]) && a[coada[p].l + dl[i]][coada[p].c + dc[i]] > a[coada[p].l][coada[p].c] && a[coada[p].l + dl[i]][coada[p].c + dc[i]] <= x) {
  50. u++;
  51.  
  52. coada[u].l = coada[p].l + dl[i];
  53. coada[u].c = coada[p].c + dc[i];
  54. }
  55. }
  56.  
  57. p++;
  58. }
  59. }
  60.  
  61. int main()
  62. {
  63. /* INPUT HERE:
  64. 4
  65. 1 3 9 10
  66. 2 4 11 13
  67. 5 6 12 14
  68. 7 15 16 19
  69. 13
  70. */
  71.  
  72. scanf("%d", &n);
  73.  
  74. //--
  75.  
  76. a = (int **) malloc (sizeof(int *) * n);
  77.  
  78. for (int i = 0; i < n; i++) {
  79. a[i] = (int *) malloc (sizeof(int) * n);
  80. }
  81.  
  82. coada = (Punct *) malloc (sizeof(Punct) * n * n);
  83.  
  84. init(coada);
  85. //--
  86.  
  87. for (int i = 0; i < n; i++) {
  88. for (int j = 0; j < n; j++) {
  89. scanf("%d", &a[i][j]);
  90. }
  91. }
  92.  
  93. scanf("%d", &x);
  94.  
  95. lee();
  96.  
  97. printf("%d %d", punct.l+1, punct.c+1);
  98.  
  99. free(a);
  100. free(coada);
  101.  
  102. return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment