FOJ 1408 位图

此题还是BFS的思想。

对每个为1的点进行BFS.

若BFS的值小于原来的,则进行替换。

这种题目注意格式问题了。。。

不要多个空格。。。。

 

#include <iostream> #include <queue> using namespace std; #define X 200 #define Y 200 struct Point_Struct { int x; int y; int flag; }; int BitMap[X+2][Y+2]; void BFS(int x,int y) { queue<Point_Struct> Queue; Point_Struct Temp_Point,Temp_Point2; int plusX[] = {0,1,0,-1}; int plusY[] = {1,0,-1,0}; int i; Temp_Point.x = x; Temp_Point.y = y; Temp_Point.flag = 0; Queue.push(Temp_Point); while (!Queue.empty()) { Temp_Point = Queue.front(); for (i = 0;i<4;i++) { if (BitMap[Temp_Point.x+plusX[i]][Temp_Point.y+plusY[i]]==-1|| BitMap[Temp_Point.x+plusX[i]][Temp_Point.y+plusY[i]]>Temp_Point.flag+1) { BitMap[Temp_Point.x+plusX[i]][Temp_Point.y+plusY[i]] = Temp_Point.flag+1; Temp_Point2.x = Temp_Point.x+plusX[i]; Temp_Point2.y = Temp_Point.y+plusY[i]; Temp_Point2.flag = Temp_Point.flag+1; Queue.push(Temp_Point2); } } Queue.pop(); } } int main() { int x,y; int i,j; char str[Y+1]; while (scanf("%d%d", &x,&y)!=EOF) { memset(BitMap, 0, sizeof(BitMap)); for (i=0;i<=y+1;i++) { BitMap[0][i] = -2; BitMap[x+1][i] = -2; } for (i=0;i<=x+1;i++) { BitMap[i][0] = -2; BitMap[i][y+1] = -2; } for (i=1;i<=x;i++) { scanf("%s", str); for (j=1;j<=y;j++) BitMap[i][j] = str[j-1]-'0'-1; } for (i=1;i<=x;i++) { for (j=1;j<=y;j++) if (BitMap[i][j] == 0) BFS(i,j); } for (i=1;i<=x;i++) { for (j=1;j<y;j++) printf("%d ", BitMap[i][j]); printf("%d/n",BitMap[i][j]); } } return 0; }

你可能感兴趣的:(struct)