結果

問題 No.1323 うしらずSwap
ユーザー kzyKTkzyKT
提出日時 2020-12-20 01:08:40
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,952 bytes
コンパイル時間 2,773 ms
コンパイル使用メモリ 174,952 KB
実行使用メモリ 49,592 KB
最終ジャッジ日時 2023-10-21 10:05:39
合計ジャッジ時間 8,646 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 WA -
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 WA -
testcase_10 AC 2 ms
4,348 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 1 ms
4,348 KB
testcase_14 WA -
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 118 ms
33,184 KB
testcase_17 AC 150 ms
33,148 KB
testcase_18 AC 120 ms
33,200 KB
testcase_19 AC 159 ms
33,216 KB
testcase_20 AC 127 ms
33,180 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 80 ms
33,100 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 172 ms
32,948 KB
testcase_34 AC 168 ms
33,468 KB
testcase_35 AC 154 ms
37,264 KB
testcase_36 AC 148 ms
33,212 KB
testcase_37 AC 146 ms
33,108 KB
testcase_38 AC 131 ms
32,960 KB
testcase_39 AC 148 ms
37,204 KB
testcase_40 AC 145 ms
33,208 KB
testcase_41 AC 143 ms
33,008 KB
testcase_42 AC 130 ms
33,916 KB
testcase_43 AC 47 ms
33,008 KB
testcase_44 AC 68 ms
33,008 KB
testcase_45 AC 63 ms
33,008 KB
testcase_46 AC 78 ms
33,008 KB
testcase_47 AC 54 ms
33,008 KB
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);}
template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-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 x=p.F,y=p.S;
    rep(i,4) {
      ll xx=x+dx[i],yy=y+dy[i];
      if(check(n,m,xx,yy)&&s[xx][yy]=='.'&&d[xx][yy]==MAX) {
        d[xx][yy]=d[x][y]+1;
        que.push(P(xx,yy));
      }
    }
  }
  return d;
}

void 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]=='.') {
        ll dd=c[i][j]+d[i][j];
        if(dd==c[tx][ty]) v.pb(P(i,j));
      }
    }
  }
  if(v.size()>c[tx][ty]+1) {
    pr(c[tx][ty]*2);
    return;
  }
  rep(i,n) {
    rep(j,m) {
      ll dd=c[i][j]+d[i][j];
      if(dd==c[tx][ty]) {
        vector<P> v;
        rep(k,4) {
          int x=i+dx[k],y=j+dy[k];
          if(check(n,m,x,y)&&s[x][y]=='.') {
            ll cc=c[x][y]+d[x][y];
            if(cc!=dd) v.pb(P(x,y));
          }
        }
        if(P(i,j)!=P(sx,sy)&&P(i,j)!=P(tx,ty)) {
          if(v.size()) ans=min(ans,dd*2+2);
        } else {
          if(v.size()>=2) ans=min(ans,dd*2+4);
        }
      }
      if(c[i][j]==d[sx][sy]+d[i][j]||d[i][j]==c[tx][ty]+c[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]=='.') {
            ll cc=c[x][y]+d[x][y];
            if(cc!=dd) v.pb(P(x,y));
          }
        }
        if(v.size()>=2) ans=min(ans,(c[i][j]+d[i][j])*2+4);
      }
    }
  }
  if(ans==MAX) {
    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);
        ll dd=d[sx][sy]+e[tx][ty]+1;
        ans=min(ans,dd);
      }
    }
  }
  if(ans==MAX) ans=-1;
  pr(ans);
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}
0