結果

問題 No.707 書道
ユーザー takeya_okinotakeya_okino
提出日時 2019-09-29 13:10:58
言語 Java19
(openjdk 21)
結果
AC  
実行時間 150 ms / 2,000 ms
コード長 1,650 bytes
コンパイル時間 1,968 ms
コンパイル使用メモリ 79,704 KB
実行使用メモリ 56,716 KB
最終ジャッジ日時 2023-07-26 17:41:32
合計ジャッジ時間 3,858 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 115 ms
56,056 KB
testcase_01 AC 114 ms
55,992 KB
testcase_02 AC 129 ms
55,924 KB
testcase_03 AC 115 ms
56,052 KB
testcase_04 AC 115 ms
55,584 KB
testcase_05 AC 142 ms
56,716 KB
testcase_06 AC 150 ms
55,824 KB
testcase_07 AC 150 ms
56,240 KB
testcase_08 AC 112 ms
55,836 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