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
- springboot
- DataSource
- ์๊ณ ๋ฆฌ์ฆ
- docker
- ConnectionPool
- ์๊ณ ๋ฆฌ์ฆ #๋ฐฑ์ค #1717
- GPG
- MariaDB
- error
- select
- DBCP
- oauth2
- ํ์
- greedy
- dml
- ๋ฐฑ์ค
- database
- Spring
- constraints
- OS
- Transaction
- interrupt
- Git
- Operating System
- hikaricp
- aws
- where
- JDBC
- MySQL
- DDL
Archives
- Today
- Total
did_story
[๋ฐฑ์ค / JAVA] 1080๋ฒ, ํ๋ ฌ (Greedy) ๋ณธ๋ฌธ
ํ์ด
1. 3 x 3 ์ด ์๋ ์์๋ ๋ค์ง์ ์ ์์ผ๋ฏ๋ก, ๊ฐ์์ง ์๋์ง๋ง ํ๋จํ๋ค๋ ๊ฒ์ ์๊ฐํ์๊ณ ,
2. 3 x 3 ์ด์์ด ๋ ์, 3x3 ํ๋ ฌ์ ์ข ์๋จ 1๊ฐ ๋ง์ ๋น๊ตํด์ ๋ค์ง์ด ์ค๋ค.
์? ๊ทธ๋ฌ๋?
- 3 x3 ํ๋ ฌ ๋ณํ์ด ๋ค๋ฅธ ๋ถ๋ถ๊ณผ ๊ฒน์น ๊ฐ๋ฅ์ฑ์ ์์ ๊ธฐ ์ํด์,
- ์ฐ์ฐ ๋ฒ์๋ฅผ ํ์ ํ์ฌ ํจ์จ์ ๋์ด๊ธฐ ์ํด์,
3. ์ฐ์ฐ์ด ๋๋๊ณ ์๋ก ๊ฐ์ง ์์ผ๋ฉด return -1 ๋!
์์ค ์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int n, m, answer;
static int[][] matA;
static int[][] matB;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
matA = new int[n][m];
matB = new int[n][m];
for (int i = 0; i < n; i++) {
String str = br.readLine();
for (int j = 0; j < str.length(); j++) {
matA[i][j] = str.charAt(j) - '0';
}
}
for (int i = 0; i < n; i++) {
String str = br.readLine();
for (int j = 0; j < str.length(); j++) {
matB[i][j] = str.charAt(j) - '0';
}
}
answer = solve();
System.out.println(answer);
}
static int solve() {
if (n < 3 || m < 3) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matA[i][j] != matB[i][j]) return -1;
}
}
return 0;
}
int rate = 0;
for (int i = 0; i <= n - 3; i++) {
for (int j = 0; j <= m - 3; j++) {
if (matA[i][j] != matB[i][j]) {
flip3x3(i, j);
rate++;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matA[i][j] != matB[i][j]) return -1;
}
}
return rate;
}
static void flip3x3(int x, int y) {
for (int i = x; i < x + 3; i++) {
for (int j = y; j < y + 3; j++) {
matA[i][j] = 1 - matA[i][j];
}
}
}
}
'Algorithm๐ > ๋ฐฑ์ค(BOJ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค / JAVA] 1049 ๊ธฐํ์ค (2) | 2025.01.02 |
---|---|
[๋ฐฑ์ค / JAVA] 11399๋ฒ, ATM (Greedy) (0) | 2024.12.24 |
[๋ฐฑ์ค / JAVA] 1717๋ฒ, ์งํฉ์ ํํ (Union-Find) (0) | 2024.12.23 |