C# 백준 3190 뱀

2024. 2. 21. 01:47C# 백준 알고리즘

 
시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 128 MB 72316 30608 20543 40.695%

문제

'Dummy' 라는 도스게임이 있다. 이 게임에는 뱀이 나와서 기어다니는데, 사과를 먹으면 뱀 길이가 늘어난다. 뱀이 이리저리 기어다니다가 벽 또는 자기자신의 몸과 부딪히면 게임이 끝난다.

게임은 NxN 정사각 보드위에서 진행되고, 몇몇 칸에는 사과가 놓여져 있다. 보드의 상하좌우 끝에 벽이 있다. 게임이 시작할때 뱀은 맨위 맨좌측에 위치하고 뱀의 길이는 1 이다. 뱀은 처음에 오른쪽을 향한다.

뱀은 매 초마다 이동을 하는데 다음과 같은 규칙을 따른다.

  • 먼저 뱀은 몸길이를 늘려 머리를 다음칸에 위치시킨다.
  • 만약 벽이나 자기자신의 몸과 부딪히면 게임이 끝난다.
  • 만약 이동한 칸에 사과가 있다면, 그 칸에 있던 사과가 없어지고 꼬리는 움직이지 않는다.
  • 만약 이동한 칸에 사과가 없다면, 몸길이를 줄여서 꼬리가 위치한 칸을 비워준다. 즉, 몸길이는 변하지 않는다.

사과의 위치와 뱀의 이동경로가 주어질 때 이 게임이 몇 초에 끝나는지 계산하라.

입력

첫째 줄에 보드의 크기 N이 주어진다. (2 ≤ N ≤ 100) 다음 줄에 사과의 개수 K가 주어진다. (0 ≤ K ≤ 100)

다음 K개의 줄에는 사과의 위치가 주어지는데, 첫 번째 정수는 행, 두 번째 정수는 열 위치를 의미한다. 사과의 위치는 모두 다르며, 맨 위 맨 좌측 (1행 1열) 에는 사과가 없다.

다음 줄에는 뱀의 방향 변환 횟수 L 이 주어진다. (1 ≤ L ≤ 100)

다음 L개의 줄에는 뱀의 방향 변환 정보가 주어지는데, 정수 X와 문자 C로 이루어져 있으며. 게임 시작 시간으로부터 X초가 끝난 뒤에 왼쪽(C가 'L') 또는 오른쪽(C가 'D')로 90도 방향을 회전시킨다는 뜻이다. X는 10,000 이하의 양의 정수이며, 방향 전환 정보는 X가 증가하는 순으로 주어진다.

출력

첫째 줄에 게임이 몇 초에 끝나는지 출력한다.

 

예제 입력 1

6
3
3 4
2 5
5 3
3
3 D
15 L
17 D

예제 출력 1

9

예제 입력 2

10
4
1 2
1 3
1 4
1 5
4
8 D
10 D
11 D
13 L

예제 출력 2

21

예제 입력 3

10
5
1 5
1 3
1 2
1 6
1 7
4
8 D
10 D
11 D
13 L

예제 출력 3

13
 
