-
Notifications
You must be signed in to change notification settings - Fork 9
/
10452-Marcus.cpp
54 lines (49 loc) · 1.21 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
#include <bits/stdc++.h>
using namespace std;
string path = "IEHOVA#";
vector<vector<int>> mov = {{0,-1},{-1,0},{0,1}};
void print(vector<int>& dirs){
for(auto& dir : dirs){
if(dir == 0) cout << "left";
else if(dir == 1) cout << "forth";
else cout << "right";
if(&dir != &dirs.back()) cout << " ";
else cout << endl;
}
}
bool dfs(int r,int c,vector<string>& grid,int idx,vector<int>& dirs){
if(idx == path.length()){
print(dirs);
return true;
}
for(int i=0;i<3;i++){
int x = r+mov[i][0];
int y = c+mov[i][1];
if(x<0 || y<0 || y>=grid[0].length()) continue;
if(grid[x][y] == path[idx]){
dirs.push_back(i);
if(dfs(x,y,grid,idx+1,dirs)) return true;
dirs.pop_back();
}
}
return false;
}
int main()
{
int t;
int m,n;
string in;
cin >> t;
while(scanf("%d %d",&m,&n) != EOF){
vector<string> grid;
vector<int> dirs;
for(int i=0;i<m;i++){
cin >> in;
grid.push_back(in);
}
int c;
for(int i=0;i<n;i++)
if(grid.back()[i] == '@') c = i;
dfs(m-1,c,grid,0,dirs);
}
}