結果

問題 No.1323 うしらずSwap
ユーザー kzyKTkzyKT
提出日時 2020-12-20 16:06:49
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 280 ms / 3,000 ms
コード長 1,684 bytes
コンパイル時間 1,835 ms
コンパイル使用メモリ 172,532 KB
実行使用メモリ 57,120 KB
最終ジャッジ日時 2024-04-27 08:54:33
合計ジャッジ時間 11,922 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 1 ms
6,948 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 192 ms
47,616 KB
testcase_17 AC 273 ms
47,616 KB
testcase_18 AC 192 ms
47,488 KB
testcase_19 AC 270 ms
47,616 KB
testcase_20 AC 191 ms
47,488 KB
testcase_21 AC 192 ms
47,360 KB
testcase_22 AC 280 ms
47,488 KB
testcase_23 AC 190 ms
47,360 KB
testcase_24 AC 268 ms
47,488 KB
testcase_25 AC 189 ms
47,488 KB
testcase_26 AC 190 ms
51,472 KB
testcase_27 AC 197 ms
57,120 KB
testcase_28 AC 200 ms
47,488 KB
testcase_29 AC 172 ms
47,616 KB
testcase_30 AC 240 ms
47,488 KB
testcase_31 AC 178 ms
47,488 KB
testcase_32 AC 241 ms
47,360 KB
testcase_33 AC 228 ms
33,536 KB
testcase_34 AC 213 ms
34,176 KB
testcase_35 AC 219 ms
37,972 KB
testcase_36 AC 204 ms
33,792 KB
testcase_37 AC 198 ms
33,664 KB
testcase_38 AC 196 ms
33,536 KB
testcase_39 AC 203 ms
38,184 KB
testcase_40 AC 201 ms
33,920 KB
testcase_41 AC 203 ms
33,664 KB
testcase_42 AC 188 ms
34,760 KB
testcase_43 AC 123 ms
47,616 KB
testcase_44 AC 165 ms
47,360 KB
testcase_45 AC 154 ms
47,616 KB
testcase_46 AC 170 ms
47,360 KB
testcase_47 AC 136 ms
47,488 KB
testcase_48 AC 136 ms
47,488 KB
testcase_49 AC 199 ms
47,360 KB
testcase_50 AC 121 ms
47,488 KB
testcase_51 AC 83 ms
47,488 KB
testcase_52 AC 104 ms
47,488 KB
testcase_53 AC 204 ms
47,360 KB
testcase_54 AC 101 ms
47,488 KB
testcase_55 AC 209 ms
47,360 KB
testcase_56 AC 88 ms
47,488 KB
testcase_57 AC 217 ms
47,488 KB
testcase_58 AC 2 ms
6,940 KB
testcase_59 AC 3 ms
6,940 KB
testcase_60 AC 2 ms
6,944 KB
testcase_61 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define ll long long
#define pb(a) push_back(a)
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1e9+7,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<ll,ll> P;

ll n,m;
string s[2222];
vector<vector<ll> > bfs(int sx,int sy) {
  vector<vector<ll> > d(n,vector<ll>(m,MAX));
  d[sx][sy]=0;
  queue<P> que;
  que.push(P(sx,sy));
  while(!que.empty()) {
    P p=que.front();que.pop();
    ll xx=p.F,yy=p.S;
    rep(i,4) {
      ll x=xx+dx[i],y=yy+dy[i];
      if(check(n,m,x,y)&&s[x][y]=='.'&&d[x][y]==MAX) {
        d[x][y]=d[xx][yy]+1;
        que.push(P(x,y));
      }
    }
  }
  return d;
}

int main() {
  ll sx,sy,tx,ty;
  cin >> n >> m >> sx >> sy >> tx >> ty;
  rep(i,n) cin >> s[i];
  sx--,sy--,tx--,ty--;
  vector<vector<ll> > c=bfs(sx,sy);
  vector<vector<ll> > d=bfs(tx,ty);
  ll ans=MAX;
  vector<P> v;
  rep(i,n)rep(j,m)if(s[i][j]=='.'&&c[i][j]+d[i][j]==c[tx][ty]) v.pb(P(i,j));
  if(v.size()>c[tx][ty]+1) {
    cout<<c[tx][ty]*2<<endl;
    return 0;
  }
  rep(i,n)rep(j,m) {
    if(s[i][j]=='#') continue;
    ll dd=c[i][j]+d[i][j];
    vector<P> v;
    rep(k,4) {
      int x=i+dx[k],y=j+dy[k];
      if(check(n,m,x,y)&&s[x][y]=='.') v.pb(P(x,y));
    }
    if(v.size()>=3) ans=min(ans,dd*2+(dd==c[tx][ty]&&P(i,j)!=P(sx,sy)&&P(i,j)!=P(tx,ty)?2:4));
  }
  s[sx][sy]='#';
  rep(k,4) {
    ll x=sx+dx[k],y=sy+dy[k];
    if(check(n,m,x,y)&&s[x][y]=='.'&&d[x][y]+c[x][y]!=c[tx][ty]) {
      vector<vector<ll> > e=bfs(x,y);
      ans=min(ans,d[sx][sy]+e[tx][ty]+1);
    }
  }
  if(ans==MAX) ans=-1;
  cout<<ans<<endl;
  return 0;
}
0