BOJ 12147. Brattleship
๐โโ๏ธ[Silver III] Brattleship (Small) - 12147
์ฑ๋ฅ ์์ฝ
๋ฉ๋ชจ๋ฆฌ: 2020 KB, ์๊ฐ: 0 ms
๋ถ๋ฅ
์ ๋ ํน, ๊ธฐํํ, ์ํ
์ ์ถ ์ผ์
2024๋ 6์ 10์ผ 19:50:21
๋ฌธ์ ์ค๋ช
You're about to play a simplified "battleship" game with your little brother. The board for this game is a rectangular grid with R rows and C columns. At the start of the game, you will close your eyes, and you will keep them closed until the end of the game. Your little brother will take a single rectangular 1 x W ship and place it horizontally somewhere on the board. The ship must always fit entirely on the board, with each cell of the ship occupying exactly one of the grid's cells, and it can never be rotated.
In each turn of the game, you name a cell on the board, and your little brother tells you whether that is a hit (one of the cells occupied by the ship) or a miss. (Your little brother doesn't say which part of the ship was hit -- just that the cell you named has a part of the ship in it.) You have perfect memory, and can keep track of all the information he has given you. Once you have named all of the cells occupied by the ship, the game is over (the ship is sunk), and your score is the number of turns taken. Your goal is to minimize your score.
Although the ship is not supposed to be moved once it is placed, you know that your little brother, who is a brat, plans to cheat by changing the location of the ship whenever he wants, as long as the ship remains horizontal and completely on the board, and the new location is consistent with all the information he has given so far. For example, for a 1x4 board and 1x2 ship, your little brother could initially place the ship such that it overlaps the leftmost two columns. If your first guess was row 1, column 2, he could choose to secretly move the ship to the rightmost two columns, and tell you that (1, 2) was a miss. If your next guess after that was (1, 3), though, then he could not say that was also a miss and move the ship back to its original location, since that would be inconsistent with what he said about (1, 2) earlier.
Not only do you know that your little brother will cheat, he knows that you know. If you both play optimally (you to minimize your score, him to maximize it), what is the lowest score that you can guarantee you will achieve, regardless of what your little brother does?
์ ๋ ฅ
The first line of the input gives the number of test cases, T. T lines follow, each with three space-separated integers R, C, and W: the number of rows and columns of the board, followed by the width of the ship.
Limits
- 1 โค W โค C.
- T = 55.
- R = 1.
- 1 โค C โค 10.
์ถ๋ ฅ
For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the minimum score you can guarantee.
๐ํ์ด
- ๋ฌธ์ ์ ๋ฆฌ
- ๋ณด๋๋ R x C ํฌ๊ธฐ์ ์ง์ฌ๊ฐํ์ ๋๋ค.
- ๋ฐฐ๋ 1 x W ํฌ๊ธฐ์ ์ํ ๋ฐฐ๋ก ์ฃผ์ด์ง๋๋ค.
- ๊ฒ์์ ์ฌ๋ฌ ๋ผ์ด๋๋ก ์งํ๋๋ฉฐ, ๊ฐ ๋ผ์ด๋๋ง๋ค ์ฃผ์ด์ง ์์น๊ฐ ๋ฐฐ์ ์ผ๋ถ์ธ์ง ํ์ธํฉ๋๋ค.
- ๋ฐฐ๋ ์ ๋ ํ์ ํ ์ ์์ผ๋ฉฐ, ๋ฐฐ์น๊ฐ ๋์์ ์์์ ์ธ ์์น ๋ณ๊ฒฝ์ผ๋ก ์ธํด ์ต์ ์ ๊ฒฝ์ฐ๋ก ์ฎ๊ฒจ์ง ์ ์์ต๋๋ค.
- ๋ชฉํ๋ ์ต์ํ์ ๋ผ์ด๋ ์๋ก ๋ฐฐ์ ์์น๋ฅผ ์ ํํ ์ฐพ๋ ๊ฒ์ ๋๋ค.
- ํด๊ฒฐ ์ ๋ต
- ๊ธฐ๋ณธ ๊ฐ๋ : Rํ C์ด์ ๋ณด๋์์, ๋ฐฐ์ ๊ธธ์ด W๊ฐ ์ฃผ์ด์ง ๋ ์ต์ ์ ๊ฒฝ์ฐ๋ฅผ ๊ณ ๋ คํ์ฌ ์ต์ ์ถ์ธก ํ์๋ฅผ ๊ณ์ฐํฉ๋๋ค.
- ์ต์ ์ ์ ๋ต:
- ๊ฐ ํ์ ๋ ๋ฆฝ์ ์ผ๋ก ์๊ฐํ์ฌ ๊ฐ ํ๋ง๋ค ์ต์ํ์ ์๋๋ก ๋ฐฐ๋ฅผ ์ฐพ์ต๋๋ค.
- ๊ฐ ํ์์์ ์ต์ ์๋ ํ์๋ C // W์ ๋๋ค. ์ด ๊ฐ์ ๊ฐ ํ์์ W ๊ธธ์ด์ ๋ฐฐ๋ฅผ ์ฐพ๊ธฐ ์ํด ํ์ํ ์ต์ํ์ ์๋์ ๋๋ค.
- ๋ฐฐ๊ฐ ํฌํจ๋ ๋ง์ง๋ง ์์น๋ฅผ ์ฐพ๊ธฐ ์ํด ์ถ๊ฐ์ ์ธ W-1 ์๋๊ฐ ํ์ํฉ๋๋ค.
- ์ฌ๋ฌ ํ์ ๊ณ ๋ คํ์ฌ ์ต์ข ์ ์๋ฅผ ๊ณ์ฐํฉ๋๋ค.
์ค๋ช
- ๊ธฐ๋ณธ ์๋ ํ์:
๊ฐ ํ์์
C / W
๋งํผ์ ์๋ ํ์๊ฐ ํ์ํฉ๋๋ค. ์ด๋ ๊ฐ ํ์์ ๋ฐฐ์ ์์น๋ฅผ ์ฐพ๊ธฐ ์ํด ํ์ํ ์ต์ ์๋ ํ์์ ๋๋ค. - ์ถ๊ฐ ์๋ ํ์: ๋ฐฐ์ ์ ํํ ์์น๋ฅผ ์ฐพ๊ธฐ ์ํด ๋ง์ง๋ง์ผ๋ก
W - 1
๋ฒ์ ์ถ๊ฐ ์๋๊ฐ ํ์ํฉ๋๋ค. - ์ต์ข ์๋ ํ์: ๋ชจ๋ ํ์ ๋ํด ๊ธฐ๋ณธ ์๋ ํ์๋ฅผ ๊ณ์ฐํ ํ, ์ถ๊ฐ ์๋ ํ์๋ฅผ ๋ํ์ฌ ์ต์ข ์๋ ํ์๋ฅผ ๊ณ์ฐํฉ๋๋ค.
- ์ถ๊ฐ ์ ๊ฒ:
C % W != 0
์ผ ๊ฒฝ์ฐ ํ ๋ฒ ๋ ์๋ํด์ผ ํฉ๋๋ค.
void solve()
{
int T;
cin >> T;
for (int t = 1; t <= T; ++t)
{
int R, C, W;
cin >> R >> C >> W;
// ๊ฐ ํ๋ง๋ค W ๊ธธ์ด์ ๋ฐฐ๋ฅผ ์ฐพ๊ธฐ ์ํด ํ์ํ ์ต์ ์๋ ํ์
int moves_per_row = C / W;
// ๋ง์ง๋ง ๋ถ๋ถ์์ ๋ฐฐ์ ์์น๋ฅผ ์ ํํ ์ฐพ๊ธฐ ์ํด W-1๋ฒ์ ์ถ๊ฐ ์๋๊ฐ ํ์
int additional_moves = W - 1;
// ๋ชจ๋ ํ์์ ๊ธฐ๋ณธ์ ์ธ ์๋ ํ์๋ฅผ ๊ณ์ฐ
int total_moves = moves_per_row * R + additional_moves;
// ๋ฐฐ๊ฐ ์ ํํ C์ ์ผ์นํ์ง ์์ ๊ฒฝ์ฐ๋ฅผ ์ํด ์ถ๊ฐ ์ ๊ฒ
if (C % W != 0)
{
total_moves += 1;
}
cout << "Case #" << t << ": " << total_moves << '\n';
}
}
๐์ฝ๋
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <climits>
using namespace std;
void solve()
{
int T;
cin >> T;
for (int t = 1; t <= T; ++t)
{
int R, C, W;
cin >> R >> C >> W;
// ๊ฐ ํ๋ง๋ค W ๊ธธ์ด์ ๋ฐฐ๋ฅผ ์ฐพ๊ธฐ ์ํด ํ์ํ ์ต์ ์๋ ํ์
int moves_per_row = C / W;
// ๋ง์ง๋ง ๋ถ๋ถ์์ ๋ฐฐ์ ์์น๋ฅผ ์ ํํ ์ฐพ๊ธฐ ์ํด W-1๋ฒ์ ์ถ๊ฐ ์๋๊ฐ ํ์
int additional_moves = W - 1;
// ๋ชจ๋ ํ์์ ๊ธฐ๋ณธ์ ์ธ ์๋ ํ์๋ฅผ ๊ณ์ฐ
int total_moves = moves_per_row * R + additional_moves;
// ๋ฐฐ๊ฐ ์ ํํ C์ ์ผ์นํ์ง ์์ ๊ฒฝ์ฐ๋ฅผ ์ํด ์ถ๊ฐ ์ ๊ฒ
if (C % W != 0)
{
total_moves += 1;
}
cout << "Case #" << t << ": " << total_moves << '\n';
}
}
int main()
{
FILE* stream;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
freopen_s(&stream, "input.txt", "rt", stdin);
solve();
return 0;
}