-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfsBacktracking
48 lines (41 loc) · 1013 Bytes
/
dfsBacktracking
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
//Timing 03:25:57
9 8 1 1
........
.#######
.#......
.#......
.#.S...S
.###.#.#
.#...#.#
.#.###.#
........
#include <bits/stdc++.h>
#define MAX 10000
using namespace std;
vector< vector< char > > ad(MAX, vector<char> (MAX));
vector< vector< bool > > v(MAX, vector<bool> (MAX, false));
pair<int,int> ady;
int dir[2][4] = { {1,-1,0,0},{0,0,1,-1} };
int n,m,x,y;
void dfs(pair<int,int> act){
v[act.first][act.second]=true;
if(ad[act.first][act.second] == 'S')
cout << 'S' << endl;
for(int i=0;i<4;i++){
ady.first = act.first + dir[0][i];
ady.second = act.second + dir[1][i];
if( (ady.first > 0 && ady.first <= n) && (ady.second > 0 && ady.second <= m) )
if(!v[ady.first][ady.second] && ad[ady.first][ady.second] != '#')
dfs(ady);
}
}
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 >> ad[i][j];
}
}
dfs(make_pair(x,y));
}