ParanoidPanda

Untitled

Oct 31st, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. int **lab;
  5. int **path;
  6. int lrow, lcol;
  7. int start_x, start_y, finish_x, finish_y;
  8.  
  9. void LoadIt(const char *filename);
  10. void Dowave(int x, int y, int countofwave);
  11. void FindPath(int x, int y, int countofwave);
  12. void MergeIt();
  13. void SaveIt(const char *filename);
  14.  
  15. void LoadIt(const char *filename){
  16. FILE *input = fopen(filename, "r");
  17. if (input == NULL){
  18. printf("Cannot open input file \n");
  19. exit(0);
  20. }
  21. else{
  22. fscanf(input, "%d%d", &lrow, &lcol);
  23. lab = (int **)malloc(lcol*sizeof(int *));
  24. path = (int **)malloc(lcol*sizeof(int *));
  25. for (int i = 0; i < lcol; i++){
  26. lab[i] = (int *)malloc(lrow *sizeof(int));
  27. path[i] = (int *)malloc(lrow *sizeof(int));
  28. }
  29. for (int i = 0; i < lcol; i++){
  30. for (int j = 0; j < lrow; j++){
  31. fscanf(input, "%d", &lab[i][j]);
  32. if (lab[i][j] == 1)
  33. lab[i][j] = -1;
  34. if (lab[i][j] == 2){
  35. start_y = j;
  36. start_x = i;
  37. lab[i][j] = 0;
  38. }
  39. if (lab[i][j] == 3){
  40. finish_y = i;
  41. finish_x = j;
  42. lab[i][j] = 0;
  43. }
  44. }
  45. }
  46. }
  47. fclose(input);
  48. }
  49.  
  50. void Dowave(int x, int y, int countofwave){
  51. lab[x][y] = countofwave;
  52. if (y + 1 < lrow){
  53. if ((lab[x][y + 1] == 0) || ((lab[x][y + 1] != -1) && (lab[x][y + 1] > countofwave))){
  54. Dowave(x, y + 1, countofwave + 1);
  55. }
  56. }
  57. if (x + 1 < lcol){
  58. if ((lab[x + 1][y] == 0) || ((lab[x + 1][y] != -1) && (lab[x + 1][y] > countofwave))){
  59. Dowave(x + 1, y, countofwave + 1);
  60. }
  61. }
  62. if (y - 1 >= 0){
  63. if ((lab[x][y - 1] == 0) || ((lab[x][y - 1] != -1) && (lab[x][y - 1]> countofwave))){
  64. Dowave(x, y - 1, countofwave + 1);
  65. }
  66. }
  67. if (x - 1 >= 0){
  68. if ((lab[x - 1][y] == 0) || ((lab[x - 1][y] != -1) && (lab[x - 1][y] > countofwave))){
  69. Dowave(x - 1, y, countofwave + 1);
  70. }
  71. }
  72. }
  73.  
  74. void FindPath(int x, int y, int countofwave){
  75. path[x][y] = 666;
  76. if (y + 1 < lrow){
  77. if ((lab[x][y + 1] == 0) || ((lab[x][y + 1] != -1) && (lab[x][y + 1] == countofwave - 1))){
  78. FindPath(x, y + 1, countofwave - 1);
  79. }
  80. }
  81. if (x + 1 < lcol){
  82. if ((lab[x + 1][y] == 0) || ((lab[x + 1][y] != -1) && (lab[x + 1][y] == countofwave - 1))){
  83. FindPath(x + 1, y, countofwave - 1);
  84. }
  85. }
  86. if (y - 1 >= 0){
  87. if ((lab[x][y - 1] == 0) || ((lab[x][y - 1] != -1) && (lab[x][y - 1] == countofwave - 1))){
  88. FindPath(x, y - 1, countofwave - 1);
  89. }
  90. }
  91. if (x - 1 >= 0){
  92. if ((lab[x - 1][y] == 0) || ((lab[x - 1][y] != -1) && (lab[x - 1][y] == countofwave - 1))){
  93. FindPath(x - 1, y, countofwave - 1);
  94. }
  95. }
  96. }
  97.  
  98. void SaveIt(const char *filename){
  99. FILE *output = fopen(filename, "w+");
  100. for (int i = 0; i < lcol; i++){
  101. for (int j = 0; j < lrow; j++){
  102. if (i == start_x && j == start_y)
  103. fprintf(output, "S");
  104. else if (i == finish_y && j == finish_x)
  105. fprintf(output, "F");
  106. else{
  107. if (lab[i][j] == -1)
  108. fprintf(output, "1");
  109. if (path[i][j] == 666)
  110. fprintf(output, "*");
  111. if (path[i][j] != 666 && lab[i][j] != -1)
  112. fprintf(output, " ");
  113. }
  114. }
  115. fprintf(output,"\n");
  116. }
  117. fclose(output);
  118. }
  119.  
  120. int main(){
  121. LoadIt("lab.txt");
  122. Dowave(start_x, start_y, 0);
  123. if (lab[start_y][finish_x] > 0){
  124. printf("possible \n");
  125. FindPath(finish_y, finish_x, lab[finish_y][finish_x]);
  126. SaveIt("result.txt");
  127. }
  128. else
  129. exit(0);
  130. free(lab);
  131. free(path);
  132. return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment