Guest User

Untitled

a guest
Jan 18th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. /*
  4. READ THIS, AND THEN REPLACE THIS ENTIRE COMMENT WITH YOUR OWN
  5. Replace everything here with an opening comment, in your own words,
  6. describing the purpose of this program. Include your name and ID
  7. number in this comment.
  8.  
  9. By including your name in this file, you are confirming that you are
  10. the author of the code that follows and that you have not copied code
  11. from anyone else for this project. Modifying, or making superficial
  12. changes to someone else's code is not acceptable. THIS IS IMPORTANT.
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17.  
  18. #define MAX_COLS 100
  19. #define MAX_ROWS 100
  20.  
  21. #define MAX_ROWS 100 // The maximum number of rows for any map
  22. #define MAX_COLS 100 // The maximum number of cols for any map
  23. #define MAX_VAL 10000 // The maximum number of positions in any map
  24.  
  25. #define FILENAME "map1.txt" // The filename of the map
  26.  
  27. typedef struct {
  28. int col, row;
  29. } Position;
  30.  
  31. typedef struct {
  32. Position Queue[10000];
  33. int size;
  34. } List;
  35.  
  36. //Define functions:
  37. void InitList(List *a);
  38. void AddToList(List *a, Position point);
  39. Position RemoveFromList(List *a);
  40. int min(int a, int b);
  41.  
  42. /* Every function must begin with a comment, including this one */
  43. int main(void)
  44. {
  45. FILE *fp;
  46. int rows;
  47. int cols;
  48. char c;
  49. char map[MAX_COLS][MAX_ROWS];
  50. int distances[MAX_COLS][MAX_ROWS];
  51. List myList;
  52. int startx;
  53. int starty;
  54. int dist;
  55. int x = 0;
  56. int y = 0;
  57. Position pos, temp;
  58.  
  59. fp = fopen(FILENAME, "r");
  60.  
  61. if(fp == 0) {
  62. printf("Sorry, couldn't open file\n");
  63. return 0;
  64. }
  65. else {
  66. fscanf(fp, "%d", &rows);
  67. fscanf(fp, "%d", &cols);
  68. }
  69.  
  70. while((c = fgetc(fp)) != EOF) {
  71. if(c == '@') {
  72. map[x][y] = c;
  73. distances[x][y] = 0;
  74. startx = x;
  75. starty = y;
  76. x++;
  77. }
  78. else if(c == ' ' || c == '*' || (c >= 'A' && c <= 'Z')) {
  79. map[x][y] = c;
  80. distances[x][y] = MAX_VAL;
  81. x++;
  82. }
  83.  
  84. if(x == cols) {
  85. x = 0;
  86. y++;
  87. }
  88. }
  89.  
  90. printf("Starting position = (%d,%d)\n", starty, startx);
  91.  
  92. InitList(&myList);
  93.  
  94. pos.col = startx;
  95. pos.row = starty;
  96. AddToList(&myList, pos);
  97.  
  98. while(myList.size > 0) {
  99. pos = RemoveFromList(&myList);
  100.  
  101. if(distances[pos.col][pos.row] != MAX_VAL && map[pos.col][pos.row] != '@') {
  102. continue;
  103. }
  104.  
  105. //The distance to pos is (the minimum distance to any adjacent position) + 1
  106. if(map[pos.col][pos.row] != '@') {
  107. dist = min(min(min(distances[pos.col - 1][pos.row], distances[pos.col + 1][pos.row]), distances[pos.col][pos.row - 1]), distances[pos.col][pos.row + 1]) + 1;
  108. distances[pos.col][pos.row] = dist;
  109. }
  110.  
  111. if(map[pos.col][pos.row] >= 'A' && map[pos.col][pos.row] <= 'Z') {
  112. printf("Distance to %c (%d,%d) = %d\n", map[pos.col][pos.row], pos.row, pos.col, distances[pos.col][pos.row]);
  113. }
  114.  
  115. if(map[pos.col - 1][pos.row] != '*') {
  116. temp.col = pos.col - 1;
  117. temp.row = pos.row;
  118. AddToList(&myList, temp);
  119. }
  120.  
  121. if(map[pos.col + 1][pos.row] != '*') {
  122. temp.col = pos.col + 1;
  123. temp.row = pos.row;
  124. AddToList(&myList, temp);
  125. }
  126.  
  127. if(map[pos.col][pos.row - 1] != '*') {
  128. temp.col = pos.col;
  129. temp.row = pos.row - 1;
  130. AddToList(&myList, temp);
  131. }
  132.  
  133. if(map[pos.col][pos.row + 1] != '*') {
  134. temp.col = pos.col;
  135. temp.row = pos.row + 1;
  136. AddToList(&myList, temp);
  137. }
  138. }
  139.  
  140. return 0;
  141. }
  142.  
  143. void InitList(List *a)
  144. {
  145. a -> size = 0;
  146. }
  147.  
  148. void AddToList(List *a, Position point)
  149. {
  150. /*Point to the values array at the last position, and make this equal the input value*/
  151. a->Queue[a->size].row = point.row;
  152. a->Queue[a->size].col = point.col;
  153. /*Increase the size*/
  154. a->size++;
  155. }
  156.  
  157. Position RemoveFromList(List *a)
  158. {
  159. Position ret;
  160. /*Initialise variable i and set it to 0*/
  161. int i = 0;
  162. /*Set the *value pointer to = the first position in the size array*/
  163. ret = a->Queue[0];
  164. /*Create a for loop to cycle through the Queue array and shit each element to the left, then reduce the value of size*/
  165. for(i = 0; i < (a->size-1); i++) {
  166. a->Queue[i] = a->Queue[i+1];
  167. }
  168. a->size--;
  169.  
  170. return ret;
  171. }
  172.  
  173. int min(int a, int b)
  174. {
  175. if(a < b) {
  176. return a;
  177. }
  178. else {
  179. return b;
  180. }
  181. }
Add Comment
Please, Sign In to add comment