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]することで修正できます。_floodfill()main()

また、入力から読みすぎているようです。

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

現在の文字を読み取り、それぞれにi1つずつ読み取りますj。最初のケースで改行を読んでいる場合は、それをに格納する必要はありませんmap[i,j]。特に、そうすると、最初のループスルーj(現在は値を持つc)の後にマップが変更されます。

また、ではchecklocation()、要素をチェックインする前に境界ケースをチェックする必要がありますmap(Geneがコメントで示唆しているように)。そうでない場合、境界チェックは無意味です。

于 2012-06-12T15:56:21.767 に答える