結果
| 問題 | No.707 書道 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2018-06-29 22:35:23 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 7 ms / 2,000 ms | 
| コード長 | 1,044 bytes | 
| コンパイル時間 | 1,682 ms | 
| コンパイル使用メモリ | 169,928 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-06-30 23:59:30 | 
| 合計ジャッジ時間 | 2,011 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 6 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
double calc(int x, int y, int H, int W, vector<string>& p) {
    double res = 0;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (p[i][j] == '0') continue;
            int x2 = j + 1, y2 = i + 1;
            res += hypot((x - x2), (y - y2));
        }
    }
    return res;
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int H, W;
    cin >> H >> W;
    vector<string> p(H);
    for (int i = 0; i < H; i++) {
        cin >> p[i];
    }
    double ans = 1e15;
    for (int i = 0; i < H; i++) {
        int y = i + 1;
        int x = 0;
        ans = min(ans, calc(x, y, H, W, p));
        x = W + 1;
        ans = min(ans, calc(x, y, H, W, p));
    }
    for (int j = 0; j < W; j++) {
        int x = j + 1;
        int y = 0;
        ans = min(ans, calc(x, y, H, W, p));
        y = H + 1;
        ans = min(ans, calc(x, y, H, W, p));
    }
    cout << fixed << setprecision(15) << ans << endl;
    return 0;
}
            
            
            
        