일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- HUFS 모각코 캠프
- 백준
- 백준 17451번
- 명품자바
- react
- ubuntu
- SQL
- javascript
- 모각코
- 그래프
- 백준 2512번
- 다이나믹 프로그래밍
- 깃헙
- 백준 1331번
- SWEA 15612번
- AWS
- 백준 3085번
- Python
- 머신러닝과 딥러닝
- 백준 16918번
- 백준 1253번
- MySQL
- 자바
- java_programming
- 그리디
- 다이나믹프로그래밍
- 백준 18310번
- 알고리즘
- 백준 15787번
- 백준 1987
Archives
- Today
- Total
차곡차곡
[SWEA/Python, Java] SW Expert Academy 2805번 - 농작물 수확하기 본문
SW Expert Academy #2805 농작물 수확하기
Python ver.
T = int(input())
for t in range(1, T+1):
n = int(input())
grid = [list(map(int, input())) for _ in range(n)]
profit = 0
for i in range(n):
if i <= n // 2:
start = n // 2 - i
end = 2 * i + 1
for j in range(end):
profit += grid[i][start+j]
else:
start = i - n // 2
end = n - (2 * i - n) - 1
for j in range(end):
profit += grid[i][start+j]
print(f'#{t} {profit}')
Java ver.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int t = 1; t < T+1; t++) {
// 입력 받기
int n = Integer.parseInt(br.readLine());
int[][] grid = new int[n][n];
for (int i = 0; i < grid.length; i++) {
String line = br.readLine();
for (int j = 0; j < grid.length; j++) {
grid[i][j] = line.charAt(j) - '0';
}
}
// 계산
int start, end;
int profit = 0;
for (int i = 0; i < n; i++) {
if (i <= n/2) {
start = n / 2 - i;
end = 2 * i + 1;
for (int j = 0; j < end; j++) {
profit += grid[i][start+j];
}
} else {
start = i - n / 2;
end = n - (2 * i - n) - 1;
for (int j = 0; j < end; j++) {
profit += grid[i][start+j];
}
}
}
System.out.println("#" + t + " " + profit);
}
}
}
++ 개선
- 중심 좌표 구하기
- (n / 2, n / 2)
- 중심 좌표로 맨해튼 거리가 n / 2 이하인 영역을 탐색 누적 ( |x2 - x1| + |y2 - y1| <= n / 2 )
* 맨해튼 거리 : |x2 - x1| + |y2 - y1| <= n / 2
참고
'CS > Algorithm' 카테고리의 다른 글
[BOJ/Java] 백준 1244번 - 스위치 켜고 끄기 (0) | 2023.08.01 |
---|---|
[BOJ/Python, Java] 백준 11729번 - 하노이 탑 이동 순서 (0) | 2023.08.01 |
[BOJ/Python, Java] 백준 1992번 - 쿼드트리 (0) | 2023.08.01 |
[SWEA/Python, Java] SW Expert Academy 1208번 - Flatten (0) | 2023.08.01 |
[BOJ/Python, Java] 백준 4779번 - 칸토어 집합 (0) | 2023.07.31 |
Comments