ParanoidPanda

Untitled

Nov 10th, 2015
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. class PathFinder{
  6. private:
  7. int in_x, in_y, o_x, o_y, wave;
  8. int **lab;
  9. int **path;
  10. int l_row, l_col;
  11. std::string savefile;
  12. public:
  13. PathFinder(std::string filename, std::string savefile);
  14. void DoWave(int x, int y, int c_wave);
  15. void FindMinimal(int x, int y, int c_wave);
  16. void MergeIt();
  17. void SaveIt();
  18. ~PathFinder();
  19. };
  20.  
  21. PathFinder::PathFinder(std::string filename, std::string savefile){
  22. this->savefile = savefile;
  23. std::ifstream fin(filename.c_str());
  24. if (!fin.is_open()){
  25. std::cout << "File cannot be open!" << std::endl;
  26. exit(0);
  27. }
  28. else{
  29. fin >> l_row >> l_col;
  30.  
  31. lab = new int*[l_col];
  32. for (int i = 0; i < l_col; i++){
  33. lab[i] = new int[l_row];
  34. }
  35.  
  36. path = new int*[l_col];
  37. for (int i = 0; i < l_col; i++){
  38. path[i] = new int[l_row];
  39. }
  40.  
  41. for (int i = 0; i < l_col; i++){
  42. for (int j = 0; j < l_row; j++){
  43. fin >> lab[i][j];
  44. if (lab[i][j] == 2){
  45. in_x = i;
  46. in_y = j;
  47. lab[i][j] = 0;
  48. }
  49. if (lab[i][j] == 3){
  50. o_x = j;
  51. o_y = i;
  52. lab[i][j] = 0;
  53. }
  54. if (lab[i][j] == 1){
  55. lab[i][j] = -1;
  56. }
  57. }
  58. }
  59. }
  60. fin.close();
  61. DoWave(in_x, in_y, 0);
  62. }
  63.  
  64. void PathFinder::DoWave(int x, int y, int c_wave){
  65. lab[x][y] = c_wave;
  66. if (y + 1 < l_row){
  67. if ((lab[x][y + 1] == 0) || ((lab[x][y + 1] != -1) && (lab[x][y + 1] > c_wave))){
  68. DoWave(x, y + 1, c_wave + 1);
  69. }
  70. }
  71. if (x + 1 < l_col){
  72. if ((lab[x + 1][y] == 0) || ((lab[x + 1][y] != -1) && (lab[x + 1][y] > c_wave))){
  73. DoWave(x + 1, y, c_wave + 1);
  74. }
  75. }
  76. if (y - 1 >= 0){
  77. if ((lab[x][y - 1] == 0) || ((lab[x][y - 1] != -1) && (lab[x][y - 1] > c_wave))){
  78. DoWave(x, y - 1, c_wave + 1);
  79. }
  80. }
  81. if (x - 1 >= 0){
  82. if ((lab[x - 1][y] == 0) || ((lab[x - 1][y] != -1) && (lab[x - 1][y] > c_wave))){
  83. DoWave(x - 1, y, c_wave + 1);
  84. }
  85. }
  86. }
  87.  
  88. void PathFinder::SaveIt(){
  89. if (lab[o_y][o_x] > 0){
  90. FindMinimal(o_y, o_x, lab[o_y][o_x]);
  91. std::cout << "possible" << std::endl;
  92. MergeIt();
  93. std::ofstream file;
  94. file.open(savefile);
  95. for (int i = 0; i < l_col; i++){
  96. for (int j = 0; j < l_row; j++){
  97. if ((i == in_x) && (j == in_y)){
  98. file << 'S';
  99. }
  100. else if ((i == o_y) && (j == o_x)){
  101. file << 'F';
  102. }
  103. else{
  104. if ((lab[i][j] == 666)){
  105. file << ".";
  106. }
  107. else if (lab[i][j] == 0){
  108. file << " ";
  109. }
  110. else{
  111. file << '1';
  112. }
  113. }
  114. }
  115. file << std::endl;
  116. }
  117. file.close();
  118. }
  119. else{
  120. std::cout << "Unpossible" << std::endl;
  121. }
  122. }
  123.  
  124. void PathFinder::FindMinimal(int x, int y, int c_wave){
  125. if (path[x][y] != 0){
  126. path[x][y] = 666;
  127. if (y + 1 < l_row && ((lab[x][y + 1] == 0) || ((lab[x][y + 1] != -1) && (lab[x][y + 1] < c_wave)))){
  128. FindMinimal(x, y + 1, lab[x][y]);
  129. }
  130. else if (x + 1 < l_col && ((lab[x + 1][y] == 0) || ((lab[x + 1][y] != -1) && (lab[x + 1][y] < c_wave)))){
  131. FindMinimal(x + 1, y, lab[x][y]);
  132. }
  133. else if (y - 1 >= 0 && ((lab[x][y - 1] == 0) || ((lab[x][y - 1] != -1) && (lab[x][y - 1] < c_wave)))){
  134. FindMinimal(x, y - 1, lab[x][y]);
  135. }
  136. else if (x - 1 >= 0((lab[x - 1][y] == 0) || ((lab[x - 1][y] != -1) && (lab[x - 1][y] < c_wave)))){
  137. FindMinimal(x - 1, y, lab[x][y]);
  138. }
  139. }
  140. }
  141.  
  142. void PathFinder::MergeIt(){
  143. for (int i = 0; i < l_col; i++){
  144. for (int j = 0; j < l_row; j++){
  145. if (lab[i][j]>0){
  146. if (path[i][j] != 666)
  147. lab[i][j] = 0;
  148. else
  149. lab[i][j] = 666;
  150. }
  151. }
  152. }
  153. }
  154.  
  155. PathFinder::~PathFinder(){
  156. for (int i = 0; i < l_col; i++){
  157. delete[](lab[i]);
  158. delete[](path[i]);
  159. }
  160. delete[](lab);
  161. delete[](path);
  162. }
  163.  
  164. int main(int argc, char **argv){
  165. PathFinder test((std::string(argv[1])), (std::string(argv[2])));
  166. test.SaveIt();
  167. return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment