#include<bits/stdc++.h>
using namespace std;
#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 6,5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,_6,N,...) N
#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)
#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)
#define macro_dispatcher___(macro, nargs) macro ## nargs
#define Debug1(a) cout<<#a<<"="<<(a)<<"\n"
#define Debug2(a,b) cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define Debug3(a,b,c) cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define Debug4(a,b,c,d) cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define Debug5(a,b,c,d,e) cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define Debug6(a,b,c,d,e,f) cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define Debug(...) macro_dispatcher(Debug, __VA_ARGS__)(__VA_ARGS__)
#define DA(a,n) cout<<#a<<"=["; printarray(a,n); cout<<"]\n"
#define DAR(a,n,s) cout<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cout<<"]\n"
#define TT1 template<class T>
#define TT1T2 template<class T1, class T2>
#define TT1T2T3 template<class T1, class T2, class T3>
template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v);
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p){ return os <<"("<<p.first<<", "<< p.second<<")"; }
TT1 ostream& operator << (ostream& os, const vector<T>& v){ bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v) { bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v){ bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multiset<T1,T2>&v){bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const map<T1,T2,T3>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multimap<T1, T2>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v) { bool f = 1; os << "["; while (!v.empty()) { auto x = v.top(); v.pop(); if (!f) os << ", "; f = 0; os << x; } return os << "]"; }
TT1T2 void printarray(const T1& a, T2 sz, T2 beg = 0){ for (T2 i = beg; i<sz; i++) cout << a[i] << " "; cout << endl; }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define sz(x) (int)x.size()
#define rep(i, n) for(int i=0;i<n;i++)
#define repp(i, n) for(int i=1;i<=n;i++)
#define all(x) x.begin(), x.end()
#define geti1(X) cin >> X
#define geti2(X,Y) cin >> X >> Y
#define geti3(X,Y,Z) cin >> X >> Y >> Z
#define geti4(X,Y,Z,W) cin >> X >> Y >> Z >> W
#define GET_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define geti(...) GET_MACRO(__VA_ARGS__, geti4, geti3, geti2, geti1) (__VA_ARGS__)
#define INF 987654321
#define endl '\n'
struct Node{
ll c; int dir,y,x;
bool operator < (const Node N) const{
return c < N.c;
}
bool operator > (const Node N) const{
return c > N.c;
}
};
int N,M;
int xx[4] = {0,1,0,-1}, yy[4] = {-1,0,1,0};
bool inbound(int y, int x){
return x >= 1 && y >= 1 && x <= N && y <= N;
}
Pi nxt[4][1000][1000]; ll dist[4][1000][1000];
ll cost[4][1000][1000];
char s[1050][1050];
int sx,sy,ex,ey;
int main(){
scanf("%d",&N);
repp(i,N) scanf("%s",s[i]+1);
rep(d,4) repp(i,N) repp(j,N) cost[d][i][j] = 1e18;
repp(i,N) repp(j,N){
if( s[i][j] == 'M' ){
sx = j; sy = i;
s[i][j] = '.';
}
if( s[i][j] == 'H' ){
ex = j; ey = i;
s[i][j] = '.';
}
}
// fill 0
repp(j,N){
ll last = -1, cnt = 0;
for(int i=1;i<=N;i++){
if( s[i][j] == 'W' ){
cnt++;
}
if( s[i][j] == '.' ){
nxt[0][i][j] = {last,j};
cost[0][i][j] = cnt*cnt;
last = i;
cnt = 0;
}
}
}
// fill 1
repp(i,N){
ll last = N+1, cnt = 0;
for(int j=N;j>=1;j--){
if( s[i][j] == 'W' ){
cnt++;
}
if( s[i][j] == '.' ){
nxt[1][i][j] = {i,last};
cost[1][i][j] = cnt*cnt;
last = j;
cnt = 0;
}
}
}
// fill 2
repp(j,N){
ll last = N+1, cnt = 0;
for(int i=N;i>=1;i--){
if( s[i][j] == 'W' ){
cnt++;
}
if( s[i][j] == '.' ){
nxt[2][i][j] = {last,j};
cost[2][i][j] = cnt*cnt;
last = i;
cnt = 0;
}
}
}
// fill 3
repp(i,N){
ll last = -1, cnt = 0;
for(int j=1;j<=N;j++){
if( s[i][j] == 'W' ){
cnt++;
}
if( s[i][j] == '.' ){
nxt[3][i][j] = {i,last};
cost[3][i][j] = cnt*cnt;
last = j;
cnt = 0;
}
}
}
repp(i,N) repp(j,N){
if( s[i][j] == '.' && s[i][j-1] == '.' ){
nxt[3][i][j] = nxt[3][i][j-1];
cost[3][i][j] = cost[3][i][j-1];
}
}
repp(i,N) for(int j=N;j>=1;j--){
if( s[i][j] == '.' && s[i][j+1] == '.' ){
nxt[1][i][j] = nxt[1][i][j+1];
cost[1][i][j] = cost[1][i][j+1];
}
}
repp(j,N) for(int i=1;i<=N;i++){
if( s[i][j] == '.' && s[i-1][j] == '.' ){
nxt[0][i][j] = nxt[0][i-1][j];
cost[0][i][j] = cost[0][i-1][j];
}
}
repp(j,N) for(int i=N;i>=1;i--){
if( s[i][j] == '.' && s[i+1][j] == '.' ){
nxt[2][i][j] = nxt[2][i+1][j];
cost[2][i][j] = cost[2][i+1][j];
}
}
priority_queue<Node, vector<Node>, greater<Node>> pq;
rep(d,4) repp(i,N) repp(j,N) dist[d][i][j] = 1e18;
rep(d,4){
int y = nxt[d][sy][sx].Fi;
int x = nxt[d][sy][sx].Se;
if( !inbound(y,x) ) continue;
dist[d][y][x] = cost[d][sy][sx];
pq.push({dist[d][y][x], d,y,x});
}
while( !pq.empty() ){
Node cur = pq.top(); pq.pop();
int dir = cur.dir; ll cc = cur.c;
int y = cur.y, x = cur.x;
if( dist[dir][y][x] < cc ) continue;
rep(d,4){
int ny = nxt[d][y][x].Fi, nx = nxt[d][y][x].Se;
if( dist[d][ny][nx] > cc + cost[d][y][x] ){
dist[d][ny][nx] = cc + cost[d][y][x];
pq.push({dist[d][ny][nx], d, ny, nx});
}
}
int ny = y + yy[dir], nx = x + xx[dir];
if( !inbound(ny,nx) ) continue;
if( s[ny][nx] == 'W' ) continue;
if( dist[dir][ny][nx] > cc ){
dist[dir][ny][nx] = cc;
pq.push({dist[dir][ny][nx], dir, ny, nx});
}
}
ll ans = 1e18;
rep(d,4) ans = min(ans, dist[d][ey][ex]);
if( ans >= 1e17 ) printf("-1\n");
else printf("%lld\n",ans);
}
Compilation message
aqua.cpp: In function 'int main()':
aqua.cpp:79:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&N);
~~~~~^~~~~~~~~
aqua.cpp:80:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
repp(i,N) scanf("%s",s[i]+1);
~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
1128 KB |
Output is correct |
3 |
Correct |
3 ms |
1588 KB |
Output is correct |
4 |
Correct |
5 ms |
3948 KB |
Output is correct |
5 |
Correct |
6 ms |
5436 KB |
Output is correct |
6 |
Correct |
8 ms |
6200 KB |
Output is correct |
7 |
Correct |
10 ms |
6584 KB |
Output is correct |
8 |
Correct |
11 ms |
6732 KB |
Output is correct |
9 |
Correct |
10 ms |
6732 KB |
Output is correct |
10 |
Correct |
8 ms |
6732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
1128 KB |
Output is correct |
3 |
Correct |
3 ms |
1588 KB |
Output is correct |
4 |
Correct |
5 ms |
3948 KB |
Output is correct |
5 |
Correct |
6 ms |
5436 KB |
Output is correct |
6 |
Correct |
8 ms |
6200 KB |
Output is correct |
7 |
Correct |
10 ms |
6584 KB |
Output is correct |
8 |
Correct |
11 ms |
6732 KB |
Output is correct |
9 |
Correct |
10 ms |
6732 KB |
Output is correct |
10 |
Correct |
8 ms |
6732 KB |
Output is correct |
11 |
Correct |
91 ms |
27836 KB |
Output is correct |
12 |
Correct |
115 ms |
56980 KB |
Output is correct |
13 |
Correct |
491 ms |
73116 KB |
Output is correct |
14 |
Correct |
339 ms |
76732 KB |
Output is correct |
15 |
Correct |
124 ms |
78128 KB |
Output is correct |
16 |
Correct |
743 ms |
87028 KB |
Output is correct |
17 |
Correct |
741 ms |
87028 KB |
Output is correct |
18 |
Correct |
282 ms |
87028 KB |
Output is correct |
19 |
Correct |
427 ms |
87028 KB |
Output is correct |
20 |
Correct |
74 ms |
87028 KB |
Output is correct |