循環比賽

【NOIP2015T】 跳石頭

 問題描述
設有n個選手進行循環比賽,其中n = 2m,要求每名選手要與其餘n-1名選手都賽一次,每名選手天天比賽一次,循環賽共進行n - 1天,要求天天沒有選手輪空。c++

輸入格式
一個正整數m(2 <= n <= 6)算法

 輸出格式spa

樣例形式的比賽安排表code

樣例輸入:ip

3ci

樣例輸出:it

1 2 3 4 5 6 7 8class

2 1 4 3 6 5 8 7循環

4 3 2 1 8 7 6 5di

5 6 7 8 1 2 3 4

6 5 8 7 2 1 4 3

7 8 5 6 3 4 1 2

8 7 6 5 4 3 2 1

思索與實現:

起初是看不大懂樣例的,後來水哲告訴我看懂樣例就能作出來了。我約莫看懂樣例表述的含義後,恍然發現算法其實和他的含義沒有任蛤關係。

這個圖事實上是中心對稱。而且每個子方塊都知足中心對稱,由此能夠填出一半複製出另外一半。而且,在結構上右側的正方形與左側在相對結構上是徹底相同的(每一個位置+a)。

那麼只要每次拓展出右側矩陣,在中心對稱便可。話說題目給了2^n彷佛在暗示兩分處理。

Code:

#include <bits/stdc++.h>

using namespace std;

int n, m;
int match[110][110];
int mid = 1, k = 1;

int main(){
    freopen("match.in","r",stdin);
    freopen("match.out","w",stdout);
    cin >> m;
    n = 1 << m;
    match[1][1] = 1;
    do {
        for (int i = 1; i <= mid; i++)
            for (int j = 1; j <= mid; j++) {
                match[i][j + mid] = match[i][j] + mid;
            }
        for (int i = 1; i <= mid; i++)
            for (int j = 1; j <= mid; j++) {
                match[i + mid][j] = match[i][j + mid];
                match[i + mid][j + mid] = match[i][j];
            }
        k ++;
        mid *= 2;
    }
    while (k <= m);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++)
            cout << match[i][j] << " ";
            cout << endl;
    }
    return 0;
}
相關文章
相關標籤/搜索