일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- PS
- 6730
- 23971
- 알고리즘
- 25379
- 자바
- 20006
- 1342
- solved.ac
- pccp
- 2866
- sovled.ac
- programmers
- 9081
- 장고
- SWEA
- 파이썬
- 프로그래머스
- 라이브러리
- 24511
- PYTHON
- Django
- Algorithm
- 11688
- sloved.ac
- 17087
- 사용자정의필터
- java
- 백준
- 15965
Archives
- Today
- Total
코깽이의 코딩일기
SWEA 2117. [모의 SW 역량테스트] 홈 방범 서비스 본문
반응형
Link
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
내가 제출한 코드
import org.w3c.dom.ls.LSOutput;
import java.io.*;
import java.util.*;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringBuilder sb = new StringBuilder();
static StringTokenizer st;
static int answer, n, m;
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};
static int[][] map;
static boolean[][] v;
public static void main(String[] args) throws Exception {
int t = Integer.parseInt(br.readLine());
for (int i = 0; i < t; i++) {
init();
run();
sb.append("#").append(i + 1).append(" ").append(answer).append("\n");
}
System.out.println(sb);
}
private static void init() throws Exception {
// 정답 초기화
answer = Integer.MIN_VALUE;
st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
map = new int[n][n];
m = Integer.parseInt(st.nextToken());
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < n; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
}
private static void run() {
// 모든 좌표에서 확인
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
bfs(i, j);
}
}
}
private static void bfs(int x, int y) {
// Max K = n+1 ( 홀수, 짝수 경우를 고려 )
for (int i = 1; i <= n+1; i++) {
Deque<int[]> q = new ArrayDeque<>();
v = new boolean[n][n];
// 수입
int totalCost = 0;
// 집 수
int homeCnt = 0;
// x, y, len
q.add(new int[]{x, y, 0});
v[x][y] = true;
while (!q.isEmpty()) {
int[] cur = q.poll();
// 현재 K의 범위 = i
if (cur[2] == i) continue;
// 집이 있는 경우
if (map[cur[0]][cur[1]] == 1) {
homeCnt++;
totalCost += map[cur[0]][cur[1]] * m;
}
for (int j = 0; j < 4; j++) {
int nx = cur[0] + dx[j];
int ny = cur[1] + dy[j];
if (nx < 0 || ny < 0 || nx >= n || ny >= n || v[nx][ny]) continue;
v[nx][ny] = true;
q.add(new int[]{nx, ny, cur[2] + 1});
}
}
// 이득을 보면서 집 수가 더 많은 경우
if (answer < homeCnt && 0 <= totalCost - ((i * i) + (i - 1) * (i - 1))) {
answer = homeCnt;
}
}
}
}
로직
1. 모든 좌표에서 탐색을 진행
2. K의 범위를 N의 값보다 1 크게 세팅 ( 홀수, 짝수의 경우에 값이 달라짐 )
3. 손해를 안보면서 ( totalCost >= 0 ) 집이 제일 큰 경우를 찾기
반응형
'PS > SWEA' 카테고리의 다른 글
SWEA 5658. [모의 SW 역량테스트] 보물상자 비밀번호 (0) | 2024.05.02 |
---|---|
SWEA 2115. [모의 SW 역량테스트] 벌꿀채취 (0) | 2024.04.30 |
SWEA 11688. Calkin-Wilf tree 1 (0) | 2023.11.10 |
SWEA 6730. 장애물 경주 난이도 (0) | 2023.11.10 |
SWEA 2050. 알파벳을 숫자로 변환 (0) | 2023.11.02 |