forked from Diusrex/UVA-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10452 Marcus.cpp
70 lines (59 loc) · 1.42 KB
/
10452 Marcus.cpp
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
#include <iostream>
#include <string>
using namespace std;
int X, Y;
// Is, like usual [y][x]
string grid[9];
const int NumMoves = 3;
const int xMoves[] = {-1, 0, 1};
const int yMoves[] = {0, -1, 0};
const string moveNames[] = {"left", "forth", "right"};
string output[7];
const string Needed = "IEHOVA#";
bool GetToIt(int x, int y, int pos)
{
if (grid[y][x] == '#')
{
cout << output[0];
for (int i = 1; i < pos; ++i)
cout << ' ' << output[i];
cout << '\n';
return true;
}
else if (pos == Needed.size())
return false;
for (int move = 0; move < NumMoves; ++move)
{
int newX = x + xMoves[move], newY = y + yMoves[move];
if (newX >= 0 && newX < X && newY >= 0 && newY < Y
&& grid[newY][newX] == Needed[pos])
{
// Try moving there
output[pos] = moveNames[move];
if (GetToIt(newX, newY, pos + 1))
return true;
}
}
return false;
}
int main()
{
int T;
cin >> T;
while (T--)
{
cin >> Y >> X;
int hisX, hisY;
for (int y = 0; y < Y; ++y)
{
cin >> grid[y];
for (int x = 0; x < X; ++x)
if (grid[y][x] == '@')
{
hisX = x;
hisY = y;
}
}
GetToIt(hisX, hisY, 0);
}
}