NYOJ92图像有用区域

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


这道题略坑,用深搜肯定就RE了,后来搜下了题解,用的广搜,还要进行加边处理。。。。


代码:

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;

typedef struct Node
{
    int x,y;
}Node;
int a[1000][1500];
int h,w;
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};

queue<Node> q;

void bfs(int stx,int sty)

{

    Node first;
    first.x = stx;
    first.y = sty;
    q.push(first);
    while(!q.empty())
    {
        Node head = q.front();
        q.pop();
        for(int i = 0;i < 4;++i)
        {
            Node t;
            t.x = head.x + dx[i];
            t.y = head.y + dy[i];
            if(t.x < 0 || t.x > h + 1 || t.y < 0 || t.y > w + 1 || !a[t.x][t.y])
                continue;
            q.push(t);
            a[t.x][t.y] = 0;
        }
    }
}
void addEdge()

{
    for(int i = 0;i <= w + 1;++i) a[0][i] = 1,a[h + 1][i] = 1;
    for(int i = 0;i <= h + 1;++i) a[i][0] = 1,a[i][w + 1] = 1;
}
int main()

{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&w,&h);
        for(int i = 1;i <= h;++i)
        {
            for(int j = 1;j <= w;++j)
                {
                    scanf("%d",&a[i][j]);
                }
        }
        addEdge();
        bfs(0,0);
        //printf("%d %d\n",stx,sty);
        for(int i = 1;i <= h;++i)
        {
            for(int j = 1;j <= w;++j)
                 printf(j == 1 ? "%d" : " %d",a[i][j]);
            printf("\n");
        }
    }
    return 0;
}


你可能感兴趣的:(搜索,ACM,广搜,加边)