

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
|
using System;
using System.Collections.Generic;
public class Solution
{
//좌석의 최대 범위
private const int placesLength = 5;
public int[] solution(string[,] places)
{
List<int> answer = new List<int>(places.Length);
for(int i = 0; i < places.GetLength(0); i++)
{
string[] room = new string[places.GetLength(1)];
for (int j = 0; j < places.GetLength(1); j++)
{
room[j] = places[i, j];
}
answer.Add(CheckRoom(room));
}
return answer.ToArray();
}
private int CheckRoom(string[] room)
{
for(int i = 0; i < room.Length; i++)
{
for(int j = 0; j < room.Length; j++)
{
if (room[i][j] != 'P')
continue;
//현재 좌석이 'P'인 경우, X를 벽, O를 경로, P를 목적지라고 생각하고 BFS를한다.
Queue<(int, int)> queue = new Queue<(int, int)>();
HashSet<(int, int)> checkAlready = new HashSet<(int, int)>();
queue.Enqueue((i, j));
checkAlready.Add((i, j));
while(queue.Count > 0)
{
(int y, int x) cur = queue.Dequeue();
var checkSeats = new List<(int, int)>();
//좌석을 검사하기 전에, 범위 안에 있는지부터 검사한다.
//상
if (cur.y > 0)
checkSeats.Add((cur.y - 1, cur.x));
//하
if (cur.y < placesLength - 1)
checkSeats.Add((cur.y + 1, cur.x));
//좌
if (cur.x > 0)
checkSeats.Add((cur.y, cur.x - 1));
//우
if (cur.x < placesLength - 1)
checkSeats.Add((cur.y, cur.x + 1));
foreach((int y, int x) seat in checkSeats)
{
//방문했으면 멈춤
if (checkAlready.Contains(seat))
continue;
char currentSeat = room[seat.y][seat.x];
//벽이 있으면 멈춤
if (currentSeat == 'X')
continue;
//P를 찾은 경우 = 거리두기를 안 지킨 것.
if (currentSeat == 'P')
return 0;
//맨헤튼거리가 2가 넘었다면 멈춤
if (CheckManhattan((seat.y, seat.x), (i, j)))
continue;
//공기가 통하는 길이 있으면 다시 체크한다.
if (currentSeat == 'O')
{
queue.Enqueue(seat);
checkAlready.Add(seat);
}
}
}
}
}
return 1;
}
private bool CheckManhattan((int, int) a, (int, int) b)
{
return (Math.Abs(a.Item1 - b.Item1) + Math.Abs(a.Item2 - b.Item2)) >= 2;
}
}
|
cs |
'프로그래머스 - 내 풀이 > 프로그래머스 Lv2' 카테고리의 다른 글
[C#]프로그래머스/큰 수 만들기/그리디 (0) | 2023.04.26 |
---|---|
[C#]프로그래머스/타겟 넘버/DFS (0) | 2023.04.25 |
[C#]프로그래머스/호텔 대실 (0) | 2023.04.20 |
프로그래머스/연속된 부분 수열의 합 (0) | 2023.04.15 |
프로그래머스/괄호 회전하기 (0) | 2023.04.14 |