티스토리 뷰
문제 출처: https://www.acmicpc.net/problem/5547
5547번: 일루미네이션
첫째 줄에 두 개의 정수 W와 H가 주어진다. (1 ≤ W, H ≤ 100) 다음 H줄에는 상근이네 집의 건물 배치가 주어진다. i+1줄에는 W개의 정수가 공백으로 구분되어 있다. j번째 (1 ≤ j ≤ w) 정수의 좌표는
www.acmicpc.net
이 문제는 2638 치즈 문제와 비슷하게 바깥영역을 bfs로 표시해주면 안쪽과 바깥쪽을 구분할 수 있다.
육각형 구조에 맞추어 인접 인덱스를 잘 구분해야한다.
추가적으로 메모리 초과가 발생했는데 이를 막기 위해서는 bfs 과정에서 visited 검사를 해줘야 한다.
그렇지 않으면 이미 큐에 존재하는 요소가 또 삽입 될 수 있다.
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef pair<int, int>P;
int arr[101][101];
int W, H;
//해당 x,y의 인접 인덱스를 계산 pos = 0(왼쪽위),1(오른쪽위),2(오른쪽)...
P calAdjIdx(int x, int y,int pos) {
P idx;
if (y % 2 == 1) {
switch (pos)
{
case 0:
idx.first = x;
idx.second = y - 1;
break;
case 1:
idx.first = x + 1;
idx.second = y - 1;
break;
case 2:
idx.first = x + 1;
idx.second = y;
break;
case 3:
idx.first = x + 1;
idx.second = y + 1;
break;
case 4:
idx.first = x;
idx.second = y + 1;
break;
case 5:
idx.first = x - 1;
idx.second = y;
break;
}
}
else {
switch (pos)
{
case 0:
idx.first = x - 1;
idx.second = y - 1;
break;
case 1:
idx.first = x;
idx.second = y - 1;
break;
case 2:
idx.first = x + 1;
idx.second = y;
break;
case 3:
idx.first = x;
idx.second = y + 1;
break;
case 4:
idx.first = x - 1;
idx.second = y + 1;
break;
case 5:
idx.first = x - 1;
idx.second = y;
break;
}
}
return idx;
}
bool canMove(int x, int y) {
if (x <= 0 || x > W)return false;
if (y <= 0 || y > H)return false;
return true;
}
//
int calLeng(int x, int y) {
int sum = 0;
int gox, goy;
for (int i = 0; i < 6; i++) {
P next = calAdjIdx(x, y, i);
gox = next.first;
goy = next.second;
if (canMove(gox, goy) == false) {
sum++;
}
else if (arr[goy][gox] == -1) {
sum++;
}
}
return sum;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> W >> H;
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
cin >> arr[i][j];
}
}
queue <P> q;
int visited[101][101] = { };
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
if (i == 1 || i == H || j == 1 || j == W) {
if (arr[i][j] == 1 || arr[i][j]==-1)continue;
q.push(P(j, i));
while (!q.empty()) {
P cur = q.front();
q.pop();
arr[cur.second][cur.first] = -1;
visited[cur.second][cur.first] = 1;
for (int i = 0; i < 6; i++) {
P next = calAdjIdx(cur.first, cur.second, i);
int gox, goy;
gox = next.first;
goy = next.second;
if (canMove(gox, goy)&&arr[goy][gox]==0){
if (visited[goy][gox] == 1)continue;
if (arr[goy][gox] == 0) {
q.push(P(gox, goy));
visited[goy][gox] = 1;
}
}
}
}
}
}
}
int totalLeng = 0;
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
if (arr[i][j] == 1) totalLeng += calLeng(j, i);
}
}
cout << totalLeng;
}'PS' 카테고리의 다른 글
| [백준] 2206번 벽 부수고 이동하기 (0) | 2022.03.29 |
|---|---|
| [백준] 1967번 트리의 지름 (0) | 2022.03.29 |
| [백준] 24500번 blobblush (0) | 2022.03.24 |
| LIS (0) | 2021.01.28 |
| 2차원 배열을 함수의 인자로 전달(포인터로) (0) | 2021.01.20 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 골목길C++
- 중위 표기식 후위 표기식으로 변환
- C++
- 1004
- 1738
- 벨만포드
- 7511
- 벨만-포드
- 어린왕자 C++
- 11657
- 후위 표기식
- tea time
- 스택
- 골목길
- 1918
- 백준
- 상범빌딩
- 타임머신
- 6539
- 소셜네트워킹어플리케이션
- 6018
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함
