//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define pob pop_back
#define int long long
#define int2 __int128_t
#define Shrek_Crush228 ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define F first
#define S second
#define sz size()
#define FOR( i, x, n, d ) for( int i = x; i <= n; i += d )
#define FORR( i, x, n, d ) for( int i = x; i >= n; i -= d )
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define no cout << "NO\n"
#define yes cout << "YES\n"
#define nep next_permutation
#define sigma signed
#define pii pair <int, int>
using namespace std;
string alp = "abcdefghijklmnopqrstuvwxyz";
string ae = "aoeiuy";
string nums = "123456789";
const int N = 3e5 + 123;
const int inf = 1e18;
const int MOD = 1e9 + 7;
const int MOD1 = 998244353;
int a[N], b[N], c[N], d[N], l, r, n, k, m, w, cur, cnt, res, sum, sum1, x, y, ans, ans1, mx = -inf, mn = inf, ind, ind1, pref[N], suf[N];
string s, s1;
struct RT
{
int sum, l, r, ind;
};
int gcd( int a, int b )
{
return ( b ? gcd( b, a % b ) : a );
}
int lcm( int a, int b )
{
return a / gcd( a, b ) * b;
}
int bp( int a, int b, int m )
{
if( !b ) return 1;
if( b & 1 ) return bp( a, b - 1, m ) * a % m;
int it = bp( a, b >> 1, m );
return it * it % m;
}
struct po
{
int v1, v2, v3;
};
char maze[505][505];
int used[505][505];
pii to_left( int x, int y )
{
if( maze[x][y - 1] == 'x' || y - 1 < 1 ) return { x, y };
return to_left( x, y - 1 );
}
pii to_right( int x, int y )
{
if( maze[x][y + 1] == 'x' || y + 1 > m ) return { x, y };
return to_right( x, y + 1 );
}
pii to_up( int x, int y )
{
if( maze[x - 1][y] == 'x' || x - 1 < 1 ) return { x, y };
return to_up( x - 1, y );
}
pii to_down( int x, int y )
{
if( maze[x + 1][y] == 'x' || x + 1 > n ) return { x, y };
return to_down( x + 1, y );
}
void get( int xn, int yn, int xst, int yst, int dist )
{
if( used[xn][yn] < dist ) return;
used[xn][yn] = dist;
int xl, yl, xr, yr, xu, yu, xd, yd, okl = 0, okr = 0, oku = 0, okd = 0;
tie( xl, yl ) = to_left( xn, yn );
tie( xr, yr ) = to_right( xn, yn );
tie( xu, yu ) = to_up( xn, yn );
tie( xd, yd ) = to_down( xn, yn );
if( xst == xn && yl <= yst && yst <= yn ) ans = min( ans, dist + 1 ), okl = 1;
else get( xl, yl, xst, yst, dist + 1 );
if( xst == xn && yn <= yst && yst <= yr ) ans = min( ans, dist + 1 ), okr = 1;
else get( xr, yr, xst, yst, dist + 1 );
if( yst == yn && xu <= xst && xst <= xn ) ans = min( ans, dist + 1 ), oku = 1;
else get( xu, yu, xst, yst, dist + 1 );
if( yst == yn && xn <= xst && xst <= xd ) ans = min( ans, dist + 1 ), okd = 1;
else get( xd, yd, xst, yst, dist + 1 );
// if( okl && dist + 1 == 2 ) cout << 'l' << xn << ' ' << yn << ' ' << xst << ' ' << yst << '\n';
// if( okr && dist + 1 == 2 ) cout << 'r' << xn << ' ' << yn << ' ' << xst << ' ' << yst << '\n';
// if( oku && dist + 1 == 2 ) cout << 'u' << xn << ' ' << yn << ' ' << xst << ' ' << yst << '\n';
// if( okd && dist + 1 == 2 ) cout << 'd' << xn << ' ' << yn << ' ' << xst << ' ' << yst << '\n';
}
void solve()
{
cin >> k >> n >> m;
FOR( i, 1, n, 1 )
{
FOR( j, 1, m, 1 )
{
cin >> maze[i][j], used[i][j] = inf;
if( maze[i][j] == 'C' || maze[i][j] == 'A' ) maze[i][j] = '.';
}
}
int x1, y1, x2, y2;
FOR( i, 1, n, 1 )
{
FOR( j, 1, m, 1 )
{
if( maze[i][j] == '1' ) x1 = i, y1 = j;
if( maze[i][j] == '2' ) x2 = i, y2 = j;
}
}
ans = 1e9;
pii L = to_left( x2, y2 ), R = to_right( x2, y2 ), U = to_up( x2, y2 ), D = to_down( x2, y2 );
get( x1, y1, L.F, L.S, 1 ), get( x1, y1, R.F, R.S, 1 ), get( x1, y1, U.F, U.S, 1 ), get( x1, y1, D.F, D.S, 1 );
get( x1, y1, x2, y2, 0 );
//cout << to_down( 1, 5 ).F << ' ' << to_down( 1, 5 ).S << '\n';
cout << ( ans == 1e9 ? -1 : ans );
}
sigma main()
{
//freopen("haircut.in", "r", stdin);
//freopen("haircut.out", "w", stdout);
Shrek_Crush228
int test = 1;
if( !test ) cin >> test;
while( test -- )
{
solve();
}
}
//// solved by KluydQ
/*
2 4 5
...1.
.xxxx
....2
xxxxx
*/
Compilation message
robots.cpp: In function 'void get(long long int, long long int, long long int, long long int, long long int)':
robots.cpp:94:38: warning: variable 'okl' set but not used [-Wunused-but-set-variable]
94 | int xl, yl, xr, yr, xu, yu, xd, yd, okl = 0, okr = 0, oku = 0, okd = 0;
| ^~~
robots.cpp:94:47: warning: variable 'okr' set but not used [-Wunused-but-set-variable]
94 | int xl, yl, xr, yr, xu, yu, xd, yd, okl = 0, okr = 0, oku = 0, okd = 0;
| ^~~
robots.cpp:94:56: warning: variable 'oku' set but not used [-Wunused-but-set-variable]
94 | int xl, yl, xr, yr, xu, yu, xd, yd, okl = 0, okr = 0, oku = 0, okd = 0;
| ^~~
robots.cpp:94:65: warning: variable 'okd' set but not used [-Wunused-but-set-variable]
94 | int xl, yl, xr, yr, xu, yu, xd, yd, okl = 0, okr = 0, oku = 0, okd = 0;
| ^~~
robots.cpp: In function 'void solve()':
robots.cpp:128:18: warning: 'y2' may be used uninitialized in this function [-Wmaybe-uninitialized]
128 | int x1, y1, x2, y2;
| ^~
robots.cpp:128:14: warning: 'x2' may be used uninitialized in this function [-Wmaybe-uninitialized]
128 | int x1, y1, x2, y2;
| ^~
robots.cpp:128:10: warning: 'y1' may be used uninitialized in this function [-Wmaybe-uninitialized]
128 | int x1, y1, x2, y2;
| ^~
robots.cpp:128:6: warning: 'x1' may be used uninitialized in this function [-Wmaybe-uninitialized]
128 | int x1, y1, x2, y2;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |