結果

問題 No.1588 Connection
ユーザー merlinmerlin
提出日時 2021-07-09 00:04:28
言語 Java21
(openjdk 21)
結果
WA  
(最新)
QLE  
(最初)
実行時間 -
コード長 3,011 bytes
コンパイル時間 2,202 ms
コンパイル使用メモリ 80,000 KB
実行使用メモリ 84,768 KB
平均クエリ数 425.47
最終ジャッジ日時 2023-09-24 11:51:39
合計ジャッジ時間 9,749 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 103 ms
68,700 KB
testcase_01 AC 99 ms
68,764 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 118 ms
68,344 KB
testcase_10 AC 117 ms
68,516 KB
testcase_11 AC 118 ms
68,312 KB
testcase_12 AC 217 ms
67,172 KB
testcase_13 AC 213 ms
68,704 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 442 ms
84,332 KB
testcase_22 AC 432 ms
84,252 KB
testcase_23 AC 241 ms
72,972 KB
testcase_24 AC 181 ms
69,364 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 188 ms
70,192 KB
testcase_30 AC 187 ms
69,700 KB
testcase_31 AC 95 ms
68,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder();
        String s[]=bu.readLine().split(" ");
        int n=Integer.parseInt(s[0]),m=Integer.parseInt(s[1])-2;
        boolean b[][]=new boolean[n+1][n+1];
        b[1][1]=b[n][n]=true;

        ArrayList<Pair> g[]=new ArrayList[2*n+1];
        int i,j;
        for(i=0;i<=2*n;i++) g[i]=new ArrayList<>();
        for(i=1;i<=n;i++)
        for(j=1;j<=n;j++) g[i+j].add(new Pair(i,j));

        int l=2,r=2*n; boolean vis[][]=new boolean[n+1][n+1];
        while(l<=r)
        {
            Queue<Pair> q=new LinkedList<>();
            for(Pair x:g[l])
            {
                if(b[x.x][x.y] && x.x+1<=n && !vis[x.x+1][x.y])
                {
                    vis[x.x+1][x.y]=true;
                    q.add(new Pair(x.x+1,x.y));
                }
                if(b[x.x][x.y] && x.y+1<=n && !vis[x.x][x.y+1])
                {
                    vis[x.x][x.y+1]=true;
                    q.add(new Pair(x.x,x.y+1));
                }
            }

            for(Pair x:g[r])
            {
                if(b[x.x][x.y] && x.x-1>0 && !vis[x.x-1][x.y])
                {
                    vis[x.x-1][x.y]=true;
                    q.add(new Pair(x.x-1,x.y));
                }
                if(b[x.x][x.y] && x.y-1>0 && !vis[x.x][x.y-1])
                {
                    vis[x.x][x.y-1]=true;
                    q.add(new Pair(x.x,x.y-1));
                }
            }

            while(!q.isEmpty())
            {
                Pair p=q.poll();
                System.out.print(p.x+" "+p.y+"\n");
                System.out.flush();
                String ver=bu.readLine();
                if(ver.charAt(0)=='-') return;
                if(ver.charAt(0)=='B')
                {
                    m--;
                    b[p.x][p.y]=true;
                }
                if(m==0) break;
            }

            if(m==0) break;
            l++; r--;
        }

        for(i=1;i<=n;i++) Arrays.fill(vis[i],false);
        vis[1][1]=true; int di[][]={{-1,0},{0,1},{1,0},{0,-1}};
        Queue<Pair> q=new LinkedList<>();
        q.add(new Pair(1,1));
        while(!q.isEmpty())
        {
            Pair p=q.poll();
            for(int d[]:di)
            {
                int tx=p.x+d[0],ty=p.y+d[1];
                if(tx>0 && tx<=n && ty>0 && ty<=n && !vis[tx][ty] && b[tx][ty])
                {
                    q.add(new Pair(tx,ty));
                    vis[tx][ty]=true;
                }
            }
        }

        if(vis[n][n] || (l>r && m>0)) sb.append("Yes\n");
        else sb.append("No\n");
        System.out.print(sb);
        System.out.flush();
        return;
    }

    static class Pair
    {
        int x,y;
        Pair(int a,int b)
        {
            x=a;
            y=b;
        }
    }
}
0