我的Floodfill功能无法正常运行时遇到了一些麻烦。
分配的目的是查看阵列中是否连接了P和C。
在Floodfill函数中,它似乎并未将'_'更改为'P'

样品输入

5
1 2
PC
2 1
P
C
2 2
P#
#C
2 2
P_
C_
8 7
__P____
####_##
_____#_
_____#C
##_###_
_____#_
___#_#_
___#___
5 7
__P____
####_##
_____#_
_____#C
##_###_




#include <stdio.h>
#define MAXC 10
#define MAXR 10

void floodfill(char map[][MAXC+1], int i, int j, int r, int c);
int checklocation(char map[][MAXC+1], int i, int j, int r, int c);

int main() {


    FILE* ifp = fopen("bunnies.in", "r");

    int numcases, loop;
    fscanf(ifp, "%d", &numcases);


    for (loop=0; loop<numcases; loop++) {

        int r, c, i=0, j=0;


        fscanf(ifp, "%d%d", &r, &c);
        //printf("\nRows = %d Cols = %d\n", r,c); //debug comment out
        char map[r][c];

        //Read in input
        for(i=0; i<r; i++) {

            map[i][j] = fgetc(ifp);

                for (j=0; j<c; j++) {
                    map[i][j] = fgetc(ifp);
                    //printf("%c", map[i][j]); //test input read comment out
                }
           // printf("\n"); //test input read comment out
        }

        int broken = 0; //to keep track if floodfill already occured

        for (i=0; i<r; i++) {

           // if (broken == 1)
            //    continue;

            for (j=0; j<c; j++) {

               // if (broken == 1)
                //    continue;

                //the whole loop only looks for P then floodfills
                if(map[i][j] == 'P') {
                    floodfill(map, i, j, r, c);
                 //   broken = 1;


                }

               // printf("%c", map[i][j]); //test floodfill, comment out later

            }

            //printf("\n"); //test floodfill, comment out later

        }

        int found = 0;

        //searches for C, calls checklocation when found
        for (i=0; i<r; i++) {
            for(j=0; j<c; j++) {
                if (map[i][j] == 'C')
                    found = checklocation(map, i,j, r, c);
            }
        }

        if (found == 1)
            printf("yes\n");
        else
            printf("no\n");

    }

fclose(ifp);
return 0;

}


//Pass map pointer, position in array i,j and row/column numbers
void floodfill(char map[][MAXC+1], int i, int j, int r, int c) {

//printf("looking at: [%d][%d]\n", i,j); //debug comment out later

//'base case' that deals with out of bounds
if (i<0 || j<0 || i>=r || j>=c)
    return;

if (map[i][j] != '_')
    return;

if (map[i][j] == '_')
    map[i][j] = 'P';



floodfill(map, r, c, i, j+1); //check right
floodfill(map, r, c, i, j-1); //check left
floodfill(map, r, c, i+1, j); //check below
floodfill(map, r, c, i-1, j); //check above

//printf("%c", map[i][j]);

}

//Same parameters as floodfill
int checklocation(char map[][MAXC+1], int i, int j, int r, int c) {

//these if statements check for p in each location around and
//makes sure the coordinate is in bounds
if (map[i-1][j] == 'P' && ((i>=0 && i < r) && (j>=0 && j < c)))
    return 1;

else if (map[i+1][j] == 'P' && ((i>=0 && i < r) && (j>=0 && j < c)))
    return 1;

else if (map[i][j+1] == 'P' && ((i>=0 && i < r) && (j>=0 && j < c)))
    return 1;

else if (map[i][j-1] == 'P' && ((i>=0 && i < r) && (j>=0 && j < c)))
    return 1;

else
    return 0;

}

最佳答案

您的floodfill()似乎已损坏:

当您在P的位置调用它时,以下检查

if (map[i][j] != '_')
    return;


将立即返回而无需执行任何操作(因为map[i][j]P而不是_

尝试:

if (map[i][j] == '_') {
  map[i][j] = 'P';

  floodfill(map, r, c, i, j+1); //check right
  floodfill(map, r, c, i, j-1); //check left
  floodfill(map, r, c, i+1, j); //check below
  floodfill(map, r, c, i-1, j); //check above
}


这将更改当前位置,仅在需要时调用周围的洪水填充

请注意,这仍然会跳过初始P,您可以在从map[i][j]调用_之前将floodfill()设置为main()来解决此问题。

另外,您似乎从输入中读取了太多内容:

map[i][j] = fgetc(ifp);
for (j=0; j<c; j++) {
  map[i][j] = fgetc(ifp);
  ...


将为当前i读取一个字符,并为每个j再次读取一个字符。如果您是在第一种情况下阅读换行符,则无需将其存储在map[i,j]中,特别是这样做会在通过j的第一个循环(现在值为)

同样,在c中,需要检查边界情况,然后再检查checklocation()中的元素(如注释中的Gene所示),否则边界检查毫无价值。

10-08 00:18