結果

問題 No.1588 Connection
ユーザー merlin
提出日時 2021-07-09 00:28:51
言語 Java
(openjdk 23)
結果
AC  
実行時間 330 ms / 2,000 ms
コード長 1,959 bytes
コンパイル時間 2,435 ms
コンパイル使用メモリ 80,672 KB
実行使用メモリ 70,588 KB
平均クエリ数 485.91
最終ジャッジ日時 2024-07-17 12:49:30
合計ジャッジ時間 9,480 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

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;

        int di[][]={{-1,0},{0,1},{1,0},{0,-1}};
        boolean vis[][]=new boolean[n+1][n+1],ask[][]=new boolean[n+1][n+1];
        vis[1][1]=true;
        Stack<Pair> q=new Stack<>();
        q.add(new Pair(1,1));
        while(!q.isEmpty())
        {
            Pair p=q.pop();
            //System.out.println(p.x+" "+p.y);
            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])
                {
                    vis[tx][ty]=true;
                    if(b[tx][ty]) q.add(new Pair(tx,ty));
                    else if(!ask[tx][ty] && m>0)
                    {
                        System.out.print(tx+" "+ty+"\n");
                        System.out.flush();
                        String ver=bu.readLine();
                        if(ver.charAt(0)=='-') return;
                        if(ver.charAt(0)=='B')
                        {
                            q.add(new Pair(tx,ty));
                            b[tx][ty]=true;
                            m--;
                        }
                        ask[tx][ty]=true;
                    }
                }
            }
            if(vis[n][n]) break;
        }

        if(vis[n][n]) 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