[Softeer] 이미지 프로세싱

rhkr9080·2022년 10월 19일
0

Softeer

목록 보기
3/10

문제링크 : https://softeer.ai/practice/info.do?idx=1&eid=627

💻 문제 풀이 : C++

#include <iostream>
#include <queue>
#include <cstring>
#define MAX 130
using namespace std;

struct Coord {
	int row, col;
};

int H, W;
int MAP[MAX][MAX];
int visited[MAX][MAX];
int dr[4] = { 0, 0, -1 ,1 };
int dc[4] = { -1,1,0,0 };
void BFS(int row, int col, int value, int key)
{
	queue<Coord> nowQ;
	nowQ.push({ row, col });
	visited[row][col] = 1;
	//MAP[row][col] = value;
	while (!nowQ.empty()) {
		Coord now = nowQ.front();
		MAP[now.row][now.col] = value;
		nowQ.pop();
		for (int i = 0; i < 4; i++)
		{
			int next_row = now.row + dr[i];
			int next_col = now.col + dc[i];
			if (next_row < 0 || next_col < 0 || next_row > H || next_col > W) continue;
			if (MAP[next_row][next_col] != key) continue;
			if (visited[next_row][next_col] == 1) continue;
			visited[next_row][next_col] = 1;
			MAP[next_row][next_col] = value;
			nowQ.push({ next_row, next_col });
		}
	}
}

int main()
{
	cin >> H >> W;
	for (int i = 1; i <= H; i++)
		for (int j = 1; j <= W; j++)
			cin >> MAP[i][j];

	int Q;
	cin >> Q;
	for (int q = 0; q < Q; q++)
	{
		int i, j, c;
		cin >> i >> j >> c;
		memset(visited, 0, sizeof(visited));
		int key = MAP[i][j];
		BFS(i, j, c, key);
	}

	for (int i = 1; i <= H; i++)
	{
		for (int j = 1; j <= W; j++) {
			cout << MAP[i][j] << " ";
		}
		cout << endl;
	}

	return (0);
}
profile
공부방

0개의 댓글