[BOJ] 2931 가스관

Time Lapse :45min 23sec

2931.cpp

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include<stdio.h>
#include <queue>
using namespace std;
struct gas {
int x;
int y;
};
int dx[4] = { 0,1,0,-1 };
int dy[4] = { -1,0,1,0 };
int main(int argc, char** argv){
int N, M, nx, ny, ansx, ansy;
char buf[25][26], ansc;
int v[25][25] = { 0, };
queue<gas> q;
pair<int, int> mos;
scanf("%d %d" ,&N, &M);
for (int i = 0; i < N; ++i) {
scanf("%s", buf[i]);
for (int j = 0; j < M; ++j)
if (buf[i][j] == 'M') mos.first = i, mos.second = j;
}
for (int i = 0; i < 4; ++i) {
nx = mos.second + dx[i];
ny = mos.first + dy[i];
if (nx < 0 || nx >= M || ny < 0 || ny >= N) continue;
if (buf[ny][nx] == '.') continue;
switch (i){
case 0:
if (buf[ny][nx] == '+' || buf[ny][nx] == '|' || buf[ny][nx] == '1' || buf[ny][nx] == '4') {
gas g;
g.x = nx;
g.y = ny;
v[ny][nx] = 1;
q.push(g);
}
break;
case 1:
if (buf[ny][nx] == '+' || buf[ny][nx] == '-' || buf[ny][nx] == '3' || buf[ny][nx] == '4') {
gas g;
g.x = nx;
g.y = ny;
v[ny][nx] = 1;
q.push(g);
}
break;
case 2:
if (buf[ny][nx] == '+' || buf[ny][nx] == '|' || buf[ny][nx] == '2' || buf[ny][nx] == '3') {
gas g;
g.x = nx;
g.y = ny;
v[ny][nx] = 1;
q.push(g);
}
break;
case 3:
if (buf[ny][nx] == '+' || buf[ny][nx] == '-' || buf[ny][nx] == '2' || buf[ny][nx] == '1') {
gas g;
g.x = nx;
g.y = ny;
v[ny][nx] = 1;
q.push(g);
}
break;
}
}
while (!q.empty()) {
int x = q.front().x;
int y = q.front().y;
q.pop();
for (int i = 0; i < 4; ++i) {
if (buf[y][x] == '|' && (i == 1 || i == 3)) continue;
if (buf[y][x] == '|' && (i == 1 || i == 3)) continue;
if (buf[y][x] == '-' && (i == 0 || i == 2)) continue;
if (buf[y][x] == '1' && (i == 0 || i == 3)) continue;
if (buf[y][x] == '2' && (i == 2 || i == 3)) continue;
if (buf[y][x] == '3' && (i == 1 || i == 2)) continue;
if (buf[y][x] == '4' && (i == 0 || i == 1)) continue;
nx = x + dx[i];
ny = y + dy[i];
if (0 <= nx && nx < M && 0 <= ny && ny < N && !v[ny][nx]) {
v[ny][nx] = 1;
if (buf[ny][nx] != '.' && buf[ny][nx] != 'M' && buf[ny][nx] !='Z') {
gas g;
g.x = nx;
g.y = ny;
q.push(g);
}
else if(buf[ny][nx] == '.'){
ansx = nx; ansy = ny;
printf("%d %d ", ansy + 1, ansx + 1);
int conn[4] = { 0, };
for (int j = 0; j < 4; ++j) {
nx = ansx + dx[j];
ny = ansy + dy[j];
if (0 <= nx && nx < M && 0 <= ny && ny < N) {
if (buf[ny][nx] == '.') continue;
if (j == 0 && (buf[ny][nx] == '+' || buf[ny][nx] == '|' || buf[ny][nx] == '1' || buf[ny][nx] == '4')) conn[j] = 1;
if (j == 1 && (buf[ny][nx] == '+' || buf[ny][nx] == '-' || buf[ny][nx] == '3' || buf[ny][nx] == '4')) conn[j] = 1;
if (j == 2 && (buf[ny][nx] == '+' || buf[ny][nx] == '|' || buf[ny][nx] == '2' || buf[ny][nx] == '3')) conn[j] = 1;
if (j == 3 && (buf[ny][nx] == '+' || buf[ny][nx] == '-' || buf[ny][nx] == '1' || buf[ny][nx] == '2')) conn[j] = 1;
}
}
if (conn[0] && conn[1] && conn[2] && conn[3]) {
printf("+");
return 0;
}
if (conn[0]) {
if (conn[1]) printf("2");
if (conn[2]) printf("|");
if (conn[3]) printf("3");
return 0;
}
if (conn[1]) {
if (conn[2]) printf("1");
if (conn[3]) printf("-");
return 0;
}
if (conn[2]) printf("4");
return 0;
}
}
}
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/2931/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.