結果

問題 No.707 書道
ユーザー silviasetitechsilviasetitech
提出日時 2020-03-15 14:50:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 173 ms / 2,000 ms
コード長 3,178 bytes
コンパイル時間 5,354 ms
コンパイル使用メモリ 76,364 KB
実行使用メモリ 56,592 KB
最終ジャッジ日時 2023-08-16 11:57:28
合計ジャッジ時間 5,231 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,656 KB
testcase_01 AC 128 ms
55,648 KB
testcase_02 AC 139 ms
55,348 KB
testcase_03 AC 129 ms
55,652 KB
testcase_04 AC 127 ms
55,420 KB
testcase_05 AC 153 ms
56,592 KB
testcase_06 AC 160 ms
56,432 KB
testcase_07 AC 173 ms
56,412 KB
testcase_08 AC 127 ms
55,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author silviase
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Caligraphy solver = new Caligraphy();
        solver.solve(1, in, out);
        out.close();
    }

    static class Caligraphy {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            // long でchmin
            int h = in.nextInt();
            int w = in.nextInt();
            char[][] c = new char[h + 2][w + 2];
            Pair p = new Pair(0, 0);
            for (int i = 1; i <= h; i++) {
                String s = in.next();
                Arrays.fill(c[i], '0');
                for (int j = 0; j < w; j++) {
                    c[i][j + 1] = s.charAt(j);
                }
            }

            double res = 1e11;
            for (int i = 1; i <= h; i++) {
                double cmp = 0;
                for (int j = 1; j <= h; j++) {
                    for (int k = 1; k <= w; k++) {
                        if (c[j][k] == '1') {
                            cmp += Math.sqrt((i - j) * (i - j) + (0 - k) * (0 - k));
                        }
                    }
                }
                res = Math.min(res, cmp);
                cmp = 0;
                for (int j = 1; j <= h; j++) {
                    for (int k = 1; k <= w; k++) {
                        if (c[j][k] == '1') {
                            cmp += Math.sqrt((i - j) * (i - j) + (w + 1 - k) * (w + 1 - k));
                        }
                    }
                }
                res = Math.min(res, cmp);
            }

            for (int i = 1; i <= w; i++) {
                double cmp = 0;
                for (int j = 1; j <= h; j++) {
                    for (int k = 1; k <= w; k++) {
                        if (c[j][k] == '1') {
                            cmp += Math.sqrt((0 - j) * (0 - j) + (i - k) * (i - k));
                        }
                    }
                }
                res = Math.min(res, cmp);
                cmp = 0;
                for (int j = 1; j <= h; j++) {
                    for (int k = 1; k <= w; k++) {
                        if (c[j][k] == '1') {
                            cmp += Math.sqrt((h + 1 - j) * (h + 1 - j) + (i - k) * (i - k));
                        }
                    }
                }
                res = Math.min(res, cmp);
            }


            out.println(res);
        }

    }

    static class Pair {
        public long x;
        public long y;

        public Pair(long x, long y) {
            this.x = x;
            this.y = y;
        }

        public String toString() {
            return "Pair{" +
                    "x=" + x +
                    ", y=" + y +
                    '}';
        }

    }
}

0