-
Notifications
You must be signed in to change notification settings - Fork 0
/
mecung.cpp
65 lines (59 loc) · 2 KB
/
mecung.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
#include <bits/stdc++.h>
using namespace std;
const int di[] = {0, 0, 1, -1};
const int dj[] = {1, -1, 0, 0};
const char ds[] = {'E', 'W', 'S', 'N'};
int a[100][100], m, n;
pair <int, int> goback[100][100], from;
char waygo[100][100];
string temp, go = " ";
void FindWay(pair <int, int> k){
temp = "";
while (goback[k.first][k.second].first != 0 && goback[k.first][k.second].second != 0){
temp = waygo[k.first][k.second] + temp;
k = goback[k.first][k.second];
}
if (temp.length() < go.length()) go = temp;
}
void bfs(pair <int, int> k){
vector <pair <int, int> > q;
int dem = 0;
q.push_back(k);
while (!q.empty()){
bool tru = true;
++dem;
for (int i = 0; i < 4; ++i){
if (a[q[0].first+di[i]][q[0].second+dj[i]] == 0){
tru = false;
a[q[0].first+di[i]][q[0].second+dj[i]] = dem;
q.push_back(make_pair(q[0].first+di[i], q[0].second+dj[i]));
goback[q[0].first+di[i]][q[0].second+dj[i]] = make_pair(q[0].first, q[0].second);
waygo[q[0].first+di[i]][q[0].second+dj[i]] = ds[i];
if (q[0].first+di[i] == 1 || q[0].first+di[i] == m || q[0].second+dj[i] == 1 || q[0].second+dj[i] == n){
FindWay(make_pair(q[0].first+di[i], q[0].second+dj[i]));
}
}
}
if (tru == true) --dem;
q.erase(q.begin());
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("mecung.inp", "r", stdin);
freopen("mecung.out", "w", stdout);
#endif // ONLINE_JUDGE
memset(a, -1, sizeof a);
scanf("%d%d", &m, &n);
for (int i = 1; i <= m; ++i){
for (int j = 1; j <= n; ++j){
char temp;
cin >> temp;
if (temp == 'O') a[i][j] = 0;
if (temp == 'E') from = make_pair(i, j);
}
}
bfs(from);
cout << go;
return 0;
}