跳到主要內容

[Training] UVA 1595 Symmetry

本題為 Brute Force 可以迅速解題。
題目給予一些二維座標,求是否所有點左右對稱。

看似簡單,但需要注意以下幾點,不然很容易拖掉 Debug 時間。

1.   scanf 讀取輸入的時候,遇到自定義的 struct,不必遲疑,直接把 “&” 寫在前面:

         
    scanf("%d %d", &a[i].x, &a[i].y);

2.   取 x 最大值和最小值,相加當作中點,不需要除以 2,避免出現浮點數問題。

3.   x_min 和 x_max 取完,要馬上回歸初始值。x_min 初始值要是很大的值,x_max 初始值是很小的值。

4.   要考慮座標有可能落在對稱線上,for 迴圈掃過所有點,某一點自己也可以跟自己取中點來做判斷。


完整程式碼:
// This code is created by EdocZec on 2020/01/25 #include <bits/stdc++.h> #define _for(i,a,b) for(int i=(a); i<(b); ++i) // this has been verified and can work correctly. #define _dbg(arr,n) _for(i,0,n){cout << " [Debug] arr[" << i << "] = " << (arr)[i] << endl;} // this has been verified and can work correctly. using namespace std; struct point { int x; int y; }; point a[4000]; int main(int argc, char** argv) { int case_num; scanf("%d", &case_num); _for(i,0,case_num) { int num, x_min = 10001, x_max = -10001, center = 0; scanf("%d", &num); _for(i,0,num) { scanf("%d %d", &a[i].x, &a[i].y); //cout << " [Debug] read a[i].x == " << a[i].x << endl; if((a[i].x) > x_max) x_max = a[i].x; if((a[i].x) < x_min) x_min = a[i].x; a[i].x = a[i].x; a[i].y = a[i].y; } // cout << " [Debug] x_max == " << x_max << endl; // cout << " [Debug] x_min == " << x_min << endl; center = (x_max + x_min); // cout << " [Debug] center == " << center << endl; x_min = 10001; x_max = -10001; // judge bool hit = false; _for(i,0,num) { hit = false; _for(j,0,num) { // cout << " [Debug] a[i].x == " << a[i].x << endl; // cout << " [Debug] a[j].x == " << a[j].x << endl; if( ((a[i].x + a[j].x) == center ) && (a[i].y == a[j].y) ) { hit = true; } } if(!hit) { // cout << " [Debug] not hit." << endl; // cout << " [Debug] a[i].x == " << a[i].x << endl; cout << "NO" << endl; break; } } if(hit) cout << "YES" << endl; } // for each case. return 0; } // above ok, updated on 2020/01/25

留言

這個網誌中的熱門文章

[Matrix] 矩陣與向量乘積

最近對線性代數應用感興趣,開始研究線性代數運算的底層程式實作。 先從矩陣乘積開始。 在此用 C++ 做矩陣與向量的乘積。 我們定義以下的矩陣 Class: template <class T, size_t, size_t j> class mtx { public: mtx(){} ~mtx(){} inline T& operator()(size_t i, size_t j) { return arr_[i][j]; } void info() { cout << endl; for(size_t i = 0; i < ROWS; i++) { for(size_t j = 0; j < COLS; j++) cout << " " << arr_[i][j]; cout << endl; } cout << endl; } private: T arr_[ROWS][COLS]; }; 接著我們就可以使用此 Class 來做矩陣與向量乘積: mtx a; mtx x; mtx b; a(0, 0) = 2; a(0, 1) = 1; a(1, 0) = 3; a(1, 1) = 5; a(2, 0) = 4; a(2, 1) = 7; x(0, 0) = 10; x(1, 0) = 12; // matrix mul: for(size_t r = 0; r < 3; r++) b(r, 0) = 0; for(size_t i = 0; i < 3; i+...

[Linux] Elementary OS 字體調校

用 gesetting 取得 elementary os 的等寬字體(這也是終端機默認字體): gsettings get org.gnome.desktop.interface monospace-font-name 會顯示目前字型跟字體大小: Roboto Mono 10 設定字體大小: gsettings set org.gnome.desktop.interface monospace-font-name 'Roboto Mono 12' 可以微調 text-scaling-factor: gsettings set org.gnome.desktop.interface text-scaling-factor <value>

[程式競賽] UVa 572, Oil Deposits,Flood Fill 演算法

原題目簡述如下: 以 m x n 大小的 grid 代表一張地圖,現今要在此地圖內探勘,找出油田。某一區塊如果標示 "@" 代表有油,"*" 代表沒有油。 "@" 相鄰的區域的聯集,可視為一個油田。(所謂相鄰,除了上下左右,斜向的相鄰也算進去) 求任意地圖中,油田的個數。 例如輸入的測資為: *    *   *    *  @ *   @  @  *  @ *   @   *   *  @ @ @  @   * @ @ @   *   *  @ 則油田個數為 2。 想法 採用典型的倒水演算法(Flood Fill),走訪 "@" 出現的區域,從此往下倒水,倒過水的區域標上 id,因此透過 id 的編號,可以得知油田的個數。 實作 先實作倒水演算法的子函式: void floodfill(vector<vector<char> >& map,                vector<vector<int>  >& id_table,                int row, int col, int id) {    if(row < 0 || (row >= map.size()) )   return;    if(col < 0 || (col >= map[0].size())) return;    if(map[row][col] != '@' || id_table[row][col] > 0) return;    id_table[row][col] = id;   ...