UVa 769 - Magic of David Copperfield 解題報告(思維)

 Magic of David Copperfield 

The well-known magician David Copperfield loves to show the following trick: a square withN rows and N columns of different pictures appears on a TV screen. Let us number allthe pictures in the following order:

1 2 $\dots$ N
$\vdots$ $\vdots$ $\ddots$ $\vdots$
N*(N-1)+1 N*(N-1)+2 $\dots$ N*N

Each member of the audience is asked to put a finger on the upper left picture (i.e., picture numberone) and The Magic begins: the magician tells the audience to move the finger k1 times through thepictures (each move is a shift of the finger to the adjacent picture up, down, left or right providedthat there is a picture to move to), then with a slight movement of his hand he removes some of thepictures with an exclamation ``You are not there!", and ... it is true - your finger is not pointing toany of the pictures removed. Then again, he tells the audience to make k2 moves, and so on. At theend he removes all the pictures but one and smiling triumphantly declares, ``I've caught you"(applause).


Just now, David is trying to repeat this trick. Unfortunately, he had a hard day before, and youknow how hard to conjure with a headache. You have to write a program that will help David tomake his trick.

Input 

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

Each test case consists of a single integer number N ($2 \le N \le 100$).

Output 

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

Your program should write the following lines with numbers to the output file:


$k_1 \ x_{1,1} \ x_{1,2} \ \dots \ x_{1,m1}$

$k_2 \ x_{2,1} \ x_{2,2} \ \dots \ x_{2,m2}$

$\dots$

$k_e \ x_{e,1} \ x_{e,2} \ \dots \ x_{e,me}$


where ki is a number of moves the audience should make on the i-th turn ($2N \le k \le 10000$).All kishould be different (i.e. ki <> kj when i <> j). $x_{i,1}, x_{i,2}, \dots, x_{i,mi}$are the numbers of the picturesDavid should remove after the audience will make ki moves (the number of the pictures removed isarbitrary, but each picture should be listed only once, and at least one picture should be removed oneach turn).


A description of the every next turn should begin with a new line. All numbers on each line shouldbe separated by one or more spaces. After e iterations, all pictures except one should be removed.

Sample input 

1

3

Sample Output 

8     4 6
13    9
10    7 1
7     8
11    3 5

    解題報告: 很有意思的一題。給定一n*n矩陣,其中爲1到n*n所有數字。求一方法,每次任意走(上下左右)k步,刪除一些數字;直到只剩下一個數字。

    剛開始,我是這樣想的:正方形矩陣可以這樣分類可以按照(i+j)的奇偶性分爲兩組,這樣每次走奇數步,刪除邊界的一個數,直到刪除到最後一個數就可以了。

    寫完後WA了。再看一遍題意,發現要求k不能相同。ok,改下k,每次不同,+2。運行一遍,發現n=100時k超了10000……

    問題就來了,每次刪除一個數是不夠的。再想一方案,仍然每次走奇數步,每次刪除右下角的一整斜行數,A了。

    其實就是一思維題,細心好好想想就很容易了。代碼如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iomanip>
using namespace std;
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
typedef long long LL;
typedef unsigned long long ULL;
void work();
int main()
{
#ifdef ACM
    freopen("in.txt", "r", stdin);
#endif // ACM
    work();
}

/***************************************************/

void work()
{
    int T;
    scanf("%d", &T);

    fff(cas, 1, T)
    {
        int n;
        scanf("%d", &n);

        typedef vector<int> vint;
        map<int, vint> mint;

        fff(i, 1, n) fff(j, 1, n)
            mint[i+j].push_back((i-1)*n + j);

        int k = 199;
        dff(i, n+n, 3)
        {
            printf("%d", k+=2);
            for(vint::iterator it = mint[i].begin(); it != mint[i].end(); it++)
                printf(" %d", *it);
            puts("");
        }

        if(T-cas) puts("");
    }
}


發佈了227 篇原創文章 · 獲贊 13 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章