This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
using namespace std;
typedef long long ll;
const ll oo = 1e9;
const ll N = 1e3;
vector <pair<vector <char>, bool> > ans;
bool mrk[N][N];
string s[N];
int n, m;
int sx, sy;
bool good(int x, int y)
{
return (x >= 0 && y >= 0 && x < n && y < m);
}
void Rec(int x, int y)
{
if (good(x, y) && mrk[x][y] || s[x][y] == '.' || s[x][y] == 'o')
{
ans.back().s = 0;
return;
}
if (s[x][y] == 'x') return;
mrk[x][y] = 1;
if (s[x][y] == '^') x--;
else
if (s[x][y] == 'v') x++;
else
if (s[x][y] == '<') y--;
else
if (s[x][y] == '>') y++;
ans.back().f.pb('v');
Rec(x, y);
}
void clear()
{
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) mrk[i][j] = 0;
}
void Check()
{
ans.pb({{}, 1});
ans.back().f.pb('N');
clear();
Rec(sx - 1, sy);
ans.pb({{}, 1});
ans.back().f.pb('S');
clear();
Rec(sx + 1, sy);
ans.pb({{}, 1});
ans.back().f.pb('E');
clear();
Rec(sx, sy + 1);
ans.pb({{}, 1});
ans.back().f.pb('W');
clear();
Rec(sx, sy - 1);
}
int main() {
ios_base::sync_with_stdio(0);
iostream::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> s[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (s[i][j] == 'o') sx = i, sy = j;
Check();
bool g = 0;
for (int i = 0; i < ans.size(); i++) g = g || ans[i].s;
if (!g) cout << ":(";
else
{
cout << ":)\n";
char ch = 'Z';
for (int i = 0; i < ans.size(); i++)
if (ans[i].s) ch = min(ch, ans[i].f[0]);
cout << ch;
}
}
Compilation message (stderr)
patkice.cpp: In function 'void Rec(int, int)':
patkice.cpp:27:20: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
27 | if (good(x, y) && mrk[x][y] || s[x][y] == '.' || s[x][y] == 'o')
| ~~~~~~~~~~~^~~~~~~~~~~~
patkice.cpp: In function 'int main()':
patkice.cpp:83:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::vector<char>, bool> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for (int i = 0; i < ans.size(); i++) g = g || ans[i].s;
| ~~^~~~~~~~~~~~
patkice.cpp:89:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::vector<char>, bool> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (int i = 0; i < ans.size(); i++)
| ~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |