Skip to content

Commit 0482e04

Browse files
committed
[Gold I] Title: 전구를 켜라, Time: 44 ms, Memory: 8392 KB -BaekjoonHub
1 parent b9dc0ab commit 0482e04

2 files changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# [Gold I] 전구를 켜라 - 2423
2+
3+
[문제 링크](https://www.acmicpc.net/problem/2423)
4+
5+
### 성능 요약
6+
7+
메모리: 8392 KB, 시간: 44 ms
8+
9+
### 분류
10+
11+
그래프 이론, 그래프 탐색, 최단 경로, 데이크스트라, 격자 그래프, 0-1 너비 우선 탐색
12+
13+
### 제출 일자
14+
15+
2026년 2월 14일 15:33:55
16+
17+
### 문제 설명
18+
19+
<p>선영이는 N × M 직사각형 크기의 전자 회로를 디자인 하고 있다. 회로에는 N × M개의 정사각형 타일이 있고, 모두 직사각형의 변과 평행하다. 모든 타일은 두 개의 마주보는 꼭짓점이 전선으로 연결되어 있다. (그림 참조)</p>
20+
21+
<p>전원은 왼쪽 위 모서리에 연결되어 있고, 전구는 오른쪽 아래 모서리에 연결되어 있다. 전구는 전원에서 전구로 가는 경로가 있을 때만 불이 켜진다. 전구에 불을 켜기 위해서, 선영이는 몇개의 타일을 90도 회전 시킬 수 있다.</p>
22+
23+
<p style="text-align: center;"><img alt="" src="https://upload.acmicpc.net/46c9ed9e-27e3-4e32-a144-0962b813347e/-/preview/" style="width: 289px; height: 195px;"></p>
24+
25+
<p>위의 그림에서 전구는 꺼져있다. 만약 오른쪽에서 2번째 열 중 아무 칸이나 90도 회전시킨다면, 전원과 전구는 연결되어 전구가 켜지게 된다. 전구에 불을 켜기 위해 돌려야 하는 칸의 개수의 최솟값을 구하는 프로그램을 작성하시오.</p>
26+
27+
### 입력
28+
29+
<p>첫째 줄에 N과 M이 주어진다. 둘째 줄부터 N개의 줄에는 전자 회로의 상태가 주어진다. 상태는 <code>/</code> 또는 <code>\</code>이다. (1 ≤ N, M ≤ 500)</p>
30+
31+
### 출력
32+
33+
<p>첫째 줄에 문제의 정답을 출력한다. 전구에 불을 켜는 것이 가능하면, 몇 개의 칸을 돌려야 하는지를 출력하고, 불가능할때는 "NO SOLUTION"을 따옴표 없이 출력한다.</p>
34+
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include<iostream>
2+
#include<queue>
3+
#include<vector>
4+
using namespace std;
5+
6+
const int N = 500;
7+
int n, m;
8+
bool arr[N][N];
9+
int dxf[] = { -1, 1, -1, 0, 0, 1 };
10+
int dyf[] = { -1, 1, 0, -1, 1, 0 };
11+
int dxt[] = { -1, 1, -1, 0, 0, 1 };
12+
int dyt[] = { 1, -1, 0, -1, 1, 0 };
13+
struct Pos {
14+
int cx, cy, ct;
15+
bool cf;
16+
bool operator<(const Pos& other) const {
17+
return ct > other.ct;
18+
}
19+
};
20+
21+
int dijkstra() {
22+
priority_queue<Pos> pq;
23+
pq.push({ 0, 0, arr[0][0], false});
24+
vector<vector<vector<int>>> dist(2, vector<vector<int>>(n, vector<int>(m, 2e9)));
25+
dist[0][0][0] = arr[0][0];
26+
27+
while (!pq.empty()) {
28+
auto [cx, cy, ct, cf] = pq.top(); pq.pop();
29+
30+
if (dist[cf][cx][cy] < ct) continue;
31+
//cout << cx << " " << cy << " " << ct << " " << cf << "\n";
32+
if (cx == n - 1 && cy == m - 1 && !cf) return ct;
33+
34+
if (!cf) {
35+
for (int i = 0; i < 6; ++i) {
36+
int nx = cx + dxf[i], ny = cy + dyf[i];
37+
38+
if (0 <= nx && nx < n && 0 <= ny && ny < m) {
39+
int nt = ct;
40+
if (i < 2 && arr[nx][ny] != cf) ++nt;
41+
if (i >= 2 && arr[nx][ny] == cf) ++nt;
42+
bool nf = (ct == nt ? arr[nx][ny] : !arr[nx][ny]);
43+
44+
if (dist[nf][nx][ny] > nt) {
45+
dist[nf][nx][ny] = nt;
46+
pq.push({ nx, ny, nt, nf });
47+
}
48+
}
49+
}
50+
}
51+
else {
52+
for (int i = 0; i < 6; ++i) {
53+
int nx = cx + dxt[i], ny = cy + dyt[i];
54+
55+
if (0 <= nx && nx < n && 0 <= ny && ny < m) {
56+
int nt = ct;
57+
if (i < 2 && arr[nx][ny] != cf) ++nt;
58+
if (i >= 2 && arr[nx][ny] == cf) ++nt;
59+
bool nd = (ct == nt ? arr[nx][ny] : !arr[nx][ny]);
60+
61+
if (dist[nd][nx][ny] > nt) {
62+
dist[nd][nx][ny] = nt;
63+
pq.push({ nx, ny, nt, nd });
64+
}
65+
}
66+
}
67+
}
68+
}
69+
return -1;
70+
}
71+
72+
int main() {
73+
ios::sync_with_stdio(0);
74+
cin.tie(0);
75+
76+
cin >> n >> m;
77+
for (int i = 0; i < n; ++i) {
78+
for (int j = 0; j < m; ++j) {
79+
char c; cin >> c;
80+
arr[i][j] = (c == '/' ? true : false);
81+
}
82+
}
83+
84+
int res = dijkstra();
85+
if (res == -1) cout << "NO SOLUTION";
86+
else cout << res;
87+
}

0 commit comments

Comments
 (0)