fragment-header
fragment-markdown
홈
로그인
로그아웃
내 블로그
설정
로그인
백준 20093 (C++) Coins
최초 업로드: 2025-10-11 15:24:48
최근 수정 시간: 2025-10-11 15:29:02
게시자: rlatjwls3333
카테고리: 백준
조회수: 12
# [Platinum III] Coins [문제 링크](https://www.acmicpc.net/problem/20093) ## 문제 설명 <p>Zahhak, the enemy of Jamshid, has captured Jamshid's daughters, Arnavaz and Shahrnaz. But he decided to offer them an opportunity to free themselves by solving a puzzle. </p> <p>Zahhak has an $8 \times 8$ chessboard with cells labeled from $0$ to $63$, as in the figure. He has put a coin on each of the $64$ cells. The cell with label $c$ has a special coin which is physically identical to the other coins, but it is cursed. Each coin is facing either heads or tails. </p> <p style="text-align: center;"><img alt="" src="https://upload.acmicpc.net/b61480d4-f12f-458c-8660-087c739eab4b/-/preview/" style="width: 256px; height: 254px;"></p> <p>Zahhak invited the sisters to dinner to describe the puzzle: after the dinner, the sisters should go to different rooms. Then Zahhak goes to Arnavaz's room, presents her the chessboard and tells her the value of $c$ (the label of the cell containing the cursed coin). Arnavaz cannot change the position of the coins but can flip (turn over) at least $1$ and at most $k$ coins. She might flip the same coin several times. Then Zahhak goes to the other room and presents the chessboard to Shahrnaz. If she finds the cursed coin, both sisters will be freed. The sisters can agree on a strategy during the dinner, but cannot communicate afterward.</p> <p>Your task is to help the sisters solve Zahhak's puzzle. </p> ## 구현 <p>You should implement two different procedures:</p> <pre> <code>int[] coin_flips(int[] b, int c) </code></pre> <ul> <li>This procedure plays for Arnavaz.</li> <li>$b$: an integer array of length $64$, demonstrating the chessboard that Zahhak presents to Arnavaz. The value of $b[i]$ (for $0 \leq i \leq 63$) is either $0$ or $1$, which indicates the coin on cell $i$ is heads or tails, respectively.</li> <li>$c$: label of the cell that contains the cursed coin.</li> <li>It should return an array containing labels of the cells that Arnavaz flips their coins. Its length should be between $1$ and $k$, inclusive. It can contain a value more than once.</li> </ul> <pre> <code>int find_coin(int[] b) </code></pre> <ul> <li>This procedure plays for Shahrnaz.</li> <li>$b$: an integer array of length $64$, demonstrating the chessboard that Zahhak presents to Shahrnaz (after Arnavaz has flipped some coins). </li> <li>It should return $c$, the position of the cursed coin.</li> </ul> <p>There are $T$ scenarios. For each scenario, the grader calls the procedure <code>coin_flips</code>. Based on its returned value, the grader updates the chessboard and calls the procedure <code>find_coin</code>. Note that in the judging system these procedures are called in separate programs. In the first program, procedure <code>coin_flips</code> is called once for each scenario. Invocations of procedure <code>find_coin</code> are made in the second program. The behavior of your implementation for each scenario must be independent of the order of the scenarios, as scenarios might not have the same order in the two programs.</p> ## 제한 <ul> <li>$1 \leq T \leq 1000$,</li> <li>$0 \leq c \leq 63$.</li> </ul> ## 예제 <p>Suppose the sisters decide that Arnavaz only flips the cursed coin and Shahrnaz reports the position of one of the coins with tails facing up, or $0$ if there is no such coin. Clearly, this is just an example, not a correct strategy.</p> <p>The grader makes the following procedure call:</p> <pre> <code>coin_flips([0,0,...,0,0], 63) </code></pre> <p>In this example, $b$ is an array of length $64$ filled with $0$'s which means all coins on the chessboard have heads facing up. This procedure returns an integer array of length $1$, containing a single value $[63]$.</p> <p>Then the grader flips the coin in the cell number $63$ and makes the following procedure call:</p> <pre> <code>find_coin([0,0,...,0,1]) </code></pre> <p>This procedure returns $63$, and it is the correct position of the cursed coin.</p> ## 서브테스크 <table class="table table-bordered td-middle subtask-table" style="width: 100%;"><thead><th style="width: 5%;">번호</th><th style="width: 5%;">배점</th><th style="width: 90%;">제한</th></thead><tbody><tr data-subtask-id="1"><td>1</td><td>10</td><td class="subtask-body"><p>$c < 2$, $k = 1$</p> </td></tr><tr data-subtask-id="2"><td>2</td><td>15</td><td class="subtask-body"><p>$c < 3$, $k = 1$</p> </td></tr><tr data-subtask-id="3"><td>3</td><td>10</td><td class="subtask-body"><p>$k = 64$</p> </td></tr><tr data-subtask-id="4"><td>4</td><td>15</td><td class="subtask-body"><p>$k = 8$</p> </td></tr><tr data-subtask-id="5"><td>5</td><td>50</td><td class="subtask-body"><p>$k = 1$</p> </td></tr></tbody></table> ## 샘플 그레이더 <p>The sample grader reads the input in the following format:</p> <ul> <li>line $1$: $\;\;T$</li> <li>block $i$ (for $0 \leq i \leq T-1$): a block of $9$ lines, representing scenario $i$. <ul> <li>line $1$: $\;\;c$</li> <li>line $2+j$ (for $0 \leq j \leq 7$): a binary (<code>0</code>/<code>1</code>) string of length $8$ representing row $j$ of table $b$</li> </ul> </li> </ul> <p>The sample grader writes the output in the following format:</p> <ul> <li>line $1+i$ (for $0 \leq i \leq T-1$): the verdict of your solution for scenario $i$.</li> </ul> ## 첨부 <ul> <li><a href="https://upload.acmicpc.net/603d24c1-8d0c-4a42-bc74-cfb2504f742f/">coins.cpp</a></li> <li><a href="https://upload.acmicpc.net/9c4ea6ae-31a1-4654-81eb-a1d185645860/">coins.h</a></li> <li><a href="https://upload.acmicpc.net/6352bff3-d649-4c20-96c3-da98539874c1/">grader.cpp</a></li> <li><a href="https://upload.acmicpc.net/6361851d-59d5-4270-b75d-b982e045f317/">01.in</a></li> </ul> ## 풀이 배열 b에서 1인 위치를 전부 XOR하면 임의의 6비트 이진수 하나가 나온다. 배열 b에서 1인 위치를 전부 XOR 했을 때, c가 나오게 하기 위해 이 나온 수를 c와 XOR한 위치의 동전을 (0이었다면 1로, 1이었다면 0으로) flip 해주면 된다. 이로 인해 배열을 받은 다음 사람은 배열 b에서 1인 위치를 전부 XOR하여 c의 위치를 알 수 있다. ``` c++ #include "coins.h" using namespace std; int getXor(vector<int> b) { int xorVal=0; for(int i=0;i<64;i++) { if(b[i]) xorVal ^= i; } return xorVal; } vector<int> coin_flips(vector<int> b, int c) { vector<int> flips(1); flips[0] = getXor(b)^c; return flips; } int find_coin(vector<int> b) { return getXor(b); } ```