-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayUtils.cs
128 lines (123 loc) · 3.78 KB
/
ArrayUtils.cs
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace HoleInTheWall
{
class ArrayUtils
{
char emptyChar = '0';
private char[] GetRow(char[,] figure, int rowNumber)
{
if (rowNumber > figure.GetLength(0))
return null;
else
{
char[] result = new char[figure.GetLength(1)];
for (int i = 0; i < figure.GetLength(1); i++)
{
result[i] = figure[rowNumber, i];
}
return result;
}
}
static Boolean HasChar(char[] array, char symbol)
{
Boolean result = false;
for (int i = 0; i < array.Length; i++)
{
if (array[i] != symbol)
{
result = false;
return result;
}
else
result = true;
}
return result;
}
static char[,] GetArray(char[,] figure)
{
char[,] temp = new char[figure.GetLength(0), figure.GetLength(1)];
for (int i = 0; i < figure.GetLength(0); i++)
{
for (int j = 0; j < figure.GetLength(1); j++)
{
temp[i, j] = figure[i, j];
}
}
return temp;
}
public char[,] CutRow(char[,] temp)
{
Boolean hasEmptyRow = true;
while (hasEmptyRow)
{
for (int i = 0; i < temp.GetLength(0); i++)
{
char[] s = GetRow(temp, i);
string charsStr = new string(s);
if (HasChar(s, emptyChar))
{
temp = RemoveRow(temp, i);
i--;
}
}
hasEmptyRow = false;
}
return temp;
}
public bool FitFigureToHole(char[,] hole, char[,] figure)
{
if (hole.GetLength(0) >= figure.GetLength(0) && hole.GetLength(1) >= figure.GetLength(1))
{
for (int j = 0; j < hole.GetLength(0) - figure.GetLength(0); j++)
{
for (int i = 0; i < hole.GetLength(1) - figure.GetLength(1); i++)
{
if (CompareMatrix(hole, figure, j, i))
return true;
}
}
}
return false;
}
private bool CompareMatrix(char[,] hole, char[,] figure, int startx, int starty)
{
for (int j = 0; j < figure.GetLength(0); j++)
{
for (int i = 0; i < figure.GetLength(1); i++)
{
if ((figure[j, i] != emptyChar) && (hole[startx + j, starty + i] != emptyChar))
{
return false;
}
}
}
return true;
}
private char[,] RemoveRow(char[,] c, int deleteRow)
{
char[,] temp = new char[c.GetLength(0) - 1, c.GetLength(1)];
int index = 0;
for (int i = 0; i < c.GetLength(0); i++)
{
if (i == deleteRow)
{
continue;
}
else
{
for (int j = 0; j < c.GetLength(1); j++)
{
temp[index, j] = c[i, j];
}
index++;
}
}
return temp;
}
}
}