티스토리 뷰
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
풀이
1) 인영이의 카드를(9장) 담는 배열을 만듬
2) ㄴ 순열로 모든 경우를 따짐
주의사항
1) 비기는 경우는 없다
package com.SWEA;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.Arrays;
import java.util.StringTokenizer;
/**
* @author yhs
* @date 2020. 12. 3
* @see
* @mem
* @time
* @caution
* [고려사항]
* 1) 순열
* 2) 규영이가 이기는 경우임
* [입력사항]
* [출력사항]
*/
public class SWEA_D3_6808_규영이와인영이의카드게임 {
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokens;
static int T, gy[], iy[], result[], cntWin;
static boolean isSelected[];
public static void main(String[] args) throws NumberFormatException, IOException {
input = new BufferedReader(new StringReader(src));
T = Integer.parseInt(input.readLine());
for(int t=1; t<=T; t++) {
tokens = new StringTokenizer(input.readLine());
gy = new int[9];
iy = new int[9];
result = new int[9];
isSelected = new boolean[19];
for(int i=0; i<9; i++) {
gy[i] = Integer.parseInt(tokens.nextToken());
}
int cnt = 0;
for(int i=1; i<=18; i++) {
boolean isUsed = false;
for(int j=0; j<9; j++) {
if(gy[j]==i) {
isUsed = true;
}
}
if(!isUsed) {
iy[cnt++] = i;
}
}
permutation(0,0);
System.out.println("#"+t+" "+ cntWin+ " "+ (362880-cntWin));
cntWin = 0;
}
}
private static void permutation(int start, int cnt) {
if(cnt == 9) {
boolean gyWin = compare(result);
if(gyWin) {
cntWin++;
}
return;
}
for(int i=0; i<9; i++) {
if(isSelected[i]) continue;
result[cnt] = iy[i];
isSelected[i] = true;
permutation(start+1, cnt+1);
isSelected[i] = false;
}
}
private static boolean compare(int[] result) {
int gyScore = 0;
int iyScore = 0;
for(int i=0; i<9; i++) {
if(gy[i] > result[i]) {
gyScore += gy[i]+result[i];
}else {
iyScore += gy[i]+result[i];
}
}
if(gyScore > iyScore) {
return true;
}else {
return false;
}
}
static String src =
"4\r\n" +
"1 3 5 7 9 11 13 15 17\r\n" +
"18 16 14 12 10 8 6 4 2\r\n" +
"13 17 9 5 18 7 11 1 15\r\n" +
"1 6 7 9 12 13 15 17 18";
}
'Algorithm' 카테고리의 다른 글
[SWEA] D5 1812 수정이의 타일 자르기 (java) (0) | 2020.12.16 |
---|---|
[SWEA] D4 8382 방향 전환 (java) (0) | 2020.12.16 |
[SWEA] D3 2814 최장 경로 (java) (0) | 2020.12.16 |
[SWEA] 1949 등산로 조성 (java) (0) | 2020.12.16 |
[SWEA] 5658 보물상자 비밀번호 (java) (0) | 2020.12.16 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- PriorityQueue
- BFS
- 코딩새내기
- laugh4mile
- S2
- react native
- java
- 구현
- SWEA
- 백준
- 리액트
- 문자열
- map
- S3
- G5
- react
- 다익스트라
- 우선순위큐
- Spring Boot
- 객체지향
- Spring
- 알고리즘
- 자바
- 그리디
- 리액트 네이티브
- DFS
- g4
- 백트래킹
- 시뮬레이션
- 현꾸라지
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함