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 |
| 31 |
Tags
- oracle
- 자바
- 이분탐색
- BOJ
- binary search
- 코테
- 러닝일지
- 코딩테스트
- 운동기록
- 백준
- priorityqueue
- SQL
- COS PRO
- math
- 시뮬레이션
- 알고리즘
- BFS
- 문제풀이
- 건강
- DP
- Java
- 프로그래머스
- 투포인터
- SWEA
- dfs
- 문자열
- MySQL
- db
- greedy
- 문제해결
Archives
- Today
- Total
슈콩
[BOJ] 백준 1799 비숍 본문


[문제]
https://www.acmicpc.net/problem/1799
[소스 코드]
import java.io.*;
import java.util.*;
public class Main {
static int n;
static boolean[] crossR,crossL;
static int blackMax,whiteMax;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
int[][] chess = new int[n][n];
StringTokenizer st;
for(int i=0;i<n;i++) {
st = new StringTokenizer(br.readLine());
for(int j=0;j<n;j++) {
chess[i][j] = Integer.parseInt(st.nextToken());
}
}
List<int[]> black = new ArrayList<>();
List<int[]> white = new ArrayList<>();
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
if(chess[i][j]==1) {
if((i+j)%2==0) black.add(new int[] {i,j});
else white.add(new int[] {i,j});
}
}
}
crossR = new boolean[2*n-1];
crossL = new boolean[2*n-1];
blackMax=0; whiteMax=0;
dfs(black,0,0,true);
dfs(white,0,0,false);
System.out.println(blackMax+ whiteMax);
}
private static void dfs(List<int[]> list,int idx,int cnt,boolean color) {
if(idx==list.size()) {
if(color) blackMax = Math.max(blackMax, cnt);
else whiteMax = Math.max(whiteMax, cnt);
return;
}
int[] curr = list.get(idx);
int r = curr[0];
int c = curr[1];
if(!crossR[r+c] && !crossL[r-c+n-1]) {
crossR[r+c] = true;
crossL[r-c+n-1] = true;
if(color) dfs(list,idx+1,cnt+1,color);
else dfs(list,idx+1,cnt+1,color);
crossR[r+c] = false;
crossL[r-c+n-1] = false;
}
dfs(list,idx+1,cnt,color);
}
}'Algorithms > Baekjoon' 카테고리의 다른 글
| [BOJ] 2178 미로탐색 (0) | 2025.11.27 |
|---|---|
| [BOJ] 백준 1260 DFS와 BFS (0) | 2025.11.27 |
| [BOJ] 백준 11600 구간 합 구하기 5 (0) | 2025.11.12 |
| [BOJ] 백준 11659 구간 합 구하기 4 (0) | 2025.11.12 |
| [BOJ] 백준 2003 수들의 합 2 (0) | 2025.11.11 |