labsolve.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include <limits.h>
  6. #include <assert.h>
  7. #define MAXCOLS 256
  8. #define MAXROWS 256
  9. /* Data type: Labyrinth - ASCII map, costs, direction */
  10. typedef struct labyrinth
  11. {
  12. char lab[MAXCOLS+2][MAXROWS];
  13. long costs[MAXCOLS][MAXROWS];
  14. long bestx[MAXCOLS][MAXROWS];
  15. long besty[MAXCOLS][MAXROWS];
  16. int maxrow;
  17. int startx;
  18. int starty;
  19. int treasurex;
  20. int treasurey;
  21. }LabCell, *Lab_p;
  22. //prototypes
  23. Lab_p LabRead(FILE* in);
  24. void printLab(Lab_p);
  25. int main(int argc, char* argv[])
  26. {
  27. FILE *in = stdin;
  28. Lab_p lab = NULL, handle;
  29. char* inp;
  30. if(argc > 2)
  31. {
  32. fprintf(stderr, "Usage: %s [<file>]\n", argv[0]);
  33. exit(EXIT_FAILURE);
  34. }
  35. if(argc == 2)
  36. {
  37. in = fopen(argv[1], "r");
  38. if(!in)
  39. {
  40. perror(argv[0]);
  41. exit(EXIT_FAILURE);
  42. }
  43. }
  44. lab = LabRead(in);
  45. printLab(lab);
  46. return 0;
  47. }
  48. Lab_p LabRead(FILE* in)
  49. {
  50. char buff[MAXCOLS];
  51. int x = 0;
  52. char value;
  53. printf("allocating space\n");
  54. Lab_p map = malloc(sizeof(Lab_p));
  55. printf("allocated space\n");
  56. while(feof(in))
  57. {
  58. fgets(buff,MAXCOLS,in);
  59. strcpy(map->lab[x],buff);
  60. x++;
  61. }
  62. return map;
  63. }
  64. void printLab(Lab_p lab)
  65. {
  66. char buff[MAXCOLS];
  67. int i = 0;
  68. for(i; i < MAXROWS;i++)
  69. {
  70. strcpy(buff,lab->lab[i]);
  71. printf("%s \n",buff);
  72. }
  73. }