fork(1) download
  1. #include <stdio.h>
  2. #define MAXC 10
  3. #define MAXR 10
  4.  
  5. void floodfill(char map[][MAXC+1], int i, int j, int r, int c);
  6. int checklocation(char map[][MAXC+1], int i, int j, int r, int c);
  7.  
  8. int main() {
  9. // FILE* ifp = fopen("bunnies.in", "r");
  10.  
  11. printf("start\n");
  12.  
  13. int numcases, loop;
  14. // fscanf(ifp, "%d", &numcases);
  15. scanf("%d", &numcases);
  16.  
  17. printf("read cases: %d\n", numcases);
  18.  
  19. for (loop=0; loop<numcases; loop++) {
  20. int r, c, i=0, j=0;
  21.  
  22. // fscanf(ifp, "%d%d", &r, &c);
  23. scanf("%d%d", &r, &c);
  24. //printf("\nRows = %d Cols = %d\n", r,c); //debug comment out
  25. char map[r][c];
  26.  
  27. //Read in input
  28. for(i=0; i<r; i++) {
  29. // fgetc(ifp);
  30.  
  31. for (j=0; j<c; j++) {
  32. // map[i][j] = fgetc(ifp);
  33. map[i][j] = getchar();
  34. //printf("%c", map[i][j]); //test input read comment out
  35. }
  36. // printf("\n"); //test input read comment out
  37. }
  38.  
  39. printf("read input\n");
  40.  
  41. int broken = 0; //to keep track if floodfill already occured
  42.  
  43. for (i=0; i<r; i++) {
  44. // if (broken == 1)
  45. // continue;
  46.  
  47. for (j=0; j<c; j++) {
  48. // if (broken == 1)
  49. // continue;
  50.  
  51. //the whole loop only looks for P then floodfills
  52. if(map[i][j] == 'P') {
  53. map[i][j] = '_';
  54. printf("calling floodfill on %d, %d", i, j);
  55. floodfill(map, i, j, r, c);
  56. // broken = 1;
  57. }
  58.  
  59. // printf("%c", map[i][j]); //test floodfill, comment out later
  60. }
  61.  
  62. //printf("\n"); //test floodfill, comment out later
  63. }
  64.  
  65. printf("after floodfill\n");
  66.  
  67. int found = 0;
  68.  
  69. //searches for C, calls checklocation when found
  70. for (i=0; i<r; i++) {
  71. for(j=0; j<c; j++) {
  72. if (map[i][j] == 'C')
  73. found = checklocation(map, i,j, r, c);
  74. }
  75. }
  76.  
  77. if (found == 1)
  78. printf("yes\n");
  79. else
  80. printf("no\n");
  81.  
  82. }
  83.  
  84. // fclose(ifp);
  85. return 0;
  86.  
  87. }
  88.  
  89.  
  90. //Pass map pointer, position in array i,j and row/column numbers
  91. void floodfill(char map[][MAXC+1], int i, int j, int r, int c) {
  92.  
  93. //printf("looking at: [%d][%d]\n", i,j); //debug comment out later
  94.  
  95. //'base case' that deals with out of bounds
  96. if (i<0 || j<0 || i>=r || j>=c)
  97. return;
  98.  
  99. if (map[i][j] == '_') {
  100. map[i][j] = 'P';
  101.  
  102. floodfill(map, r, c, i, j+1); //check right
  103. floodfill(map, r, c, i, j-1); //check left
  104. floodfill(map, r, c, i+1, j); //check below
  105. floodfill(map, r, c, i-1, j); //check above
  106. }
  107.  
  108. //printf("%c", map[i][j]);
  109.  
  110. }
  111.  
  112. //Same parameters as floodfill
  113. int checklocation(char map[][MAXC+1], int i, int j, int r, int c) {
  114.  
  115. //these if statements check for p in each location around and
  116. //makes sure the coordinate is in bounds
  117. if ((i-1>=0) && (i-1 < r) && (j>=0) && (j < c) && (map[i-1][j] == 'P'))
  118. return 1;
  119.  
  120. else if ((i+1>=0) && (i+1 < r) && (j>=0) && (j < c) && (map[i+1][j] == 'P'))
  121. return 1;
  122.  
  123. else if ((i>=0) && (i < r) && (j+1>=0) && (j+1 < c) && (map[i][j+1] == 'P'))
  124. return 1;
  125.  
  126. else if ((i>=0) && (i < r) && (j-1>=0) && (j-1 < c) && (map[i][j-1] == 'P'))
  127. return 1;
  128.  
  129. else
  130. return 0;
  131.  
  132. }
Success #stdin #stdout 0.01s 1724KB
stdin
5
1 2
PC
2 1
P
C
2 2
P#
#C
2 2
P_
C_
8 7
__P____
####_##
_____#_
_____#C
##_###_
_____#_
___#_#_
___#___
5 7
__P____
####_##
_____#_
_____#C
##_###_
stdout
start
read cases: 5
read input
calling floodfill on 0, 0after floodfill
yes
read input
calling floodfill on 0, 0after floodfill
yes
read input
calling floodfill on 0, 0after floodfill
no
read input
calling floodfill on 0, 0after floodfill
yes
read input
calling floodfill on 0, 2after floodfill
no