10.23 배열에서 숫자이동시키기
C#/실습 2019. 10. 24. 17:350,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;
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(0, 4);
MoveTo04(2, 0);
}
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
|
'C# > 실습' 카테고리의 다른 글
10.30 클래스로 LinkedList 구현하기 (메서드, 속성의 재귀호출) (0) | 2019.10.30 |
---|---|
10.23 Stack 활용하기 (0) | 2019.10.24 |
10.24 벡터 (0) | 2019.10.24 |
2048 프로그램 (저장기능x) (0) | 2019.10.23 |
10.17 상속 클래스를 이용한 뽑기박스 (0) | 2019.10.17 |