結果

問題 No.1323 うしらずSwap
ユーザー tailstails
提出日時 2020-12-20 01:38:47
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 2,416 bytes
コンパイル時間 337 ms
コンパイル使用メモリ 32,512 KB
実行使用メモリ 272,556 KB
最終ジャッジ日時 2024-09-21 11:15:22
合計ジャッジ時間 12,884 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 1 ms
6,940 KB
testcase_11 WA -
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 1 ms
6,940 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 148 ms
144,464 KB
testcase_17 AC 274 ms
148,564 KB
testcase_18 AC 150 ms
144,636 KB
testcase_19 AC 272 ms
148,592 KB
testcase_20 AC 146 ms
144,436 KB
testcase_21 AC 144 ms
144,472 KB
testcase_22 AC 252 ms
148,844 KB
testcase_23 AC 143 ms
144,392 KB
testcase_24 AC 253 ms
148,776 KB
testcase_25 AC 141 ms
144,884 KB
testcase_26 AC 102 ms
99,260 KB
testcase_27 AC 102 ms
93,276 KB
testcase_28 AC 162 ms
146,244 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 750 ms
208,108 KB
testcase_34 AC 716 ms
230,348 KB
testcase_35 AC 592 ms
241,308 KB
testcase_36 AC 637 ms
248,128 KB
testcase_37 AC 582 ms
251,680 KB
testcase_38 AC 597 ms
268,548 KB
testcase_39 AC 597 ms
270,448 KB
testcase_40 AC 616 ms
271,484 KB
testcase_41 AC 613 ms
272,556 KB
testcase_42 AC 560 ms
272,320 KB
testcase_43 AC 87 ms
68,152 KB
testcase_44 AC 138 ms
87,400 KB
testcase_45 AC 125 ms
103,320 KB
testcase_46 AC 141 ms
87,124 KB
testcase_47 AC 94 ms
80,452 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 AC 13 ms
14,580 KB
testcase_60 AC 2 ms
6,944 KB
testcase_61 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c:7:19: warning: no semicolon at end of struct or union
    7 | struct{int y,x,z,d} q[16000000];
      |                   ^
