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 |
Tags
- 고모네 콩탕
- mvc
- 포두부 보쌈
- 양꼬치
- Servlet
- 2020 KAKAO BLIND
- 설탕 배달
- 투어
- 1로 만들기
- HTTP API
- Spring
- dp
- 알고리즘
- 백준
- 서블릿
- 문자열 압축
- 맛집
- BFS
- 2589
- 2638
- 동적 프로그래밍
- 맛집 투어
- 호유동
- 스프링
- 완도산회
- 2839
- 다이나믹 프로그래밍
- 프로그래머스
- 스프링 MVC
- 쓰레드 풀
Archives
- Today
- Total
프로그래밍 공방
[프로그래머스] 기둥과 보 설치 본문
문제
programmers.co.kr/learn/courses/30/lessons/60061
코딩테스트 연습 - 기둥과 보 설치
5 [[1,0,0,1],[1,1,1,1],[2,1,0,1],[2,2,1,1],[5,0,0,1],[5,1,0,1],[4,2,1,1],[3,2,1,1]] [[1,0,0],[1,1,1],[2,1,0],[2,2,1],[3,2,1],[4,2,1],[5,0,0],[5,1,0]] 5 [[0,0,0,1],[2,0,0,1],[4,0,0,1],[0,1,1,1],[1,1,1,1],[2,1,1,1],[3,1,1,1],[2,0,0,0],[1,1,1,0],[2,2,0,1]] [[
programmers.co.kr
문제해결방법
1. 기둥과 보를 설치할 때, 설치할 수 있는 조건을 만족하는지 확인하고 설치한다.
2. 구조물을 제거할 때는 영향을 받는 구조물들이 유지될 수 있는지 확인 후 제거한다.
코드
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 | package Programmers; import java.util.ArrayList; import java.util.List; public class Solution_기둥과보설치 { public static boolean checkPillar(int x, int y, int[][] pillar, int[][] bo) { boolean check = false; if(y==0) check = true; if(y!=0 && pillar[y-1][x] == 1) check = true; if(bo[y][x]==1 || (x!=0 && bo[y][x-1]==1)) check = true; return check; } public static boolean checkBo(int x, int y, int[][] pillar, int[][] bo) { boolean check = false; if((y!=0 && (pillar[y-1][x]==1) || pillar[y-1][x+1]==1)) check = true; if(bo[y][x+1]==1 && (x!=0 && bo[y][x-1]==1)) check = true; return check; } public static boolean deletePillar(int x, int y, int[][] pillar, int[][] bo) { boolean check = true; pillar[y][x] = 0; if(pillar[y+1][x]==1) check &= checkPillar(x, y+1, pillar, bo); if(bo[y+1][x]==1) check &= checkBo(x, y+1, pillar, bo); if(x!=0 && bo[y+1][x-1]==1) check &= checkBo(x-1, y+1, pillar, bo); pillar[y][x] = 1; return check; } public static boolean deleteBo(int x, int y, int[][] pillar, int[][] bo) { boolean check = true; bo[y][x] = 0; if(bo[y][x+1]==1) check &= checkBo(x+1, y, pillar, bo); if(x!=0 && bo[y][x-1]==1) check &= checkBo(x-1, y, pillar, bo); if(pillar[y][x]==1) check &= checkPillar(x, y, pillar, bo); if(pillar[y][x+1]==1) check &= checkPillar(x+1, y, pillar, bo); bo[y][x] = 1; return check; } public static int[][] solution(int n, int[][] build_frame) { int[][] pillar = new int[n+2][n+2]; int[][] bo = new int[n+2][n+2]; for(int i=0; i<build_frame.length; i++) { int x = build_frame[i][0]; int y = build_frame[i][1]; int a = build_frame[i][2]; int b = build_frame[i][3]; if(b==1) { if(a==0 && checkPillar(x, y, pillar, bo)) pillar[y][x] = 1; else if(a==1 && checkBo(x, y, pillar, bo)) bo[y][x] = 1; } else { if(a==0 && deletePillar(x, y, pillar, bo)) pillar[y][x] = 0; else if(a==1 && deleteBo(x, y, pillar, bo)) bo[y][x] = 0; } } List<int[]> list = new ArrayList<>(); for(int i=0; i<n+2; i++) { for(int j=0; j<n+2; j++) { if(pillar[j][i]==1) list.add(new int[] {i, j, 0}); if(bo[j][i]==1) list.add(new int[] {i, j, 1}); } } int[][] answer = new int[list.size()][3]; for(int i=0; i<list.size(); i++) { for(int j=0; j<list.get(i).length; j++) { int[] temp = list.get(i); answer[i][j] = temp[j]; } } return answer; } public static void main(String[] args) { int n = 5; int[][] build_frame = {{0,0,0,1},{2,0,0,1},{4,0,0,1},{0,1,1,1},{1,1,1,1},{2,1,1,1},{3,1,1,1},{2,0,0,0}, {1,1,1,0}, {2,2,0,1}}; int[][] answer = solution(n, build_frame); for(int i=0; i<answer.length; i++) { for(int j=0; j<answer[i].length; j++) { System.out.print(answer[i][j]+" "); } System.out.println(); } } } | cs |
코드에 대한 피드백이나 더 좋은 아이디어는 언제나 환영입니다.
'개발 > 문제해결' 카테고리의 다른 글
[백준] 1194번 : 달이 차오른다, 가자. (0) | 2021.01.20 |
---|---|
[프로그래머스] 블록 이동하기 (0) | 2021.01.14 |
[백준] 1654번 : 랜선 자르기 (0) | 2021.01.12 |
[백준] 2252번 : 줄 세우기 (0) | 2021.01.12 |
[프로그래머스] 도둑질 (0) | 2021.01.09 |