Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- unity
- --watch
- server
- Unity IAP
- screencapture
- react
- Google Refund
- Camera Zoom
- spread 연산자
- Spring Boot
- MySQL
- springboot
- rpg server
- Camera Movement
- Google Developer API
- java
- Git
- OverTheWire
- linux
- express
- docker
- mongoDB
- css framework
- Packet Network
- draganddrop
- critical rendering path
- nodejs
- Unity Editor
- SDK upgrade
- Digital Ocean
Archives
- Today
- Total
우당탕탕 개발일지
[프로그래머스] Level 2. 거리두기 확인하기 본문
https://school.programmers.co.kr/learn/courses/30/lessons/81302
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
해결 방법
- 테이블에서 P가 나오면, 검사범위를 설정한다.
- 그 범위안에서 P가 나왔을때 만약 맨해튼거리가 1이면 false를 바로 리턴한다.
- 아니라면, 두개의 P자리 (x,y) 와 (i,j) 사이에 X가 한개라도 없으면 바로 false를 리턴한다.
#include <string>
#include <vector>
#include <cmath>
#include <iostream>
using namespace std;
bool checkPlace(vector<string> tables, int i, int j){
//검사해야하는 인덱스들
vector<vector<int>> checks = {
{i-2,j},
{i-1,j-1},{i-1,j},{i-1,j+1},
{i,j-2},{i,j-1},{i,j+1},{i,j+2},
{i+1,j-1},{i+1,j},{i+1,j+1},
{i+2,j}
};
for(int idx =0; idx<checks.size(); idx++){
int x = checks[idx][0]; int y = checks[idx][1];
if(x < 0 || x >= tables.size() || y <0 || y >=tables[x].size()) continue;
if(tables[x][y] != 'P') continue;
if(abs(x-i) + abs(y-j) <=1) return false;
for(int row = min(x,i) ; row <= max(x,i) ; row ++){
for(int col = min(y,j); col <=max(y,j); col ++){
if(row == x && col ==y) continue;
if(row == i && col == j) continue;
if(tables[row][col] != 'X') return false;
}
}
}
return true;
}
bool isSafeRoom(vector<string> tables){
for(int i=0; i<tables.size(); i++){
for(int j=0; j<tables[i].size(); j++){
if(tables[i][j]=='P'){
bool safe= checkPlace(tables,i,j);
if(!safe) return false;
}
}
}
return true;
}
vector<int> solution(vector<vector<string>> places) {
vector<int> answer;
for(int i=0; i<places.size(); i++){
bool safe = isSafeRoom(places[i]);
answer.push_back(safe?1:0);
}
return answer;
}
'Algorithm(c++) > Level 2' 카테고리의 다른 글
[프로그래머스] Level 2 . 이모티콘 할인행사 (0) | 2025.03.18 |
---|---|
[프로그래머스] Level 2. 후보키 (1) | 2025.03.15 |
[프로그래머스] Level 2. 2개 이하로 다른 비트 (0) | 2025.02.26 |
[프로그래머스] Level 2. 2 x n 타일링 (0) | 2025.02.25 |
[프로그래머스] Level 2. 땅따먹기 (0) | 2025.02.24 |