'배열안의 숫자 옮기기'에 해당되는 글 1건

  1. 2019.10.24 10.23 배열에서 숫자이동시키기

10.23 배열에서 숫자이동시키기

C#/실습 2019. 10. 24. 17:35

0,4 위치에 있는 숫자 2를 2,0으로 옮겼다가 다시 0,4로(지그재그) 옮긴다.

코드:

 

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _10._23_step3
{
    class App
    {
        int[,] arr =
        {
            {0,0,0,0,2},
            {0,0,0,0,0},
            {0,0,0,0,0}
        };
        int count=1;
        public App()
        {
            MoveTo20(04);
            MoveTo04(20);
        }
        public void MoveTo04(int row, int col)
        {
            if (count %3!=0&& Check(row, col + 1== true && arr[row,col+1==0 )
            {
                arr[row, col + 1= arr[row, col];
                arr[row, col] = 0;
                count++;
                Print();
                MoveTo04(row, col+1);
            }
            else if(count%3==0&& Check(row-1, col)==true && arr[row-1,col]==0)
            {
                arr[row-1, col] = arr[row, col];
                arr[row, col] = 0;
                count++;
                Print();
                MoveTo04(row-1, col);
            }
        }
        public void MoveTo20(int row, int col)
        {
            if(Check(row, col - 1)==true && arr[row,col-1== 0)
            {
                arr[row, col - 1= arr[row, col];
                arr[row, col] = 0;
                Print();
                MoveTo20(row, col - 1);
            }
            else if (Check(row, col - 1== false && Check(row + 1, col) == true && arr[row + 1, col] == 0)
            {
                arr[row + 1, col] = arr[row, col];
                arr[row, col] = 0;
                Print();
                MoveTo20(row + 1, col);
            }
            
        }
        public void Print()
        {
            Console.WriteLine("--------------------");
            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 bool Check(int nextIndexRow, int nextIndexCol)
        {
            if(nextIndexRow>=0 && nextIndexRow<3 
               && nextIndexCol>=0 && nextIndexCol<5 
               && arr[nextIndexRow, nextIndexCol]==0)
            {
                return true;
            }
            return false;
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
: