圖像有用區域

圖像有用區域

時間限制:3000 ms  |  內存限制:65535 KB
難度:4
描述

“ACKing”同學以前做一個圖像處理的項目時,遇到了一個問題,他需要摘取出圖片中某個黑色線圏成的區域以內的圖片,現在請你來幫助他完成第一步,把黑色線圏外的區域全部變爲黑色。

     

                圖1                                                        圖2 

已知黑線各處不會出現交叉(如圖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

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;

void bfs();
queue<int>q;
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int map[1500][1500] = {1};
int col, row;

int main()
{
	int n, i, j;
	cin>>n;
	while(n--)
	{
		while(!q.empty())
		{
			q.pop();
		}
		memset(map, -1, sizeof(map));
		cin>>col>>row;

		for(i = 1; i <= row; i++)
		for(j = 1; j <= col; j++)
			cin>>map[i][j];

		bfs();

		for(i = 1; i <= row; i++)
		{
			for(j = 1; j <= col; j++)
			{
				cout<<map[i][j];
				if(j < col)
					cout<<" ";
			}
			cout<<endl;
		}
	}

	return 0;
}

void bfs()
{
	int a, b, c, d;
	q.push(0);
	q.push(0);
	while(!q.empty())
	{
		a = q.front(); q.pop();
		b = q.front(); q.pop();
		for(int i = 0; i < 4; i++)
		{
			c = a + dir[i][0];
			d = b + dir[i][1];
			if(c < 0 || c > row + 1 || d < 0 || d > col + 1 || map[c][d] == 0)
				continue;
			map[c][d] = 0;
			q.push(c);
			q.push(d);
		}
	}
}


發佈了39 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章