정답 코드
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Prob3190_뱀
{
    public enum Direction
    {
        Up,
        Down,
        Left,
        Right
    }
    public class XY
    {
        public int X { get; set; }
        public int Y { get; set; }

        public XY(int x, int y)
        {
            X = x;
            Y = y;
        }
    }
    class Program
    {
        private static int[,] map;
        private static int N;
        private static int K;
        private static int D;
        private static char[] dict = new char[100002];
        private static Direction enumDirection;    
        private static int tail = 0;

        private static int[] x = {0, 0, -1, 1};  
        private static int[] y = { -1, 1, 0, 0 };  
        // 상 하 좌 우


        static void Main(string[] args)
        {
            /*int[,] map;
            int N;

            int K;
   
            int D;
            char[] dict = new char[100002];
            int tail = 0 ;
            Direction enumDirection;


            int[] x = { 0, 0, -1, 1 };
            int[] y = { -1, 1, 0, 0 };*/


            Queue<XY> queue = new Queue<XY>();

            N = int.Parse(Console.ReadLine());    
            map = new int[N + 2, N + 2];
            K = int.Parse(Console.ReadLine());

            for (int i = 0; i < K; i++)
            {
                string[] arr = Console.ReadLine().Split();

                int x1 = int.Parse(arr[0]);
                int y1 = int.Parse(arr[1]);

                map[x1, y1] = 1;
            }

            D = int.Parse(Console.ReadLine());

            for (int i = 0; i < D; i++)
            {
                string[] arr = Console.ReadLine().Split();

                int index = int.Parse(arr[0]);
                char direction = char.Parse(arr[1]);

                dict[index] = direction;
            }

            int count = 1;

            int nx = 1;
            int ny = 1;

            int xx = 3;
            int yy = 3;

            enumDirection = Direction.Right;

            while (true)
            {               
                queue.Enqueue(new XY(nx, ny));
                map[ny, nx] = 2;

                nx += x[xx]; //스네이크 머리
                ny += y[yy];

                if (nx == 0 || ny == 0 || nx == N + 1 || ny == N + 1)
                {
               //     Console.WriteLine("벽에 닿아버림");
                    break;
                }

                if (map[ny, nx] == 2)
                {
                 //  Console.WriteLine("꼬리의 닿아버림");
                    break;
                }

                if (map[ny, nx] == 1)
                {
                    tail++;
                    map[ny, nx] = 0;
                //   Console.Write($"{nx}, {ny}에 있는 사과 먹음 ");
                }

                if (tail < queue.Count)
                {
                    XY xy = queue.Dequeue();
                    map[xy.Y, xy.X] = 0;
                }
                    
             //  Console.WriteLine($"시간 : {count}, 좌표 : {nx},{ny} 현재 방향: {enumDirection.ToString()}현재 꼬리 길이: {tail}");

                if (dict[count] == 'D')
                {
                 //  Console.Write("오른쪽으로 방향 전환 ");
                    switch (enumDirection)
                    {
                        case Direction.Up:
                            xx = 3;
                            yy = 3;
                            enumDirection = Direction.Right;
                            break;
                        case Direction.Down:
                            xx = 2;
                            yy = 2;
                            enumDirection = Direction.Left;
                            break;
                        case Direction.Left:
                            xx = 0;
                            yy = 0;
                            enumDirection = Direction.Up;
                            break;
                        case Direction.Right:
                            xx = 1;
                            yy = 1;
                            enumDirection = Direction.Down;
                            break;
                    }

                }
                else if (dict[count] == 'L')
                {
                   // Console.Write("왼쪽으로 방향 전환 ");
                    switch (enumDirection)
                    {
                        case Direction.Up:
                            xx = 2;
                            yy = 2;
                            enumDirection = Direction.Left;
                            break;
                        case Direction.Down:
                            xx = 3;
                            yy = 3;
                            enumDirection = Direction.Right;
                            break;
                        case Direction.Left:
                            xx = 1;
                            yy = 1;
                            enumDirection = Direction.Down;
                            break;
                        case Direction.Right:
                            xx = 0;
                            yy = 0;
                            enumDirection = Direction.Up;
                            break;
                    }
                }

                count++;
            }

            // 현재 남은거 로직

            // 뱀 머리 방향전환
            // 뱀 꼬리 구현
            // 사과 먹었을때 길이 늘리기 구현

            Console.WriteLine(count);
           // Console.WriteLine(tail);
 
        }
    }
}

'C# 백준 알고리즘' 카테고리의 다른 글

C# 백준 2798 블랙잭  (0) 2024.02.23
C# 백준 10025 게으른 백곰  (0) 2024.02.23
C# 백준 1463 1로 만들기  (0) 2023.11.07
C# 백준 2775 부녀회장이 될테야  (0) 2023.08.19
C# 백준 1312 소수  (0) 2023.08.16