結果

問題 No.707 書道
ユーザー takeya_okinotakeya_okino
提出日時 2019-09-29 13:10:58
言語 Java21
(openjdk 21)
結果
AC  
実行時間 182 ms / 2,000 ms
コード長 1,650 bytes
コンパイル時間 2,374 ms
コンパイル使用メモリ 77,692 KB
実行使用メモリ 54,668 KB
最終ジャッジ日時 2024-04-14 07:14:30
合計ジャッジ時間 4,565 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 138 ms
54,108 KB
testcase_01 AC 139 ms
54,084 KB
testcase_02 AC 148 ms
53,784 KB
testcase_03 AC 135 ms
54,252 KB
testcase_04 AC 131 ms
54,092 KB
testcase_05 AC 168 ms
54,668 KB
testcase_06 AC 171 ms
54,364 KB
testcase_07 AC 182 ms
54,268 KB
testcase_08 AC 137 ms
53,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int h = sc.nextInt();
    int w = sc.nextInt();
    double ans = 1000000000;
    ArrayList<Integer> listx = new ArrayList<Integer>();
    ArrayList<Integer> listy = new ArrayList<Integer>();
    for(int i = 1; i <= h; i++) {
      String s = sc.next();
      for(int j = 1; j <= w; j++) {
        if(s.charAt(j - 1) == '1') {
          listx.add(i);
          listy.add(j);
        }
      }
    }
    for(int j = 1; j <= w; j++) {
      double te = 0;
      for(int k = 0; k < listx.size(); k++) {
        double t1 = listx.get(k);
        double t2 = listy.get(k) - j;
        te += Math.sqrt(t1 * t1 + t2 * t2);
      }
      ans = Math.min(ans, te);
    }
    for(int j = 1; j <= w; j++) {
      double te = 0;
      for(int k = 0; k < listx.size(); k++) {
        double t1 = listx.get(k) - h - 1;
        double t2 = listy.get(k) - j;
        te += Math.sqrt(t1 * t1 + t2 * t2);        
      }
      ans = Math.min(ans, te);
    }
    for(int i = 1; i <= h; i++) {
      double te = 0;
      for(int k = 0; k < listx.size(); k++) {
        double t1 = listx.get(k) - i;
        double t2 = listy.get(k);
        te += Math.sqrt(t1 * t1 + t2 * t2);        
      }
      ans = Math.min(ans, te);
    }
    for(int i = 1; i <= h; i++) {
      double te = 0;
      for(int k = 0; k < listx.size(); k++) {
        double t1 = listx.get(k) - i;
        double t2 = listy.get(k) - w - 1;
        te += Math.sqrt(t1 * t1 + t2 * t2);        
      }
      ans = Math.min(ans, te);
    }
    System.out.println(ans);
  }
}
0