https://www.acmicpc.net/problem/1018
next()를 여러 번 반복해서 한 줄씩 받아주면 됨!
cf.
Scanner의 next() 메소드는 스페이스 전까지 입력받은 문자열을 리턴
nextLine()메소드는 Enter를 치기 전까지 쓴 문자열을 모두 리턴
-> 공백이 있는 문자열 한 줄을 리턴받고 싶으면 nextLine() 메소드를 사용
그냥 한 단어만 쓰고 싶다면 next()
public class ScannerExample2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("문자열입력: "); // 안녕하세요 밥입니다.
String str1 = scanner.nextLine();
System.out.println(str1); // 안녕하세요 밥입니다.
System.out.println("문자열입력: "); // 안녕하세요 밥입니다.
String str2 = scanner.next();
System.out.println(str2); // 안녕하세요
}
}
출처: https://deftkang.tistory.com/55 [deftkang의 IT 블로그]
import java.util.Scanner;
public class baek_1018 {
public static boolean[][] rec;
public static int min = 64;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 세로 길이 입력
int num1 = in.nextInt();
// 가로 길이 입력
int num2 = in.nextInt();
// 패턴 입력할 2차원 배열 선언
rec = new boolean[num1][num2];
// 2차원 배열에 패턴 입력
for(int i = 0; i < num1; i++){
String str = in.next();
// W는 1, B는 2로 저장
for (int j = 0; j < num2; j++){
if(str.charAt(j) == 'W'){
rec[i][j] = true;
} else {
rec[i][j] = false;
}
}
}
int N_row = num1 - 7;
int M_col = num2 - 7;
for (int i = 0; i < N_row; i++) {
for (int j = 0; j < M_col; j++) {
find(i, j);
}
}
System.out.println(min);
}
public static void find(int x, int y) {
int end_x = x + 8;
int end_y = y + 8;
int count = 0;
boolean TF = rec[x][y]; // 첫 번째 칸의 색
for (int i = x; i < end_x; i++) {
for (int j = y; j < end_y; j++) {
// 올바른 색이 아닐경우 count 1 증가
if (rec[i][j] != TF) {
count++;
}
TF = (!TF);
}
TF = !TF;
}
count = Math.min(count, 64 - count);
min = Math.min(min, count);
}
}