-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfsWithStackStructure
62 lines (54 loc) · 1.26 KB
/
dfsWithStackStructure
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
//----INPUT----
//9 8 1 1
//........
//.#######
//.#......
//.#......
//.#.S...S
//.###.#.#
//.#...#.#
//.#.###.#
//........
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000
int n,m,x,y;
char ady[MAX][MAX];
bool visitado[MAX][MAX];
int dir[2][4] = { {1,-1,0,0},
{0,0,-1,1} };
stack< pair<int,int> > S;
void dfs(){
S.push(make_pair(x,y));
pair<int,int> act, sig;
while(!S.empty()){
act = S.top();
S.pop();
if(ady[act.first][act.second] == 'S'){
cout << act.first << ' ' << act.second << endl;
return;
};
if(visitado[act.first][act.second]) continue;
visitado[act.first][act.second] = true;
for(int i=0;i<4;i++){
sig.first = act.first + dir[0][i];
sig.second = act.second + dir[1][i];
if( (sig.first > 0 && sig.first <= n) && (sig.second > 0 && sig.second <= m) ){
if(ady[sig.first][sig.second] != '#'){
S.push(sig);
}
}
}
}
}
int main(){
ifstream fin("in.txt");
fin >> n >> m >> x >> y;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
fin >> ady[i][j];
visitado[i][j]=false;
}
}
dfs();
}