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
|
#include <string>
#include <vector>
#include <queue>
#include <utility>
using namespace std;
#define pii pair<int,int >
#define piii pair<int, pii >
vector<vector<int>> rec;
int n;
int dy[4]={0,1,0,-1};
int dx[4]={1,0,-1,0};
int board[200][200];
int fx,fy;
void BFS(int x,int y){
queue<pii > q;
q.push({x,y});
while(!q.empty()){
int x=q.front().first;
int y=q.front().second;
q.pop();
if(x==fx && y==fy)
return ;
for(int i=0;i<4;i++){
int nx=x+dx[i];
int ny=y+dy[i];
if(board[nx][ny]==1 ){
board[nx][ny]=board[x][y]+1;
q.push({nx,ny});
}
}
}
}
int solution(vector<vector<int>> rectangle, int characterX, int characterY, int itemX, int itemY) {
int answer = 0;
n=rectangle.size();
fx=itemX*2;
fy=itemY*2;
characterX *= 2, characterY *= 2;
rec.resize(n,vector<int>(4));
int x1, y1, x2, y2;
for(int i=0;i<n;i++){
for(int j=0;j<4;j++){
rec[i][j]=rectangle[i][j]*2;
}
x1 = rec[i][0], y1 = rec[i][1];
x2 = rec[i][2], y2 = rec[i][3];
for(int i=x1; i<=x2;i++)
for(int j=y1; j<=y2; j++)
board[i][j]=1;
}
for (int i = 0; i < n; i++) {
x1 = rec[i][0], y1 = rec[i][1];
x2 = rec[i][2], y2 = rec[i][3];
for (int i = x1 + 1; i < x2; i++)
for (int j = y1 + 1; j < y2; j++)
board[i][j] = 0;
}
BFS(characterX,characterY);
return board[fx][fy]/2 ;
}
|
cs |
출처:https://school.programmers.co.kr/learn/courses/30/lessons/87694?language=cpp
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[c++] 무지의 먹방 라이브 (0) | 2022.10.08 |
---|---|
[c++] N으로 표현 (1) | 2022.10.04 |
[c++] 1차 비밀지도 (구현) (0) | 2022.09.28 |
[c++] 베스트앨범 (해쉬) (0) | 2022.09.23 |
[c++] 단속카메라 (그리디, 탐욕법) (0) | 2022.09.23 |