题意是“ACKing”同学以前做一个图像处理的项目时,遇到了一个问题,他需要摘取出图片中某个黑色线圏成的区域以内的图片,现在请你来帮助他完成第一步,把黑色线圏外的区域全部变为黑色。
图1 图2
已知黑线各处不会出现交叉(如图2),并且,除了黑线上的点外,图像中没有纯黑色(即像素为0的点)。
-
输入
-
第一行输入测试数据的组数N(0<N<=6)
每组测试数据的第一行是两个个整数W,H分表表示图片的宽度和高度(3<=W<=1440,3<=H<=960)
随后的H行,每行有W个正整数,表示该点的像素值。(像素值都在0到255之间,0表示黑色,255表示白色)
输出
- 以矩阵形式输出把黑色框之外的区域变黑之后的图像中各点的像素值。
一开始想到的是dfs,但是交了几发后总是re,一想应该是栈溢出了,改用bfs,ac,思路见码
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
using namespace std;
#define LL long long
const int maxw = 1440 + 100;
const int maxh = 960 + 100;
int w,h;
int G[maxh][maxw];
const int dirx[] = {-1, 0, 1, 0};
const int diry[] = {0, -1, 0, 1};
void bfs(int x, int y) {
if(!G[x][y]) return;
queue<int> q;
int val = (x - 1) * w + y;
q.push(val);
G[x][y] = 0;
while(!q.empty()) {
int tval = q.front(); q.pop();
int tx = (tval - 1) / w + 1, ty = (tval - 1) % w + 1;
for(int i = 0; i < 4; i++) {
int nx = tx + dirx[i];
int ny = ty + diry[i];
if(nx < 1 || nx > h || ny < 1 || ny > w || !G[nx][ny]) continue;
G[nx][ny] = 0;
int nval = (nx - 1) * w + ny;
q.push(nval);
}
}
}
int main() {
//freopen("input.txt", "r", stdin);
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", &G[i][j]);
for(int i = 1; i <= w; i++) {bfs(1, i); bfs(h, i);}
for(int i = 1; i <= h; i++) {bfs(i, 1); bfs(i, w);}
for(int i = 1; i <= h; i++){
for(int j = 1; j <= w; j++)
if(j == 1) printf("%d", G[i][j]);
else printf(" %d", G[i][j]);
printf("\n");
}
}
return 0;
}