main.c:112:1: warning: return type defaults to 'int' [-Wimplicit-int]
  112 | main(){
      | ^~~~
main.c: In function 'main':
main.c:113:9: warning: implicit declaration of function 'scanf' [-Wimplicit-function-declaration]
  113 |         scanf("%d%d%d%d%d%d",&h,&w,&ra,&ca,&rb,&cb);
      |         ^~~~~
main.c:1:1: note: include '<stdio.h>' or provide a declaration of 'scanf'
  +++ |+#include <stdio.h>
    1 | int h,w,ra,ca,rb,cb;
main.c:113:9: warning: incompatible implicit declaration of built-in function 'scanf' [-Wbuiltin-declaration-mismatch]
  113 |         scanf("%d%d%d%d%d%d",&h,&w,&ra,&ca,&rb,&cb);
      |         ^~~~~
main.c:113:9: note: include '<stdio.h>' or provide a declaration of 'scanf'
main.c:121:17: warning: implicit declaration of function 'puts' [-Wimplicit-function-declaration]
  121 |                 puts("-1");
      |                 ^~~~
main.c:121:17: note: include '<stdio.h>' or provide a declaration of 'puts'
main.c:122:17: warning: implicit declaration of function 'exit' [-Wimplicit-function-declaration]
  122 |                 exit(0);
      |                 ^~~~
main.c:1:1: note: include '<stdlib.h>' or provide a declaration of 'exit'
  +++ |+#include <stdlib.h>
    1 | int h,w,ra,ca,rb,cb;
main.c:122:17: warning: incompatible implicit declaration of built-in function 'exit' [-Wbuiltin-declaration-mismatch]
  122 |                 exit(0);
      |                 ^~~~
main.c:122:17: note: include '<stdlib.h>' or provide a declaration of 'exit'
main.c:143:17: warning: incompatible implicit declaration of built-in function 'exit' [-Wbuiltin-declaration-mismatch]
  143 |                 exit(0);
      |                 ^~~~
main.c:143:17: note: include '<stdlib.h>' or provide a declaration of 'exit'
main.c:145:9: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
  145 |  

ソースコード

diff #

int h,w,ra,ca,rb,cb;
char s[1333][1335];

int dp1[1333][1333];
int dp2[1333][1333][2];

struct{int y,x,z,d} q[16000000];
int qr,qw;

void enq(int y,int x,int z,int d){
	q[qw].y=y;
	q[qw].x=x;
	q[qw].z=z;
	q[qw].d=d;
	++qw;
}

void f1(){
	qr=qw=0;
	enq(ra,ca,0,1);
	while(qr<qw){
		int y=q[qr].y;
		int x=q[qr].x;
		int d=q[qr].d;
		++qr;
		if(s[y][x]=='.'&&dp1[y][x]==0){
			dp1[y][x]=d;
			enq(y-1,x,0,d+1);
			enq(y+1,x,0,d+1);
			enq(y,x-1,0,d+1);
			enq(y,x+1,0,d+1);
		}
	}
}

void f2(int y,int x){
	int d=dp1[y][x];
	dp1[y][x]=-1;
	if(d==1){
	}else if(dp1[y-1][x]==d-1){
		f2(y-1,x);
	}else if(dp1[y+1][x]==d-1){
		f2(y+1,x);
	}else if(dp1[y][x-1]==d-1){
		f2(y,x-1);
	}else if(dp1[y][x+1]==d-1){
		f2(y,x+1);
	}
}

void f3(){
	qr=qw=0;
	enq(ra,ca,0,1);
	while(qr<qw){
		int y=q[qr].y;
		int x=q[qr].x;
		int z=q[qr].z;
		int d=q[qr].d;
		++qr;
		if(s[y][x]=='.'&&dp2[y][x][z]==0&&(z?!(y==ra&&x==ca):!(y==rb&&x==cb))){
			dp2[y][x][z]=d;
			if(z==0&&dp1[y][x]>=0){
				enq(y,x,1,d+1);
			}
			enq(y-1,x,z,d+1);
			enq(y+1,x,z,d+1);
			enq(y,x-1,z,d+1);
			enq(y,x+1,z,d+1);
		}
	}
}

int f4(int r,int c,int vy,int vx){
	for(int y=0;y<h;++y){
		for(int x=0;x<w;++x){
			dp1[y][x]=0;
		}
	}
	qr=qw=0;
	{
		int y=r+vy;
		int x=c+vx;
		if(s[y][x]=='.'){
			dp1[y][x]=1;
			if(s[y+vy][x+vx]=='.'){
				enq(y+vy,x+vx,0,2);
			}
			if(s[y-vx][x+vy]=='.'){
				enq(y-vx,x+vy,0,2);
			}
			if(s[y+vx][x-vy]=='.'){
				enq(y+vx,x-vy,0,2);
			}
		}
	}
	while(qr<qw){
		int y=q[qr].y;
		int x=q[qr].x;
		int d=q[qr].d;
		++qr;
		if(s[y][x]=='.'&&dp1[y][x]==0){
			dp1[y][x]=d;
			enq(y-1,x,0,d+1);
			enq(y+1,x,0,d+1);
			enq(y,x-1,0,d+1);
			enq(y,x+1,0,d+1);
		}
	}
	return dp1[r][c];
}

main(){
	scanf("%d%d%d%d%d%d",&h,&w,&ra,&ca,&rb,&cb);
	--ra;--ca;--rb;--cb;
	for(int y=0;y<h;++y){
		scanf("%s",s[y]);
	}
	f1();
	int d1=dp1[rb][cb]-1;
	if(d1<0){
		puts("-1");
		exit(0);
	}
	f2(rb,cb);
	f3();
	int d2=dp2[rb][cb][1]-2;
	if(d2<0){
		d2=3000000;
	}
	{
		int d;
		d=f4(ra,ca,-1,0);if(d>0&&d2>d+d1+2)d2=d+d1+2;
		d=f4(ra,ca,+1,0);if(d>0&&d2>d+d1+2)d2=d+d1+2;
		d=f4(ra,ca,0,-1);if(d>0&&d2>d+d1+2)d2=d+d1+2;
		d=f4(ra,ca,0,+1);if(d>0&&d2>d+d1+2)d2=d+d1+2;
		d=f4(rb,cb,-1,0);if(d>0&&d2>d+d1+2)d2=d+d1+2;
		d=f4(rb,cb,+1,0);if(d>0&&d2>d+d1+2)d2=d+d1+2;
		d=f4(rb,cb,0,-1);if(d>0&&d2>d+d1+2)d2=d+d1+2;
		d=f4(rb,cb,0,+1);if(d>0&&d2>d+d1+2)d2=d+d1+2;
	}
	if(d2==3000000){
		puts("-1");
		exit(0);
	}
	printf("%d",d1+d2);
}
0