結果

問題 No.1323 うしらずSwap
ユーザー kzyKTkzyKT
提出日時 2020-12-20 01:13:06
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 210 ms / 3,000 ms
コード長 2,935 bytes
コンパイル時間 1,795 ms
コンパイル使用メモリ 158,712 KB
実行使用メモリ 55,444 KB
最終ジャッジ日時 2023-08-09 13:51:24
合計ジャッジ時間 10,347 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 2 ms
4,384 KB
testcase_14 AC 2 ms
4,384 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 154 ms
46,940 KB
testcase_17 AC 205 ms
46,756 KB
testcase_18 AC 155 ms
46,928 KB
testcase_19 AC 209 ms
46,700 KB
testcase_20 AC 157 ms
46,516 KB
testcase_21 AC 155 ms
46,692 KB
testcase_22 AC 206 ms
46,704 KB
testcase_23 AC 153 ms
46,564 KB
testcase_24 AC 210 ms
46,652 KB
testcase_25 AC 156 ms
46,648 KB
testcase_26 AC 139 ms
50,636 KB
testcase_27 AC 130 ms
55,444 KB
testcase_28 AC 99 ms
46,652 KB
testcase_29 AC 134 ms
46,616 KB
testcase_30 AC 163 ms
46,648 KB
testcase_31 AC 135 ms
46,568 KB
testcase_32 AC 176 ms
46,652 KB
testcase_33 AC 189 ms
32,808 KB
testcase_34 AC 182 ms
33,396 KB
testcase_35 AC 176 ms
38,128 KB
testcase_36 AC 167 ms
33,228 KB
testcase_37 AC 167 ms
32,852 KB
testcase_38 AC 157 ms
32,816 KB
testcase_39 AC 165 ms
37,676 KB
testcase_40 AC 157 ms
32,952 KB
testcase_41 AC 160 ms
32,924 KB
testcase_42 AC 151 ms
33,676 KB
testcase_43 AC 64 ms
46,608 KB
testcase_44 AC 96 ms
46,632 KB
testcase_45 AC 76 ms
46,888 KB
testcase_46 AC 101 ms
46,648 KB
testcase_47 AC 69 ms
46,516 KB
testcase_48 AC 94 ms
46,604 KB
testcase_49 AC 146 ms
46,620 KB
testcase_50 AC 86 ms
46,504 KB
testcase_51 AC 45 ms
46,520 KB
testcase_52 AC 65 ms
46,632 KB
testcase_53 AC 148 ms
46,676 KB
testcase_54 AC 66 ms
46,632 KB
testcase_55 AC 159 ms
46,624 KB
testcase_56 AC 55 ms
46,656 KB
testcase_57 AC 157 ms
46,520 KB
testcase_58 AC 2 ms
4,384 KB
testcase_59 AC 3 ms
4,380 KB
testcase_60 AC 2 ms
4,380 KB
testcase_61 AC 2 ms
4,384 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) {
      if(s[i][j]=='#') continue;
      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];
            v.pb(P(x,y));
          }
        }
        if(v.size()>=3) ans=min(ans,(c[i][j]+d[i][j])*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);
      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