Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
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
Archives
Today
Total
관리 메뉴

다빈치코드

[코드트리 조별과제] 본문

카테고리 없음

[코드트리 조별과제]

Rash_99 2024. 8. 10. 15:32

https://www.codetree.ai/missions/5/problems/the-moment-we-meet/introduction

 

코드트리 | 코딩테스트 준비를 위한 알고리즘 정석

국가대표가 만든 코딩 공부의 가이드북 코딩 왕초보부터 꿈의 직장 코테 합격까지, 국가대표가 엄선한 커리큘럼으로 준비해보세요.

www.codetree.ai

 

문제 - 만나는 그순간까지

각 상황마다 적절한 캐릭터를 이동시켜 만나는 순간을 찾는 문제이다

배열에 움직임을 담고, 반복문을 통해서 해결했다.

import Foundation

var input = readLine()!.split(separator: " ").map { Int($0)! }
let N = input[0]
let M = input[1]
var flag = false

var a = 0
var b = 0
var aCommand: [String] = []
var bCommand: [String] = []
var count = 0

for _ in 0..<N {
    let tmp = readLine()!.split(separator: " ").map { String($0) }
    for _ in 0..<Int(tmp[1])! {
        aCommand.append(tmp[0])
    }
}

for _ in 0..<M {
    let tmp = readLine()!.split(separator: " ").map { String($0) }
    for _ in 0..<Int(tmp[1])! {
        bCommand.append(tmp[0])
    }
}


func moveLeft(_ player: Int) -> Int {
    return player - 1
}

func moveRight(_ player: Int) -> Int {
    return player + 1
}




for i in 0..<aCommand.count {
    if aCommand[i] == "R" {
        a = moveRight(a)
    } else {
        a = moveLeft(a)
    }

    if bCommand[i] == "R" {
        b = moveRight(b)
    } else {
        b = moveLeft(b)
    }
    count += 1
    if a == b { 
        print(count)
        flag = true
        break
    }
}

if !flag {
    print("-1")
}