我的洪水填充功能无法正常工作。 此分配的目的是查看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;
}
答案 0 :(得分: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
}
)
尝试:
map[i][j]
这将改变当前位置,只在需要时调用周围的填充
请注意,这仍然会跳过初始P,您可以在_
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()
)
此外,在map
中,您需要在检查{{1}}中的元素之前检查边界情况(如注释中的Gene暗示),否则您的边界检查毫无价值。