結果

問題 No.707 書道
ユーザー tnakao0123tnakao0123
提出日時 2018-07-01 01:58:50
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,403 bytes
コンパイル時間 1,552 ms
コンパイル使用メモリ 82,728 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-13 16:27:43
合計ジャッジ時間 1,405 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 707.cc: No.707 書道 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_H = 50;
const int MAX_W = 50;
const int MAX_N = MAX_H * MAX_W;
const double DINF = 1.0e60;

/* typedef */

/* global variables */

char s[MAX_W + 2];
int xs[MAX_N], ys[MAX_N];

/* subroutines */

double dsum(int n, int x0, int y0) {
  double sum = 0.0;
  for (int i = 0; i < n; i++) {
    int dx = x0 - xs[i], dy = y0 - ys[i];
    sum += sqrt(dx * dx + dy * dy);
  }
  return sum;
}

inline void setmin(double &a, double b) { if (a > b) a = b; }

/* main */

int main() {
  int h, w;
  scanf("%d%d", &h, &w);

  int n = 0;
  for (int y = 1; y <= h; y++) {
    scanf("%s", s);
    for (int x = 1; x <= w; x++)
      if (s[x - 1] == '1') xs[n] = x, ys[n] = y, n++;
  }
  //printf("n=%d\n", n);

  double mind = DINF;
  for (int x = 1; x <= w; x++) {
    setmin(mind, dsum(n, x, 0));
    setmin(mind, dsum(n, x, h + 1));
  }
  for (int y = 1; y <= h; y++) {
    setmin(mind, dsum(n, 0, y));
    setmin(mind, dsum(n, w + 1, y));
  }

  printf("%.8lf\n", mind);
  return 0;
}
0