arr[n][n]
의 배열을 만든다. int[][] board = new int[n][n];
n*n
배열의 index를 입력 받는다.for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
board[i][j] = kb.nextInt();
}
}
M
을 입력받는다.int m = kb.nextInt();
int[] moves = new int[m];
for(int i=0;i<m;i++)
moves[i] = kb.nextInt();
board[][] , moves[i]
를 solution 메스드에 넘기자
import java.util.Scanner;
public class Main38 {
public int solution(int[][] board, int[] moves){
return answer;
}
public static void main(String[] args) {
Main38 T = new Main38();
Scanner kb = new Scanner(System.in) ;
int n = kb.nextInt();
int[][] board = new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
board[i][j] = kb.nextInt();
}
}
int m = kb.nextInt();
int[] moves= new int[m];
for(int i=0;i<m;i++)
moves[i] = kb.nextInt();
System.out.println(T.solution(board,moves));
}
}
public int solution(int[][] board, int[] moves) {
int answer = 0;
Stack<Integer> stack = new Stack<>();
for (int pos : moves) {
// board의 행 크기
for (int i = 0; i < board.length; i++) {
if (board[i][pos - 1] != 0) {
int tmp = board[i][pos - 1];
board[i][pos - 1] = 0;
if (!stack.isEmpty() && tmp == stack.peek()) {
answer += 2;
stack.pop();
} else stack.push(tmp);
break;
}
}
}
return answer;
}
package algolecture;
import java.util.Scanner;
import java.util.Stack;
public class Main38 {
public int solution(int[][] board, int[] moves) {
int answer = 0;
Stack<Integer> stack = new Stack<>();
for (int pos : moves) {
// board의 행 크기
for (int i = 0; i < board.length; i++) {
if (board[i][pos - 1] != 0) {
int tmp = board[i][pos - 1];
board[i][pos - 1] = 0;
if (!stack.isEmpty() && tmp == stack.peek()) {
answer += 2;
stack.pop();
} else stack.push(tmp);
break;
}
}
}
return answer;
}
public static void main(String[] args) {
Main38 T = new Main38();
Scanner kb = new Scanner(System.in) ;
int n = kb.nextInt();
int[][] board = new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
board[i][j] = kb.nextInt();
}
}
int m = kb.nextInt();
int[] moves= new int[m];
for(int i=0;i<m;i++)
moves[i] = kb.nextInt();
System.out.println(T.solution(board,moves));
}
}