NYoj 92 图像有用区域

题目来源:http://acm.nyist.net/JudgeOnline/problem.php?pid=92

这个题只需要在原来图像周围加一圈1,利用广搜,可以得到最后结果!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

const int MAXN = 1000;
const int MAXM = 1500;

int Graph[MAXN][MAXM];
int row, col;

int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};

bool Is_CanGo(int x, int y)
{
    if(x < 0 || x > row+1 || y < 0 || y > col+1)
        return false;
    return true;
}

void BFS(int x, int y)
{
    queue <int> Que;
    Que.push(x);
    Que.push(y);
    while(!Que.empty())
    {
        int a = Que.front();
        Que.pop();
        int b = Que.front();
        Que.pop();
        for(int i = 0; i < 4; ++i)
        {
            int xx = a + dir[i][0];
            int yy = b + dir[i][1];
            if(Is_CanGo(xx, yy))
            {
                if(Graph[xx][yy] != 0)
                {
                    Graph[xx][yy] = 0;
                    Que.push(xx);
                    Que.push(yy);
                }
            }
        }
    }
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d %d", &col, &row);
        for(int i = 0; i <= row+1; ++i)
        {
            Graph[i][0] = 1;
            Graph[i][col+1] = 1;
        }
        for(int i = 0; i <= col+1; ++i)
        {
            Graph[0][i] = 1;
            Graph[row+1][i] = 1;
        }

        for(int i = 1; i <= row; ++i)
        {
            for(int j = 1; j <= col; ++j)
            {
                scanf("%d", &Graph[i][j]);
            }
        }

        BFS(0, 0);
        for(int i = 1; i <= row; ++i)
        {
            for(int j = 1; j < col; ++j)
                printf("%d ", Graph[i][j]);
            printf("%d\n", Graph[i][col]);
        }

    }
    return 0;
}


你可能感兴趣的:(搜索)