結果

問題 No.55 正方形を描くだけの簡単なお仕事です。
ユーザー DialBirdDialBird
提出日時 2017-02-11 10:02:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,778 bytes
コンパイル時間 787 ms
コンパイル使用メモリ 74,688 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-28 12:45:43
合計ジャッジ時間 2,020 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <sstream>
#include <vector>
#include <array>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>

using namespace std;

#define REP(i,first,last) for (int i=first;i<last;++i)
#define MAX(x,y) (x > y ? x : y)
#define MIN(x,y) (x < y ? x : y)

typedef pair<int, int> p_ii;

vector<p_ii> points;

int get_distance(p_ii p_1, p_ii p_2){
  int x = pow(p_1.first - p_2.first, 2);
  int y = pow(p_1.second - p_2.second, 2);
  return x + y;
}

int main(){
  for (int i=0;i<3;i++) {
    p_ii p;
    cin >> p.first;
    cin >> p.second;
    points.push_back(p);
  }
  
  // 最初の点を適当に決め、残り二つのうちから距離が近い方を二番目の点にする
  p_ii p_1 = points[0];
  int dis_1 = get_distance(points[0], points[1]);
  int dis_2 = get_distance(points[0], points[2]);
  p_ii p_2 = MIN(dis_1, dis_2) == dis_1 ? points[1] : points[2];

  // 2点間のxとyの差から、正方形になるための点候補を四つ挙げる
  int x_dif = p_2.first - p_1.first;
  int y_dif = p_2.second - p_1.second;
  p_ii p_3 = {p_1.first + y_dif, p_1.second - x_dif};
  p_ii p_4 = {p_1.first - y_dif, p_1.second + x_dif};
  p_ii p_5 = {p_2.first + y_dif, p_2.second - x_dif};
  p_ii p_6 = {p_2.first - y_dif, p_2.second + x_dif};

  // 残りの一点が候補内にあれば正方形ができる
  p_ii rest_p = p_2 == points[1] ? points[2] : points[1];
  if (rest_p == p_3) {
    cout << p_5.first << " " << p_5.second << endl;
  } else if (rest_p == p_4) {
    cout << p_6.first << " " << p_6.second << endl;
  } else if (rest_p == p_5) {
    cout << p_3.first << " " << p_3.second << endl;
  } else if (rest_p == p_6) {
    cout << p_4.first << " " << p_4.second << endl;
  } else {
    cout << -1 << endl;
  } 
}
0