/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int NM_MAX = 102;
int n, m;
char ma[NM_MAX][NM_MAX];
struct Cell
{
int a, b;
};
bool inside (Cell c)
{
return 1 <= c.a & c.a <= n && 1 <= c.b && c.b <= m;
}
char dir[] = {'N', 'E', 'S', 'W'};
int da[] = {-1, 0, 1, 0}, db[] = {0, 1, 0, -1};
int f (char c)
{
if(c == '^')
return 0;
else if(c == '>')
return 1;
else if(c == 'v')
return 2;
else if(c == '<')
return 3;
return -1;
}
Cell start, finish;
int dfs (Cell c)
{
if(ma[c.a][c.b] == 'x')
return 0;
if(f(ma[c.a][c.b]) == -1)
return -1;
int d = f(ma[c.a][c.b]);
Cell c1 = Cell{c.a + da[d], c.b + db[d]};
int len = dfs(c1);
if(len == -1)
return -1;
return len + 1;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
cin >> ma[i][j];
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
if(ma[i][j] == 'o')
start = Cell{i, j};
else if(ma[i][j] == 'x')
finish = Cell{i, j};
int minLen = INT_MAX;
char ans = 'X';
for(int d = 0; d < 4; d++)
{
Cell c = Cell{start.a + da[d], start.b + db[d]};
if(inside(c) == false)
continue;
int len = dfs(c);
if(len == -1)
continue;
if(len < minLen || (len == minLen && dir[d] < ans))
{
minLen = len;
ans = dir[d];
}
}
if(ans == 'X')
cout << ":(\n";
else
cout << ":)\n" << ans << "\n";
return 0;
}
Compilation message
patkice.cpp: In function 'bool inside(Cell)':
patkice.cpp:28:14: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
28 | return 1 <= c.a & c.a <= n && 1 <= c.b && c.b <= m;
| ~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |