結果

問題 No.707 書道
ユーザー lapilapi
提出日時 2019-06-15 20:40:13
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,497 bytes
コンパイル時間 1,037 ms
コンパイル使用メモリ 111,656 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-13 03:48:15
合計ジャッジ時間 1,611 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <numeric>
#include <regex>
#include <tuple>
#include <iomanip>
#include <math.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
#define MOD 1000000007 // 10^9 + 7
#define INF 1000000000 // 10^9
#define LLINF 1LL<<60

double dis(int a, int b, int x, int y) {
	return sqrt(double((a - x)*(a - x) + (b - y)*(b - y)));
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int H, W; cin >> H >> W;
	vector<P> V;
	for (int i = 1; i <= H; i++) {
		for (int j = 1; j <= W; j++) {
			char tmp; cin >> tmp;
			if (tmp == '1') V.push_back(P(i, j));
		}
	}

	double ans = INF;

	// j==0
	for (int i = 1; i <= H; i++) {
		double sum = 0;
		for (int k = 0; k < V.size(); k++) sum += dis(i, 0, V[k].first, V[k].second);
		ans = min(ans, sum);
	}

	// j == W+1
	for (int i = 1; i <= H; i++) {
		double sum = 0;
		for (int k = 0; k < V.size(); k++) sum += dis(i, W+1, V[k].first, V[k].second);
		ans = min(ans, sum);
	}

	// i == 0
	for (int j = 1; j <= W; j++) {
		double sum = 0;
		for (int k = 0; k < V.size(); k++) sum += dis(0, j, V[k].first, V[k].second);
		ans = min(ans, sum);
	}

	// i == H+1
	for (int j = 1; j <= W; j++) {
		double sum = 0;
		for (int k = 0; k < V.size(); k++) sum += dis(H+1, j, V[k].first, V[k].second);
		ans = min(ans, sum);
	}

	cout << setprecision(15) << ans << endl;


	return 0;
}
0