Game Development, 게임개발/개발

오브젝트를 해당 위치로 옮기기 - Unity

게임이 더 좋아 2021. 5. 28. 16:51
반응형
728x170

이 문제도 기본적이면서 어려운 문제일 수 있다.

이 역시 생각의 흐름을 써놓아야 하기 때문에 기록한다

https://www.codingame.com/ide/puzzle/power-of-thor-episode-1

 

 

 


 

#풀이

using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 * ---
 * Hint: You can use the debug stream to print initialTX and initialTY, if Thor seems not follow your orders.
 **/
class Player
{
    static void Main(string[] args)
    {
        string[] inputs = Console.ReadLine().Split(' ');
        int lightX = int.Parse(inputs[0]); // the X position of the light of power
        int lightY = int.Parse(inputs[1]); // the Y position of the light of power
        int initialTx = int.Parse(inputs[2]); // Thor's starting X position
        int initialTy = int.Parse(inputs[3]); // Thor's starting Y position

        int nx = lightX - initialTx;
        int ny = lightY - initialTy;

        // game loop
        while (true)
        {
            int remainingTurns = int.Parse(Console.ReadLine()); // The remaining amount of turns Thor can move. Do not remove this line.
            string s1 = "";
            string s2 = "";

            // Write an action using Console.WriteLine()
            // To debug: Console.Error.WriteLine("Debug messages...");
            if(nx > 0){
                s1 = "E";
                nx--;
            }else if(nx < 0){
                s1 = "W";
                nx++;
            }

            if(ny > 0){
                s2 = "S";
                ny--;
            }else if(ny < 0){
                s2 = "N";
                ny++;
            }

            // A single line providing the move to be made: N NE E SE S SW W or NW
            Console.WriteLine(s2+s1);
        }
    }
}

 

 

우리가 맨날 생각하는 이동과 다르게

얘는 대각선으로도 움직인다.

 

아무튼 생각의 흐름을 써보자

 

토르의 위치와 가려는 목표의 위치가 좌표로 주어진다.

이 토르는 대각선으로도 이동할 수 있기에 x, y 따로움직이는 것은 최소시간에 부합하지 않다.

그렇다면 해당 위치까지 어떻게 해야할까?

우선 좌표라는 것은 x만큼 떨어진 거리, y만큼 떨어진 거리를 의미하기 때문에 서로의 위치를 빼면

서로의 각 축에서 떨어진거리가 나올터였다.

 

맵에 가지 말아야 할 공간이 있다면 OutOfBounds 처리를 해줘야겠지만 여기는 없다.

그래서 무조건 예외처리를 하지 않더라도 맵을 벗어나지 않는다.

 

떨어진 거리를 구했다.

 

그다음 토르가 움직일 수 있는 방향은 몇가지가 있는가?

(x 가 -1,0,1) (y가 -1,0,1) 즉 2^3  = 8 

가지가 나온다.

 

x축 어디로 가야하는가, y축은 어디로 가야하는가를 각각 구해 합치면 될 것 같았다.

그래서 해당 nx,ny를 기준으로 0,0이 도달할 때까지

nx의 값과 ny값을 이용해서 토르를 움직이게 만들었다.

 

반응형
그리드형