圖像有用區域(廣搜) - C++
標籤(空格分隔): 算法
題目
url http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=92
圖像有用區域
時間限制:3000 ms | 內存限制:65535 KB
難度:4
描述
“ACKing”同學以前做一個圖像處理的項目時,遇到了一個問題,他需要摘取出圖片中某個黑色線圏成的區域以內的圖片,現在請你來幫助他完成第一步,把黑色線圏外的區域全部變爲黑色。
已知黑線各處不會出現交叉(如圖2),並且,除了黑線上的點外,圖像中沒有純黑色(即像素爲0的點)。
輸入
第一行輸入測試數據的組數N(0 < N <= 6)
每組測試數據的第一行是兩個個整數W,H分表表示圖片的寬度和高度(3 <= W <= 1440,3 <= H <= 960)
隨後的H行,每行有W個正整數,表示該點的像素值。(像素值都在0到255之間,0表示黑色,255表示白色)
輸出
以矩陣形式輸出把黑色框之外的區域變黑之後的圖像中各點的像素值。
樣例輸入
1
5 5
100 253 214 146 120
123 0 0 0 0
54 0 33 47 0
255 0 0 78 0
14 11 0 0 0
樣例輸出
0 0 0 0 0
0 0 0 0 0
0 0 33 47 0
0 0 0 78 0
0 0 0 0 0
//
// main.cpp
// NYOJ92
//
// Created by jtusta on 2017/7/8.
// Copyright © 2017年 jtahstu. All rights reserved.
//
#include <iostream>
#include <queue>
using namespace std;
struct point
{
int x;
int y;
};
int w, h;
int map[970][1450];
int dir[4][4] = {-1, 0, 0, 1, 1, 0, 0, -1};
void BFS(int a, int b)
{
queue <point> q;
point t1, t2;
t1.x = a;
t1.y = b;
q.push(t1);
while(!q.empty())
{
t1= q.front();
q.pop();
for(int i = 0; i < 4; ++i)
{
t2.x = t1.x + dir[i][0];
t2.y = t1.y + dir[i][3];
if(t2.x < 0 || t2.x > h+1 || t2.y < 0 || t2.y > w+1 || map[t2.x][t2.y] == 0)
continue;
map[t2.x][t2.y] = 0;
q.push(t2);
}
}
}
int main()
{
int T;
cin >> T;
while(T--)
{
cin >> w >> h;
for(int i = 0; i <= h; ++i)
map[i][0] = map[i][w + 1] = 1;
for(int j = 0; j <= w; ++j)
map[0][j] = map[h + 1][j] = 1;
for(int i = 1; i <= h; ++i)
for(int j = 1; j <= w; ++j)
cin >> map[i][j];
BFS(0, 0);
for(int i = 1; i <= h; ++i)
for(int j = 1; j <= w; ++j)
{
if(j == w)
cout << map[i][j] << endl;
else
cout << map[i][j] << ' ';
}
}
return 0;
}