This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#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 = 1e9;
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;
};
int dx[] = { 1, 0, -1, 0 }, dy[] = { 0, -1, 0, 1 };
char maze[505][505];
pii in[505][505][5], rob[11];
vector <pii> g[505][505];
int dp[10][10][505][505];
bool may( int x, int y )
{
if( x > n || x < 1 || y > m || y < 1 || maze[x][y] == 'x' ) return 0;
return 1;
}
pii adj( int x, int y, int tp )
{
if( in[x][y][tp].F ) return in[x][y][tp]; in[x][y][tp] = { -inf, -inf };
int copy = tp;
if( maze[x][y] == 'C' ) tp = ( tp + 1 ) % 4;
if( maze[x][y] == 'A' ) tp = ( tp + 3 ) % 4;
int xt = x + dx[tp], yt = y + dy[tp];
if( may( xt, yt )) return in[x][y][copy] = adj( xt, yt, tp );
if( !may( xt, yt )) return in[x][y][copy] = { x, y };
}
void solve()
{
cin >> k >> m >> n;
int x1, y1, x2, y2;
vector <pii> qu;
FOR( i, 1, n, 1 )
{
FOR( j, 1, m, 1 )
{
cin >> maze[i][j];
if( maze[i][j] >= '1' && maze[i][j] <= '9' ) rob[maze[i][j] - '0'] = { i, j };
}
}
FOR( i, 1, n, 1 )
{
FOR( j, 1, m, 1 )
{
FOR( tp, 0, 3, 1 )
{
if( !may( i, j ) ) continue;
pii brat = adj( i, j, tp );
if( brat.F != -inf ) g[i][j].pb( brat );
}
}
}
FOR( l, 1, k, 1 )
{
FOR( r, l, k, 1 )
{
FOR( i, 1, n, 1 )
{
FOR( j, 1, m, 1 ) dp[l][r][i][j] = inf;
}
}
}
FOR( id, 1, k, 1 ) dp[id][id][rob[id].F][rob[id].S] = 0;
FOR( otrezok, 1, k, 1 )
{
FOR( l, 1, k, 1 )
{
if( otrezok + l - 1 > k ) break;
// r - l + 1 = otrezok
r = otrezok + l - 1;
FOR( i, 1, n, 1 )
{
FOR( j, 1, m, 1 )
{
FOR( md, l, r - 1, 1 ) dp[l][r][i][j] = min( dp[l][r][i][j], dp[l][md][i][j] + dp[md + 1][r][i][j] );
}
}
queue <pair <int, pii>> q;
FOR( i, 1, n, 1 )
{
FOR( j, 1, m, 1 )
{
if( dp[l][r][i][j] != inf ) q.push({ dp[l][r][i][j], { i, j }});
}
}
while( q.sz )
{
pair <int, pii> v = q.front();
q.pop();
if( dp[l][r][v.S.F][v.S.S] < v.F ) continue;
for( auto to : g[v.S.F][v.S.S] )
{
if( dp[l][r][to.F][to.S] > dp[l][r][v.S.F][v.S.S] + 1 )
{
dp[l][r][to.F][to.S] = dp[l][r][v.S.F][v.S.S] + 1;
q.push({ dp[l][r][to.F][to.S], to });
}
}
}
}
}
res = inf;
FOR( i, 1, n, 1 )
{
FOR( j, 1, m, 1 ) res = min( res, dp[1][k][i][j] );
}
if( res == inf ) res = -1;
cout << res << '\n';
}
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
/*
4 10 5
1.........
AA...x4...
..A..x....
2....x....
..C.3.A...
*/
Compilation message (stderr)
robots.cpp: In function 'std::pair<int, int> adj(int, int, int)':
robots.cpp:80:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
80 | if( in[x][y][tp].F ) return in[x][y][tp]; in[x][y][tp] = { -inf, -inf };
| ^~
robots.cpp:80:47: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
80 | if( in[x][y][tp].F ) return in[x][y][tp]; in[x][y][tp] = { -inf, -inf };
| ^~
robots.cpp: In function 'void solve()':
robots.cpp:95:6: warning: unused variable 'x1' [-Wunused-variable]
95 | int x1, y1, x2, y2;
| ^~
robots.cpp:95:10: warning: unused variable 'y1' [-Wunused-variable]
95 | int x1, y1, x2, y2;
| ^~
robots.cpp:95:14: warning: unused variable 'x2' [-Wunused-variable]
95 | int x1, y1, x2, y2;
| ^~
robots.cpp:95:18: warning: unused variable 'y2' [-Wunused-variable]
95 | int x1, y1, x2, y2;
| ^~
robots.cpp: In function 'std::pair<int, int> adj(int, int, int)':
robots.cpp:91:1: warning: control reaches end of non-void function [-Wreturn-type]
91 | }
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |