0

我的洪水填充功能无法正常工作时遇到了一些问题。此分配的目的是查看 P 和 C 在数组内是否连接。在洪水填充功能中,它似乎没有将“_”更改为“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;

}
4

1 回答 1

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]_rightfloodfill()来修复它main()

此外,您似乎从输入中读取了太多信息:

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

将为当前读取一个字符ij再次读取一个字符。如果您在第一种情况下读取换行符,则不需要将其存储在 中map[i,j],尤其是这样做会在第一次循环后更改地图j(现在具有值c

此外,在 中checklocation(),您需要在检查元素之前检查边界情况map(正如 Gene 在评论中暗示的那样),否则您的边界检查毫无价值。

于 2012-06-12T15:56:21.767 回答