-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
131 lines (116 loc) · 4.25 KB
/
Program.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
129
130
131
using System;
using System.Linq;
namespace _11.ParkingSystem
{
class Program
{
private static int[][] matrix;
private static int rows;
static void Main()
{
CreateMatrix();
ParkCars();
}
static void ParkCars()
{
string input = Console.ReadLine();
while (!input.Equals("stop"))
{
int[] tokens = input
.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse).ToArray();
int entryRow = tokens[0];
int targetRow = tokens[1];
int targetColumn = tokens[2];
int rowCounter = Math.Abs(targetRow - entryRow) + 1;
int columnCounter = targetColumn;
int moves = 0;
if (matrix[targetRow] == null)
{
matrix[targetRow] = new int[rows];
}
int colLength = matrix[targetRow].Length;
if (matrix[targetRow][targetColumn] == 1)
{
int nextLeftIndex = FindNextLeft(targetRow, targetColumn);
int nextRightIndex = FindNextRight(targetRow, targetColumn, colLength);
int leftDiff = targetColumn - nextLeftIndex;
int rightDiff = nextRightIndex - targetColumn;
if (leftDiff == rightDiff && nextLeftIndex > 0)
{
matrix[targetRow][nextLeftIndex] = 1;
columnCounter = nextLeftIndex;
}
else if (leftDiff == rightDiff && nextRightIndex > 0)
{
matrix[targetRow][nextRightIndex] = 1;
columnCounter = nextRightIndex;
}
else if (nextLeftIndex > 0 && nextRightIndex == -1)
{
matrix[targetRow][nextLeftIndex] = 1;
columnCounter = nextLeftIndex;
}
else if (nextLeftIndex == -1 && nextRightIndex > 0)
{
matrix[targetRow][nextRightIndex] = 1;
columnCounter = nextRightIndex;
}
else if (leftDiff > rightDiff && nextRightIndex > 0)
{
matrix[targetRow][nextRightIndex] = 1;
columnCounter = nextRightIndex;
}
else if (leftDiff < rightDiff && nextLeftIndex > 0)
{
matrix[targetRow][nextLeftIndex] = 1;
columnCounter = nextLeftIndex;
}
else
{
Console.WriteLine($"Row {targetRow} full");
input = Console.ReadLine();
continue;
}
}
else
{
matrix[targetRow][targetColumn] = 1;
}
moves = rowCounter + columnCounter;
Console.WriteLine(moves);
input = Console.ReadLine();
}
}
static int FindNextRight(int targetRow, int targetColumn, int columnLength)
{
for (int col = targetColumn + 1; col < columnLength; col++)
{
if (matrix[targetRow][col] == 0)
{
return col;
}
}
return -1;
}
static int FindNextLeft(int targetRow, int targetColumn)
{
for (int col = targetColumn - 1; col >= 1; col--)
{
if (matrix[targetRow][col] == 0)
{
return col;
}
}
return -1;
}
static void CreateMatrix()
{
int[] dimensions = Console.ReadLine()
.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse).ToArray();
rows = dimensions[1];
matrix = new int[dimensions[0]][];
}
}
}