-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
258 lines (217 loc) · 13.3 KB
/
Copy pathProgram.cs
File metadata and controls
258 lines (217 loc) · 13.3 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using System;
using System.Linq;
using System.Text;
using ASD;
using System.Collections.Generic;
namespace ASD
{
public abstract class Lab02TestCase : TestCase
{
protected readonly int n;
protected readonly int m;
protected readonly ((int di, int dj) step, int cost)[] moves;
private bool checkStrings;
private readonly bool expectedRes;
private readonly int expectedCost;
protected (bool returnedRes, int returnedCost, (int i, int j)[] returnedPath) result;
protected Lab02TestCase(int n, int m, ((int di, int dj) step, int cost)[] moves, bool expectedRes, int expectedCost, bool checkStrings, int timeLimit, string description) : base(timeLimit, null, description)
{
this.n = n;
this.m = m;
this.moves = moves;
this.expectedRes = expectedRes;
this.expectedCost = expectedCost;
this.checkStrings = checkStrings;
}
protected override (Result resultCode, string message) VerifyTestCase(object settings)
{
var (code, msg) = checkSolution();
return (code, $"{msg} [{this.Description}]");
}
protected (Result resultCode, string message) checkSolution()
{
// sprawdzenie czy odpowiedź jest poprawna
if (result.returnedRes != expectedRes)
return (Result.WrongResult, $"Zwrócono ({result.returnedRes}), powinno być ({expectedRes})");
// jak nie ma trasy to nic już nie sprawdzamy
if (!result.returnedRes)
return OkResult("OK");
if (result.returnedCost != expectedCost)
return (Result.WrongResult, $"Zwrócono za duży koszt, zwrócono ({result.returnedCost}), powinno być ({expectedCost})");
if (result.returnedPath == null)
return (Result.WrongResult, $"Odpowiedź poprawna, ale zwrócono null zamiast trasy");
// odkomentować, żeby wypisywać zwracaną ścieżkę
//int p = 0;
//for (; p < result.returnedPath.Length - 1; ++p)
//{
// Console.Write($"({result.returnedPath[p].i}, {result.returnedPath[p].j}) -> ");
//}
//Console.WriteLine($"({result.returnedPath[p].i}, {result.returnedPath[p].j})");
// odkomentować, żeby wypisywać kroki
//int s = 0;
//for (; s < result.returnedPath.Length - 1; ++s)
//{
// Console.Write($"({result.returnedPath[s + 1].i - result.returnedPath[s].i}, {result.returnedPath[s + 1].j - result.returnedPath[s].j}) ");
//}
// sprawdzenie czy podana trasa prowadzi do ostatniego wiersza i nie wychodzi poza planszę
int i;
(int i, int j) pos = (0, 0);
if (result.returnedPath[0].i != 0 || result.returnedPath[0].j != 0)
return (Result.WrongResult, $"Odpowiedź poprawna, ale zwrócona ścieżka nie zaczyna się w polu (0,0)");
for (i = 1; i < result.returnedPath.Length; ++i)
{
pos = result.returnedPath[i];
if (pos.i < 0 || pos.i >= n || pos.j < 0 || pos.j >= m)
return (Result.WrongResult, $"Odpowiedź poprawna, ale zwrócona ścieżka wychodzi poza planszę");
}
if (pos.i != n - 1)
return (Result.WrongResult, $"Odpowiedź poprawna, ale zwrócona ścieżka nie kończy się w ostatnim wierszu");
return CheckMoves();
}
// metoda sprawdzająca poprawność ruchów
protected abstract (Result resultCode, string message) CheckMoves();
public (Result resultCode, string message) OkResult(string message) => (TimeLimit < PerformanceTime ? Result.LowEfficiency : Result.Success, $"{message} {PerformanceTime.ToString("#0.00")}s");
}
public class Stage1TestCase : Lab02TestCase
{
public Stage1TestCase(int n, int m, ((int, int), int)[] moves, bool expectedRes, int expectedCost, bool checkStrings, int timeLimit, string description) : base(n, m, moves, expectedRes, expectedCost, checkStrings, timeLimit, description)
{ }
protected override void PerformTestCase(object prototypeObject)
{
result = ((Lab02)prototypeObject).Lab02Stage1(n, m, moves);
}
protected override (Result resultCode, string message) CheckMoves()
{
(int i, int j) pos = (0, 0);
for (int i = 1; i < result.returnedPath.Length; ++i)
{
(int di, int dj) move = (result.returnedPath[i].i - pos.i, result.returnedPath[i].j - pos.j);
pos = result.returnedPath[i];
bool ok = false;
int k;
for (k = 0; k < moves.Length; ++k)
if (moves[k].step.di == move.di && moves[k].step.dj == move.dj)
{
ok = true;
break;
}
if (!ok)
return (Result.WrongResult, $"Odpowiedź poprawna, ale zwrócona ścieżka zawiera niedopuszczalny krok {move}");
}
return OkResult("OK");
}
}
public class Stage2TestCase : Lab02TestCase
{
public Stage2TestCase(int n, int m, ((int, int), int)[] moves, bool expectedRes, int expectedCost, bool checkStrings, int timeLimit, string description) : base(n, m, moves, expectedRes, expectedCost, checkStrings, timeLimit, description)
{ }
protected override void PerformTestCase(object prototypeObject)
{
result = ((Lab02)prototypeObject).Lab02Stage2(n, m, moves);
}
protected override (Result resultCode, string message) CheckMoves()
{
(int i, int j) pos = (0, 0);
int[] counterList = new int[moves.Length];
for (int i = 1; i < result.returnedPath.Length; ++i)
{
(int di, int dj) move = (result.returnedPath[i].i - pos.i, result.returnedPath[i].j - pos.j);
pos = result.returnedPath[i];
int k;
bool ok = false;
for (k = 0; k < moves.Length; ++k)
if (moves[k].step.di == move.di && moves[k].step.dj == move.dj)
{
ok = true;
++counterList[k];
break;
}
if (!ok)
return (Result.WrongResult, $"Odpowiedź poprawna, ale zwrócona ścieżka zawiera niedopuszczalny krok {move}");
}
for (int i = 0; i < counterList.Length; ++i)
if (counterList[i] > 1)
return (Result.WrongResult, $"Odpowiedź poprawna, ale krok {moves[i].step} występuje więcej niż raz na zwróconej ścieżce");
return OkResult("OK");
}
}
public class Lab02Tests : TestModule
{
TestSet Stage1 = new TestSet(prototypeObject: new Lab02(), description: "Etap 1", settings: true);
TestSet Stage2 = new TestSet(prototypeObject: new Lab02(), description: "Etap 2", settings: true);
public override void PrepareTestSets()
{
TestSets["Stage1"] = Stage1;
TestSets["Stage2"] = Stage2;
prepare();
}
private void addStage1(Stage1TestCase s1TestCase)
{
Stage1.TestCases.Add(s1TestCase);
}
private void addStage2(Stage2TestCase s2TestCase)
{
Stage2.TestCases.Add(s2TestCase);
}
private void prepare()
{
Random rand = new Random(1500190);
((int, int), int)[] moves = new ((int, int), int)[] { ((1, 1), 4), ((2, 2), 6), ((2, 1), 5), ((1, 0), 3), ((0, 1), 3) };
// Przykład z zadania
addStage1(new Stage1TestCase(n: 8, m: 6, moves: moves, expectedRes: true, expectedCost: 18, timeLimit: 1, description: "TRASA ISTNIEJE: przykład z zadania", checkStrings: false));
addStage2(new Stage2TestCase(n: 8, m: 6, moves: moves, expectedRes: false, expectedCost: int.MaxValue, timeLimit: 1, description: "TRASA NIE ISTNIEJE: przykład z zadania", checkStrings: false));
// Mała kwadratowa plansza
moves = new ((int, int), int)[] { ((4, 5), 4), ((1, 3), 1), ((3, 2), 2), ((4, 6), 10), ((2, 0), 3) };
addStage1(new Stage1TestCase(n: 10, m: 10, moves: moves, expectedRes: true, expectedCost: 6, timeLimit: 1, description: "TRASA ISTNIEJE: mała kwadratowa plansza", checkStrings: false));
addStage2(new Stage2TestCase(n: 10, m: 10, moves: moves, expectedRes: true, expectedCost: 9, timeLimit: 1, description: "TRASA ISTNIEJE: mała kwadratowa plansza", checkStrings: false));
// Mała wąska plansza
moves = new ((int, int), int)[] { ((3, 1), 4), ((4, 1), 5), ((2, 0), 4), ((1, 0), 2) };
addStage1(new Stage1TestCase(n: 10, m: 3, moves: moves, expectedRes: true, expectedCost: 12, timeLimit: 1, description: "TRASA ISTNIEJE: mała wąska plansza", checkStrings: false));
addStage2(new Stage2TestCase(n: 10, m: 3, moves: moves, expectedRes: true, expectedCost: 13, timeLimit: 1, description: "TRASA ISTNIEJE: mała wąska plansza", checkStrings: false));
moves = new ((int, int), int)[] { ((3, 1), 4), ((4, 1), 5), ((2, 1), 4), ((1, 1), 2) };
addStage1(new Stage1TestCase(n: 10, m: 3, moves: moves, expectedRes: false, expectedCost: int.MaxValue, timeLimit: 1, description: "TRASA NIE ISTNIEJE: mała wąska plansza", checkStrings: false));
addStage2(new Stage2TestCase(n: 10, m: 3, moves: moves, expectedRes: false, expectedCost: int.MaxValue, timeLimit: 1, description: "TRASA NIE ISTNIEJE: mała wąska plansza", checkStrings: false));
// Duża kwadratowa plansza
moves = new ((int, int), int)[] { ((170, 200), 140), ((170, 300), 140), ((50, 50), 42), ((70, 30), 58), ((1, 0), 1), ((200, 10), 210), ((100, 1), 100), ((8, 0), 7), ((70, 70), 60), ((90, 90), 80) };
addStage1(new Stage1TestCase(n: 1000, m: 1000, moves: moves, expectedRes: true, expectedCost: 826, timeLimit: 1, description: "TRASA ISTNIEJE: duża kwadratowa plansza", checkStrings: false));
addStage2(new Stage2TestCase(n: 1000, m: 1000, moves: moves, expectedRes: false, expectedCost: int.MaxValue, timeLimit: 5, description: "TRASA NIE ISTNIEJE: duża kwadratowa plansza", checkStrings: false));
moves = new ((int, int), int)[] { ((170, 200), 140), ((170, 300), 150), ((50, 50), 42), ((50, 150), 47), ((70, 30), 58), ((70, 90), 62), ((1, 0), 1), ((200, 10), 210), ((100, 10), 100), ((100, 100), 120), ((8, 0), 7), ((70, 70), 60), ((190, 90), 180) };
addStage1(new Stage1TestCase(n: 1000, m: 1000, moves: moves, expectedRes: true, expectedCost: 826, timeLimit: 1, description: "TRASA ISTNIEJE: duża kwadratowa plansza", checkStrings: false));
addStage2(new Stage2TestCase(n: 1000, m: 1000, moves: moves, expectedRes: true, expectedCost: 910, timeLimit: 10, description: "TRASA ISTNIEJE: duża kwadratowa plansza", checkStrings: false));
// Testy losowe
moves = RandomMoves(1000, 1000, rand);
addStage1(new Stage1TestCase(n: 1000, m: 1000, moves: moves, expectedRes: true, expectedCost: 512, timeLimit: 10, description: "TRASA ISTNIEJE: test losowy, plansza 1000x1000", checkStrings: false));
addStage2(new Stage2TestCase(n: 1000, m: 1000, moves: moves, expectedRes: true, expectedCost: 762, timeLimit: 40, description: "TRASA ISTNIEJE: test losowy, plansza 1000x1000", checkStrings: false));
moves = RandomMoves(300, 3000, rand);
addStage1(new Stage1TestCase(n: 300, m: 3000, moves: moves, expectedRes: true, expectedCost: 185, timeLimit: 1, description: "TRASA ISTNIEJE: test losowy, plansza 300x3000", checkStrings: false));
addStage2(new Stage2TestCase(n: 300, m: 3000, moves: moves, expectedRes: false, expectedCost: int.MaxValue, timeLimit: 5, description: "TRASA NIE ISTNIEJE: test losowy, plansza 300x3000", checkStrings: false));
moves = RandomMoves(100, 10, rand);
addStage1(new Stage1TestCase(n: 100, m: 10, moves: moves, expectedRes: false, expectedCost: int.MaxValue, timeLimit: 1, description: "TRASA NIE ISTNIEJE: test losowy, plansza 100x10", checkStrings: false));
addStage2(new Stage2TestCase(n: 100, m: 10, moves: moves, expectedRes: false, expectedCost: int.MaxValue, timeLimit: 1, description: "TRASA NIE ISTNIEJE: test losowy, plansza 100x10", checkStrings: false));
}
private ((int, int), int)[] RandomMoves(int n, int m, Random rand)
{
((int, int), int)[] moves = new ((int, int), int)[rand.Next((int)n / 100, (int)n / 25)];
for (int i = 0; i < moves.Length; ++i)
{
(int di, int dj) step = (rand.Next(1, (int)n / 10), rand.Next(1, (int)n / 10));
int cost = rand.Next(Math.Max((int)step.di / 2, 1), step.di * 2);
moves[i] = (step, cost);
}
return moves;
}
}
class Program
{
static void Main(string[] args)
{
var tests = new Lab02Tests();
tests.PrepareTestSets();
foreach (var ts in tests.TestSets)
{
ts.Value.PerformTests(verbose: true, checkTimeLimit: false);
}
}
}
}