Outdated/Algorithm Solution

[BOJ] 3101번 토끼의 이동

해달 2020. 4. 13. 18:00

복기

타입에 주의하자...


코드

  • C++ 17


#include <cstdio>

 

using namespace std;

 

long long Sigma(int N)

{

    return 1LL * N * (N + 1LL) / 2LL;

}

 

int main()

{

    int N, K, R = 0, C = 0;

    scanf("%d %d\n", &N, &K);

    long long result = 1;

    for (int i = 0; i < K; i++)

    {

        switch (getchar())

        {

        case 'U':

            --R;

            break;

        case 'D':

            ++R;

            break;

        case 'L':

            --C;

            break;

        default:

            ++C;

        }

        

        // 좌표를 이용해서 값을 얻어낼 수 있다.

        long long first, dist;

        if (R + C < N)

        {

            // 각 줄은 1의 등차수열이다.

            // 첫번째 값을 얻어내고

            first = 1LL + Sigma(R + C);

            // 첫번쨰 값의 좌표로부터 얼마나 떨어져 있는지를 계산한다.

            dist = ((R + C) % 2 ? R : C);

        } 

        // 여기를 계산할 때 주의해야 한다.

        else

        {

            first = 1LL + Sigma(N) + Sigma(N - 1) - Sigma(2 * N - (R + C + 1));

            dist = ((R + C) % 2 ? R : C) - ((long long)R + C - N + 1);

        }

        result += (first + dist);

    }

 

    printf("%lld\n", result);

}


  • C# 6.0


using System;

 

namespace Csharp

{

    class Program

    {

        static void Main(string[] args)

        {

            long N, K;

            string[] inputs = Console.ReadLine().Split();

            N = Convert.ToInt64(inputs[0]);

            K = Convert.ToInt64(inputs[1]);

 

            long R = 0L, C = 0L, result = 1L;

            string moves = Console.ReadLine();

            for (int i = 0; i < K; i++)

            {

                switch (moves[i])

                {

                    case 'U': --R; break;

                    case 'D': ++R; break;

                    case 'L': --C; break;

                    case 'R': ++C; break;

                }

 

                long lineNo = R + C;

                long first, dist;

                if (lineNo < N)

                {

                    first = 1L + Sigma(lineNo);

                    dist = (lineNo % 2 == 1) ? R : C;

                }

                else

                {

                    first = Sigma(N) + Sigma(N - 1) + Sigma(2L * N - lineNo + 1);

                    dist = ((lineNo % 2 == 1) ? R : C) - (lineNo - N); 

                }

                result += (first + dist);

            }

 

            Console.WriteLine(result);

        }

        

        static long Sigma(long n)

        {

            return n * (n + 1L) / 2L;

        }

    }

}


  • Python 3


from sys import stdin

input = stdin.readline

 

N, K = map(int, input().split())

R, C = 1, 1

firstNumbers = [ 0 ] * (N * 2)

firstNumbers[1] = 1

 

def getLength(lineNo):

    if lineNo <= N:

        return lineNo

    

    return N - lineNo % N

 

for i in range(2, N * 2):

    firstNumbers[i] = firstNumbers[i - 1] + getLength(i - 1)

 

def getNumber(r, c):

    lineNumber = r + c - 1

    firstNumber = firstNumbers[lineNumber]

    

    if lineNumber <= N:

        if lineNumber % 2 == 0:

            return firstNumber + (r - 1)

        else:

            return firstNumber + (c - 1)

    else:

        if lineNumber % 2 == 0:

            return firstNumber + (N - c)

        else:

            return firstNumber + (N - r)

    

result = 1

for ch in input().rstrip():

    if ch == 'U':

        R -= 1

    elif ch == 'D':

        R += 1

    elif ch == 'L':

        C -= 1

    elif ch == 'R':

        C += 1

 

    result += getNumber(R, C)

 

print(result)


'Outdated > Algorithm Solution' 카테고리의 다른 글

[BOJ] 1083번 소트  (0) 2020.04.20
[BOJ] 1431번 시리얼 번호  (0) 2020.04.20
[BOJ] 4948번 베르트랑 공준  (0) 2020.04.13
[BOJ] 1024번 수열의 합  (0) 2020.03.23
[BOJ] 1676번 팩토리얼 0의 개수  (0) 2020.03.23