-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDIEHARD.cs
165 lines (154 loc) · 5.78 KB
/
DIEHARD.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
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
using System;
namespace AlgoSolution
{
struct M
{
public int Health;
public int Armour;
public string current;
}
class DIEHARD
{
public static void Main()
{
Run();
}
static void Run()
{
int t = int.Parse(Console.ReadLine());
while (t > 0)
{
string[] str = Console.ReadLine().Split();
int health = int.Parse(str[0]);
int armour = int.Parse(str[1]);
M[] r = new M[3];
for (int i = 0; i < 3; i++)
{
r[i].Armour = armour;
r[i].Health = health;
}
r[0].current = "air";
r[1].current = "water";
r[2].current = "fire";
int totalTime = 0;
M air = new M();
air.Health = 3;
air.Armour = 2;
air.current = "air";
M water = new M();
water.Armour = -10;
water.Health = -5;
water.current = "water";
M fire = new M();
fire.Health = -20;
fire.Armour = 5;
fire.current = "fire";
while (true)
{
int missingCounter = 3;
if (r[0].Armour > 0 && r[0].Health > 0)
{
D(ref air, ref water, ref fire, ref r[0]);
}
else
{
missingCounter--;
}
if (r[1].Armour > 0 && r[1].Health > 0)
{
M myCurrentPlace = r[1];
D(ref air, ref water, ref fire, ref r[1]);
}
else
{
missingCounter--;
}
if (r[2].Armour > 0 && r[2].Health > 0)
{
M myCurrentPlace = r[2];
D(ref air, ref water, ref fire, ref r[2]);
}
else
{
missingCounter--;
}
if (missingCounter == 0)
break;
totalTime++;
}
t--;
Console.WriteLine(totalTime - 1);
}
Console.ReadLine();
}
private static void D(ref M air, ref M water, ref M fire, ref M myCurrentPlace)
{
if (myCurrentPlace.current == "air")
{
//We can go either in fire or water
int healthAfterFire = myCurrentPlace.Health + fire.Health;
int healthAfterWater = myCurrentPlace.Health + water.Health;
int armourAfterFire = myCurrentPlace.Armour + fire.Armour;
int armourAfterWater = myCurrentPlace.Armour + water.Armour;
int waterMin = Math.Min(healthAfterWater, armourAfterWater);
int fireMin = Math.Min(healthAfterFire, armourAfterFire);
if (waterMin >= fireMin)
{
myCurrentPlace.current = "water";
myCurrentPlace.Armour = armourAfterWater;
myCurrentPlace.Health = healthAfterWater;
}
else
{
myCurrentPlace.current = "fire";
myCurrentPlace.Armour = armourAfterFire;
myCurrentPlace.Health = healthAfterFire;
}
}
else if (myCurrentPlace.current == "water")
{
//We can go either in fire or water
int healthAfterFire = myCurrentPlace.Health + fire.Health;
int healthAfterair = myCurrentPlace.Health + air.Health;
int armourAfterFire = myCurrentPlace.Armour + fire.Armour;
int armourAfterair = myCurrentPlace.Armour + air.Armour;
int waterMin = Math.Min(healthAfterair, armourAfterair);
int fireMin = Math.Min(healthAfterFire, armourAfterFire);
if (waterMin >= fireMin)
{
myCurrentPlace.current = "air";
myCurrentPlace.Armour = armourAfterair;
myCurrentPlace.Health = healthAfterair;
}
else
{
myCurrentPlace.current = "fire";
myCurrentPlace.Armour = armourAfterFire;
myCurrentPlace.Health = healthAfterFire;
}
}
else if (myCurrentPlace.current == "fire")
{
//We can go either in fire or water
int healthAfterwater = myCurrentPlace.Health + water.Health;
int healthAfterair = myCurrentPlace.Health + air.Health;
int armourAfterwater = myCurrentPlace.Armour + water.Armour;
int armourAfterair = myCurrentPlace.Armour + air.Armour;
int waterMin = Math.Min(healthAfterair, armourAfterair);
int fireMin = Math.Min(healthAfterwater, armourAfterwater);
if (waterMin >= fireMin)
{
myCurrentPlace.current = "air";
myCurrentPlace.Armour = armourAfterair;
myCurrentPlace.Health = healthAfterair;
}
else
{
myCurrentPlace.current = "water";
myCurrentPlace.Armour = armourAfterwater;
myCurrentPlace.Health = healthAfterwater;
}
}
}
}
}