2019-10-23 23일차 재귀함수 4 x 5행렬에서 숫자 움직이기

Console Programming/C# Console 2019. 10. 24. 17:39

                {0, 0, 0, 0, 2},
                {0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0}

 

이와 같은 모양의 행렬에서 [0,4]에 있는 숫자 2를 [0,0]자리로 이동한 뒤 [3,0]으로 이동한다.

 

그 뒤 지그재그로 이동해 원래 위치로 돌아간다. (오른쪽2, 위1, 오른쪽1, 위1, 오른쪽1, 위1)

 

 

 

 

Program.cs

namespace Syntax03
{
    class Program
    {
        static void Main(string[] args)
        {
            new App();
        }
    }
}

App.cs

using System;
namespace Syntax03
{
    public class App
    {
        int[,] arr;
        ArrInfo arrinfo = new ArrInfo();
        public App()
        {
            arr = new int[,]
            {
                {0, 0, 0, 0, 2},
                {0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0}
            };
            Show();           

            
            Move(0,4); //왼쪽으로 옮기고 끝까지 가면 아래로 옮긴다

            Show();

            MoveZigZag(3, 0);

            Show();
                       

        }

        public void Show()
        {
            for(int i =0; i<arr.GetLength(0); i++)
            {
                for(int j =0; j<arr.GetLength(1); j++)
                {
                    Console.Write($"{arr[i,j]}\t");
                }
                Console.WriteLine();
            }
            Console.WriteLine("===============================");

        }

        public void MoveZigZag(int startRow, int startCol)
        {
            var info1 = MoveRightByCount(3, 0, 2);
            Show();
            var info2 = MoveUpByCount(info1.row, info1.col, 1);
            Show();
            var info3 = MoveRightByCount(info2.row, info2.col, 1);
            Show();
            var info4 = MoveUpByCount(info3.row, info3.col, 1);
            Show();
            var info5 = MoveRightByCount(info4.row, info4.col, 1);
            Show();
            var info6 = MoveUpByCount(info5.row, info5.col, 1);
        }

        public bool Check(int startRow,int startCol)
        {
            if (startCol>=0 && startCol < arr.GetLength(1) && startRow>=0 && startRow < arr.GetLength(0)
                && arr[startRow,startCol] ==0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public void Move(int startRow, int startCol)
        {
            var nextCol = startCol - 1;
            var val = arr[startRow, startCol];
            if(Check(startRow,nextCol))
            {
                arr[startRow, nextCol] = val;
                arr[startRow, startCol] = 0;
                Move(startRow, nextCol);

            }
            else
            {
                nextCol += 1;
                //Console.WriteLine($"{startRow}, {nextCol}");

                MoveDown(startRow,nextCol);
                return;
            }
        }

        public void MoveRight(int startRow, int startCol)
        {
            var nextCol = startCol + 1;
            var val = arr[startRow, startCol];
            if (Check(startRow, nextCol))
            {
                arr[startRow, nextCol] = val;
                arr[startRow, startCol] = 0;
                MoveRight(startRow, nextCol);
            }
            else
            {
                return;
            }
        }
        public ArrInfo MoveRightByCount(int startRow, int startCol,int count)
        {            
            var nextCol = startCol + 1;
            var val = arr[startRow, startCol];
            int nextCount = count - 1;
            arrinfo.row = startRow;
            arrinfo.col = startCol;
            if (nextCount<0)
            {
               
                return arrinfo; 
            }
            else
            {
                if (Check(startRow, nextCol))
                {
                    arrinfo.row = startRow;
                    arrinfo.col = nextCol;
                    arr[startRow, nextCol] = val;
                    arr[startRow, startCol] = 0;
                    MoveRightByCount(startRow, nextCol, nextCount);
                }
                else
                {
                    return arrinfo;
                }          
            }
            return arrinfo;
        }


        public void MoveLeft(int startRow, int startCol)
        {
            var nextCol = startCol - 1;
            var val = arr[startRow, startCol];
            if (Check(startRow, nextCol))
            {
                arr[startRow, nextCol] = val;
                arr[startRow, startCol] = 0;
                MoveLeft(startRow, nextCol);

            }
            else
            {               
                return;
            }
        }
        public ArrInfo MoveLeftByCount(int startRow, int startCol, int count)
        {
            var nextCol = startCol - 1;
            var val = arr[startRow, startCol];
            int nextCount = count - 1;
            arrinfo.row = startRow;
            arrinfo.col = startCol;
            if (nextCount < 0)
            {
                return arrinfo;
            }
            if (Check(startRow, nextCol))
            {
                arrinfo.row = startRow;
                arrinfo.col = nextCol;
                arr[startRow, nextCol] = val;
                arr[startRow, startCol] = 0;
                MoveLeftByCount(startRow, nextCol, nextCount);

            }
            else
            {
                return arrinfo;
            }
            return arrinfo;
        }

        public void MoveUp(int startRow, int startCol)
        {
            var nextRow = startRow - 1;
            var val = arr[startRow, startCol];      
            if (Check(nextRow, startCol))
            {
                arr[nextRow, startCol] = val;
                arr[startRow, startCol] = 0;
                MoveUp(nextRow, startCol);
            }
            else
            {
                return;
            }
        }
        public ArrInfo MoveUpByCount(int startRow, int startCol,int count)
        {
            var nextRow = startRow - 1;
            var val = arr[startRow, startCol];
            int nextCount = count - 1;
            arrinfo.row = startRow;
            arrinfo.col = startCol;
            if (nextCount < 0)
            {
                return arrinfo;
            }
            if (Check(nextRow, startCol))
            {
                arrinfo.row = nextRow;
                arrinfo.col = startCol;
                arr[nextRow, startCol] = val;
                arr[startRow, startCol] = 0;
                MoveUpByCount(nextRow, startCol, nextCount);
            }
            else
            {
                return arrinfo;
            }
            return arrinfo;
        }
        public void MoveDown(int startRow, int startCol)
        {
            var nextRow = startRow + 1;
            var val = arr[startRow, startCol];

            if(Check(nextRow,startCol))
            {
                arr[nextRow, startCol] = val;
                arr[startRow, startCol] = 0;
                MoveDown(nextRow, startCol);
            }
            else
            {
                return;
            }
        }

        public ArrInfo MoveDownByCount(int startRow, int startCol,int count)
        {
            var nextRow = startRow + 1;
            var val = arr[startRow, startCol];
            int nextCount = count - 1;
            arrinfo.row = startRow;
            arrinfo.col = startCol;
            if (nextCount < 0)
            {
                return arrinfo;
            }
            if (Check(nextRow, startCol))
            {
                arrinfo.row = nextRow;
                arrinfo.col = startCol;
                arr[nextRow, startCol] = val;
                arr[startRow, startCol] = 0;
                MoveDownByCount(nextRow, startCol, nextCount);
            }
            else
            {
                return arrinfo;
            }
            return arrinfo;
        }

    }
}

ArrInfo.cs

namespace Syntax03
{
    
    public class ArrInfo
    {
        public int row;
        public int col;
        public ArrInfo()
        {

        }
    }
}

 

 

: