2차원 배열 문제
import java.util.Scanner;
public class Swea_어디에단어가들어갈수있을까3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
int[] ansArr = new int[t];
for(int testCase = 0; testCase < t; testCase++) {
int n = sc.nextInt();
int k = sc.nextInt();
int[][] arr = new int[n][n];
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
arr[i][j] = sc.nextInt();
}
}
int check = 0;
int ans = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(arr[i][j] == 1)
check++;
else if(arr[i][j] == 0 && check > 0) {
if(check == k)
ans++;
check = 0;
}
}
if(check == k) {
ans++;
}
check = 0;
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(arr[j][i] == 1)
check++;
else if(arr[j][i] == 0 && check > 0) {
if(check == k)
ans++;
check = 0;
}
}
if(check == k) {
ans++;
}
check = 0;
}
ansArr[testCase] = ans;
}
for(int i = 1; i <= t; i++)
System.out.println("#" + i + " " + ansArr[i-1]);
}
}
import java.util.Arrays;
import java.util.Scanner;
public class Swea_어디에단어가들어갈수있을까2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int test_case = 1; test_case <= T; test_case++) {
int N = sc.nextInt(); // 단어퍼즐의 N*N 행렬
int K = sc.nextInt(); // k길이의 단어를 1에다가 넣자
int[][] arrpuzzle = new int[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
arrpuzzle[i][j] = sc.nextInt();
}
}
// 1이 K개인곳을 찾자.
// 행탐색
int result = 0;
int ncount = 0;
for (int i = 0; i < N; i++) {
ncount = 0;
for (int j = 0; j < N; j++) {
if (arrpuzzle[i][j] == 1) {
ncount++;
} else { // 퍼즐이 0일때
if (ncount == K)
result++;
ncount = 0;
}
}
if(ncount == K) {
result ++;
}
}
// 열탐색
int count = 0;
for (int j = 0; j < N; j++) {
count = 0;
for (int i = 0; i < N; i++) {
if (arrpuzzle[i][j] == 1) {
count++;
} else { // 퍼즐이 0일때
if (count == K)
result++;
count = 0;
}
}
if(count == K) {
result ++;
}
}
System.out.println("#" + test_case + " " +result);
}
}
}
포인트
- 행탐색과 열탐색을 해주고 한번 더 동일한 카운트 값을 가지는지 각각 확인해서 정답에 가까워져야한다. (정답에서 빠져나갈 수 있는 애들을 걸러내준다)
- 풀 때는 어려웠는데, 4방탐색문제 풀고 이거 보니까.. 오히려 쉬워.. 오히려 조아!
- 오늘도 제이크 땡삼.
'Algorithm' 카테고리의 다른 글
[알고리즘]SWEA_11315. 오목 판정 (0) | 2023.08.13 |
---|---|
[알고리즘]SWEA_1954.달팽이숫자 (0) | 2023.08.12 |
[알고리즘]SWEA_2001.파리퇴치 (0) | 2023.08.09 |
[알고리즘] SWEA_1208.Flatten (2) | 2023.08.08 |
[알고리즘]기본 알고리즘 (0) | 2023.08.05 |