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 |
Tags
- 별 찍기 10
- heap
- 버킷 정렬
- 풀이
- 백준 17822
- 1764
- AVL 시간 복잡도
- dfs
- qorwns
- 백준
- 백준 1406
- 스택의 특징
- 조세퍼스 순열
- 해시구현
- Stack 이란
- 백준 5397
- 백준 17471
- 시간 복잡도
- 원판 돌리기
- 해시 구현
- 백준 1158
- 게리멘더링2
- 백준 2447
- ㅣ풀이
- 자료구조
- 구현
- c#
- 5397
- C/C++ 구현
- 백준 17779
Archives
- Today
- Total
홍시홍의 프로그래밍
[백준 17281] 야구 본문
분류
dfs, 구현
요구사항
각 라운드별 선수의 타율이 주어졌을 때, 최고 점수를 얻을 수 있도록 타순 정하기
풀이
1. 타순 정하기
- 타순은 dfs로 구현할 수 있도록 한다.
- visit와 play 이용
- 4번째 선수는 항상 1번
2. 게임 진행
- 주어진 공식에 따라서 구현한다
dfs(1)로 시작해야하는데 dfs(0)으로 시작하여서 한참을 고민했다
dfs(0)으로 해도
visit[1]은 1이니깐 적용 안되고 그 다음부터 시작하니깐 보기에 있는 것은 다 맞았다
설계대로 되는지 검증하기
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int visit[10];
int play[10];
int map[55][11];
int n;
int house[5];
int ans = 0;
int Anta() {
int nowscore = 0;
if (house[3] == 1) {
house[3] = 0;
nowscore++;
}
if (house[2] == 1) {
house[3] = 1;
house[2] = 0;
}
if (house[1] == 1) {
house[2] = 1;
house[1] = 0;
}
house[1] = 1;
return nowscore;
}
int TwoRoo() {
int nowscore = 0;
if (house[3] == 1) {
house[3] = 0;
nowscore++;
}
if (house[2] == 1) {
nowscore++;
house[2] = 0;
}
if (house[1] == 1) {
house[3] = 1;
house[1] = 0;
}
house[2] = 1;
return nowscore;
}
int ThreeRoo() {
int nowscore = 0;
if (house[3] == 1) {
house[3] = 0;
nowscore++;
}
if (house[2] == 1) {
nowscore++;
house[2] = 0;
}
if (house[1] == 1) {
nowscore++;
house[1] = 0;
}
house[3] = 1;
return nowscore;
}
int HomeRun() {
int nowscore = 0;
if (house[3] == 1) {
house[3] = 0;
nowscore++;
}
if (house[2] == 1) {
nowscore++;
house[2] = 0;
}
if (house[1] == 1) {
nowscore++;
house[1] = 0;
}
nowscore++;
return nowscore;
}
int Game(int arr[]) {
//memset(house, 0, sizeof(house));
int turn = 1;
int round = 1;
int score = 0;
int cnt = 0;
while (true) {
if (round == n + 1)
break;
int out = 0;
//한 라운드
memset(house, 0, sizeof(house));
while (true) {
if (map[round][arr[turn]] == 0)
out++;
else if (map[round][arr[turn]] == 1)
{
if (house[3] == 1) {
house[3] = 0;
score++;
}
if (house[2] == 1) {
house[3] = 1;
house[2] = 0;
}
if (house[1] == 1) {
house[2] = 1;
house[1] = 0;
}
house[1] = 1;
}
else if (map[round][arr[turn]] == 2)
{
if (house[3] == 1) {
house[3] = 0;
score++;
}
if (house[2] == 1) {
score++;
house[2] = 0;
}
if (house[1] == 1) {
house[3] = 1;
house[1] = 0;
}
house[2] = 1;
}
else if (map[round][arr[turn]] == 3)
{
if (house[3] == 1) {
house[3] = 0;
score++;
}
if (house[2] == 1) {
score++;
house[2] = 0;
}
if (house[1] == 1) {
score++;
house[1] = 0;
}
house[3] = 1;
}
else if (map[round][arr[turn]] == 4)
{
if (house[3] == 1) {
house[3] = 0;
score++;
}
if (house[2] == 1) {
score++;
house[2] = 0;
}
if (house[1] == 1) {
score++;
house[1] = 0;
}
score++;
}
turn++;
if (turn == 10)
turn = 1;
if (out == 3)
break;
}
round++;
}
return score;
}
int ccnt = 0;
void dfs(int cnt) {
if (cnt == 4) {
play[4] = 1;
dfs(cnt + 1);
return;
}
if (cnt == 10) {
ccnt++;
int now = Game(play);
if (ans < now) {
ans = now;
}
return;
}
//i는 타순
for (int i = 2; i <= 9; i++) {
if (visit[i] == 0) {
visit[i] = 1;
play[cnt] = i;
dfs(cnt + 1);
visit[i] = 0;
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 9; j++) {
scanf("%d", &map[i][j]);
}
}
visit[1] = 1;
dfs(1);
printf("%d\n", ans);
// cout << ccnt << endl;
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준 14501] 퇴사 (0) | 2020.05.16 |
---|---|
[백준 17472] 다리 만들기2 (0) | 2020.05.14 |
[백준 17136] 색종이 붙히기 (0) | 2020.05.02 |
[백준 17135] 캐슬 디펜스 (0) | 2020.05.02 |
[백준 17070] 파이프 옮기기1 (0) | 2020.05.02 |
Comments