Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<fstream>
  4.  
  5. using namespace std;
  6.  
  7. int count = 0;
  8.  
  9. int lucas(int n)
  10. {
  11. if (n == 0)
  12. return 2;
  13. if (n == 1)
  14. return 1;
  15.  
  16. return lucas(n - 1) + lucas(n - 2);
  17. }
  18.  
  19. bool sciezka(char** lab, int x, int y, int maxX, int maxY)
  20. {
  21. count++;
  22. if (x > maxX || x < 0 || y > maxY || y < 0)
  23. {
  24. return false;
  25. } //granice labiryntu
  26.  
  27. if (lab[x][y] == 2)
  28. {
  29. return false;
  30. }; //backtracking
  31.  
  32. if (x == 0 && y == 0)
  33. {
  34. return true;
  35. }; //znalezienie wyjscia
  36.  
  37. if (lab[x][y] == 2 || lab[x][y] == 0)
  38. { return false;
  39. }//kolejnosc jest wazna
  40.  
  41. lab[x][y] = 2; // oznaczenie drogi
  42.  
  43. if (sciezka(lab, x + 1, y, maxX, maxY) == true)
  44. {
  45. return true;
  46. }
  47. if (sciezka(lab, x - 1, y, maxX, maxY) == true)
  48. {
  49. return true;
  50. }
  51. if (sciezka(lab, x, y + 1, maxX, maxY) == true)
  52. {
  53. return true;
  54. }
  55. if (sciezka(lab, x, y - 1, maxX, maxY) == true)
  56. {
  57. return true;
  58. } //implementacja pseudokodu
  59.  
  60. return false;
  61. }
  62. int main()
  63. {
  64. std::ifstream file("tab.txt", std::ifstream::in);
  65. int x, y;
  66. file >> x >> y;
  67. char** lab = new char*[x];
  68. for (int i = 0; i < x; i++)
  69. *lab = new char[y];
  70.  
  71. for (int i = 0; i < x; i++)
  72. {
  73. for (int j = 0; j < y; j++)
  74. {
  75. file >> lab[i][j];
  76. }
  77. }
  78.  
  79. if (sciezka(lab, x - 1, y - 1, x, y) == true)
  80.  
  81. {
  82. cout << "udalo sie " << endl;
  83. };
  84.  
  85. for (int i = 0; i < x; ++i)
  86. {
  87. for (int j = 0; j < y; ++j)
  88. cout << +lab[i][j] << " "; //+dla wynikow liczbowych
  89. cout << "\n";
  90. }
  91.  
  92.  
  93. cout << count;
  94. getchar();
  95.  
  96. for (int i = 0; i < x; i++)
  97. {
  98. delete[] lab[i];
  99. }
  100.  
  101. delete[] lab;
  102.  
  103. return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement