結果

問題 No.1323 うしらずSwap
ユーザー fgwiebfaoishfgwiebfaoish
提出日時 2020-12-20 03:59:36
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,721 ms / 3,000 ms
コード長 5,217 bytes
コンパイル時間 4,241 ms
コンパイル使用メモリ 110,300 KB
実行使用メモリ 64,164 KB
最終ジャッジ日時 2023-08-09 13:54:27
合計ジャッジ時間 41,787 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
20,924 KB
testcase_01 AC 62 ms
18,784 KB
testcase_02 AC 64 ms
20,968 KB
testcase_03 AC 62 ms
18,736 KB
testcase_04 AC 61 ms
18,836 KB
testcase_05 AC 63 ms
20,948 KB
testcase_06 AC 63 ms
22,784 KB
testcase_07 AC 65 ms
20,972 KB
testcase_08 AC 64 ms
20,888 KB
testcase_09 AC 64 ms
20,940 KB
testcase_10 AC 65 ms
21,084 KB
testcase_11 AC 64 ms
22,908 KB
testcase_12 AC 63 ms
20,836 KB
testcase_13 AC 63 ms
20,828 KB
testcase_14 AC 64 ms
20,680 KB
testcase_15 AC 63 ms
20,784 KB
testcase_16 AC 641 ms
61,992 KB
testcase_17 AC 780 ms
53,448 KB
testcase_18 AC 653 ms
64,040 KB
testcase_19 AC 791 ms
53,388 KB
testcase_20 AC 632 ms
63,892 KB
testcase_21 AC 628 ms
59,824 KB
testcase_22 AC 796 ms
63,956 KB
testcase_23 AC 639 ms
63,760 KB
testcase_24 AC 774 ms
51,488 KB
testcase_25 AC 621 ms
61,904 KB
testcase_26 AC 613 ms
61,760 KB
testcase_27 AC 631 ms
63,972 KB
testcase_28 AC 750 ms
63,948 KB
testcase_29 AC 530 ms
63,864 KB
testcase_30 AC 632 ms
51,424 KB
testcase_31 AC 536 ms
63,768 KB
testcase_32 AC 626 ms
51,280 KB
testcase_33 AC 1,236 ms
51,196 KB
testcase_34 AC 1,376 ms
61,940 KB
testcase_35 AC 1,434 ms
51,448 KB
testcase_36 AC 1,499 ms
62,128 KB
testcase_37 AC 1,502 ms
53,660 KB
testcase_38 AC 1,616 ms
53,324 KB
testcase_39 AC 1,681 ms
63,928 KB
testcase_40 AC 1,649 ms
51,696 KB
testcase_41 AC 1,721 ms
64,164 KB
testcase_42 AC 1,640 ms
51,492 KB
testcase_43 AC 395 ms
59,712 KB
testcase_44 AC 473 ms
53,316 KB
testcase_45 AC 559 ms
63,972 KB
testcase_46 AC 467 ms
53,320 KB
testcase_47 AC 431 ms
61,828 KB
testcase_48 AC 367 ms
61,964 KB
testcase_49 AC 493 ms
51,184 KB
testcase_50 AC 313 ms
51,196 KB
testcase_51 AC 133 ms
51,360 KB
testcase_52 AC 215 ms
53,240 KB
testcase_53 AC 469 ms
51,380 KB
testcase_54 AC 226 ms
51,552 KB
testcase_55 AC 499 ms
59,636 KB
testcase_56 AC 192 ms
60,056 KB
testcase_57 AC 593 ms
63,900 KB
testcase_58 AC 67 ms
23,016 KB
testcase_59 AC 69 ms
24,976 KB
testcase_60 AC 68 ms
20,852 KB
testcase_61 AC 66 ms
20,968 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
using System.Threading;
using System.Runtime.CompilerServices;
//using nint=System.Int32;
static class Program{
	const int mod=(int)1e9+7;
	static readonly int[][] di1=new int[][]{new int[]{-1,0},new int[]{1,0},new int[]{0,-1},new int[]{0,1}};
	static readonly int[][] di2=new int[][]{new int[]{-1,-1},new int[]{-1,0},new int[]{-1,1},new int[]{0,-1},new int[]{0,1},new int[]{1,-1},new int[]{1,0},new int[]{1,1}};
	static char[][] bm;
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Ia;
		int h=s[0],w=s[1];
		char c='#';
		bm=new char[h+2][];
		var b1=new int[h+2][];
		var b2=new int[h+2][];
		var b3=new int[h+2][];
		var qu=new Queue<ValueTuple<int,int,int>>();
		for(int i = 1;i<=h;i++) {
			bm[i]=(c+sc.S+c).ToCharArray();
			b1[i]=new int[w+2];
			b2[i]=new int[w+2];
			b3[i]=new int[w+2];
		}
		bm[0]=bm[h+1]=new string(c,w+2).ToCharArray();
		b1[0]=b1[h+1]=b2[0]=b2[h+1]=b3[0]=b3[h+1]=new int[w+2];

		qu.Enqueue(ValueTuple.Create(s[2],s[3],1));
		b1[s[2]][s[3]]=1;
		while(qu.Count>0){
			var e=qu.Dequeue();
			Fdi(e.Item1,e.Item2,(y,x,z)=>{
				if(bm[y][x]=='.'&&b1[y][x]==0){
					qu.Enqueue(ValueTuple.Create(y,x,e.Item3+1));
					b1[y][x]=e.Item3+1;
				}
			});
		}
		qu=new Queue<ValueTuple<int,int,int>>();
		qu.Enqueue(ValueTuple.Create(s[4],s[5],1));
		b2[s[4]][s[5]]=1;
		while(qu.Count>0){
			var e=qu.Dequeue();
			Fdi(e.Item1,e.Item2,(y,x,z)=>{
				if(bm[y][x]=='.'&&b2[y][x]==0){
					qu.Enqueue(ValueTuple.Create(y,x,e.Item3+1));
					b2[y][x]=e.Item3+1;
				}
			});
		}
		qu=new Queue<ValueTuple<int,int,int>>();
		qu.Enqueue(ValueTuple.Create(s[2],s[3],1));
		b3[s[2]][s[3]]=1;
		while(qu.Count>0){
			var e=qu.Dequeue();
			if(e.Item1==s[4]&&e.Item2==s[5]){continue;}
			Fdi(e.Item1,e.Item2,(y,x,z)=>{
				if(bm[y][x]=='.'&&b3[y][x]==0){
					qu.Enqueue(ValueTuple.Create(y,x,e.Item3+1));
					b3[y][x]=e.Item3+1;
				}
			});
		}
		if(b1[s[4]][s[5]]==0){
			Console.WriteLine(-1);
			return;
		}
		int ans=mod;
		var li=new List<int>();
		Fdi(s[4],s[5],(y,x,z)=>{
			if(bm[y][x]!=c&&b3[y][x]!=0){li.Add(b3[y][x]);}
		});
		if(li.Count>=2){
			li.Sort();
			ans=li[0]+li[1];
		}
		for(int i = 1;i<=h;i++) {
			for(int j = 1;j<=w;j++) {
				if(bm[i][j]!=c){
					if(b1[i][j]==0||b2[i][j]==0){continue;}
					int p1=0,p2=0,g1=0,g2=0;
					var li1=new List<int>();
					var li2=new List<int>();
					Fdi(i,j,(y,x,z)=>{
						if(bm[y][x]!=c){
							if(b1[i][j]>b1[y][x]){p1++;g1|=1<<z;}
							else{li1.Add(b2[y][x]);}
							if(b2[i][j]>b2[y][x]){p2++;g2|=1<<z;}
							else{li2.Add(b1[y][x]);}
						}
					});
					if(p1+li1.Count>=3){
						if(p1==1&&p2==1&&(g1&g2)!=0){ans=Min(ans,b1[i][j]*2+b2[i][j]*2);}
						else if(p1+p2==1){ans=Min(ans,b1[i][j]*2+b2[i][j]*2);}
						else{ans=Min(ans,b1[i][j]*2+b2[i][j]*2-2);}
					}
					if(p1>=2&&li1.Count>=1){
						li1.Sort();
						ans=Min(ans,(b1[i][j]-1)*2+li1[0]*2);
					}
					if(p2>=2&&li2.Count>=1){
						li2.Sort();
						ans=Min(ans,(b2[i][j]-1)*2+li2[0]*2);
					}
					if(i==s[2]&&j==s[3]&&p2>=2){ans=Min(ans,(b2[i][j]-1)*2);}
					if(i==s[4]&&j==s[5]&&p1>=2){ans=Min(ans,(b1[i][j]-1)*2);}
				}
			}
		}
		Console.WriteLine("{0}",ans==mod?-1:ans);
	}
	static void Fdi(int y,int x,Action<int,int,int> f){for(int i=0;i<4;i++){f(y+di1[i][0],x+di1[i][1],i);}}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),double.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}
0