작성일 :

문제 링크

13224번 - Chop Cup

설명

컵 게임에서 공의 최종 위치를 찾는 상황에서, 컵 교환 명령어로 구성된 문자열(최대 50글자)이 주어질 때, 모든 교환을 수행한 후 공이 위치한 컵의 번호를 구하는 문제입니다.

공은 처음에 왼쪽 컵(1번) 아래에 있으며, 세 가지 명령어에 따라 컵을 교환합니다:

  • A: 왼쪽(1번) ↔ 가운데(2번)
  • B: 가운데(2번) ↔ 오른쪽(3번)
  • C: 왼쪽(1번) ↔ 오른쪽(3번)


접근법

세 개의 컵 중 공이 어느 컵 아래에 있는지를 불리언 배열로 표현합니다.

각 명령어를 순서대로 처리하며, 해당하는 두 컵의 상태를 교환합니다. 모든 명령을 수행한 후, 공이 있는 위치를 찾아 1부터 시작하는 번호로 출력합니다.



Code

C#

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
using System;

namespace Solution {
  class Program {
    static void Main(string[] args) {
      var cups = new bool[] { true, false, false };
      var operations = Console.ReadLine()!;
      
      foreach (var operation in operations) {
        if (operation == 'A') Swap(cups, 0, 1);
        else if (operation == 'B') Swap(cups, 1, 2);
        else if (operation == 'C') Swap(cups, 0, 2);
      }
      
      for (var i = 0; i < 3; i++) {
        if (cups[i]) {
          Console.WriteLine(i + 1);
          break;
        }
      }
    }

    static void Swap(bool[] arr, int a, int b) {
      var temp = arr[a];
      arr[a] = arr[b];
      arr[b] = temp;
    }
  }
}

C++

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
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  bool cups[3] = {true, false, false};
  string operations; cin >> operations;
  
  for (char operation : operations) {
    if (operation == 'A') swap(cups[0], cups[1]);
    else if (operation == 'B') swap(cups[1], cups[2]);
    else if (operation == 'C') swap(cups[0], cups[2]);
  }
  
  for (int i = 0; i < 3; i++) {
    if (cups[i]) {
      cout << i + 1 << "\n";
      break;
    }
  }
  
  return 0;
}