CSUOJ 1861 Unscrambling Images 直接模拟,题面有点难看

来源:互联网 发布:西安天睿软件 编辑:程序博客网 时间:2024/04/29 23:22

Description

    Quadtrees are commonly used for encoding digital images in a compact form. Given an n x n image (where n is a power of 2, 1 <= n <= 16 ), its quadtree encoding is computed as follows. Start with a quadtree with exactly one node, namely the root, and associate with this node the n x n square region for the entire image. Then the following is performed recursively: 

  1. If every pixel in the region associated with the current node has an intensity value of p, then the node is made a leaf and it is assigned an associated value of p. 
  2. Otherwise, four nodes are added as children of the current node. The region is divided into four equal (square) quadrants and each quadrant is assigned to one child node. The algorithm recurses on each of the children nodes.

    When the process terminates, we obtain a quadtree in which every internal node has four children. Every leaf node has an associated value representing the intensity of the region corresponding to the leaf node. An example of an image and its quadtree encoding is shown below. 
D1
D2
    We have assumed that the four children represents, from left to right, the upper left, upper right, lower left, and lower right quadrants, respectively.
    To easily identify a node in a quadtree, we assign a number to each node by the following rules: 

  • The root is numbered 0. 
  • If the number of a node is k, then its children, from left to right, are numbered 4k+1, 4k+2, 4k+3, 4k+4.

    Images encoded as quadtrees can be encrypted by a secret password as follows: whenever a subdivision is performed, the four branches are reordered. The reordering may be different at each node, and is completely determined by the password and the node number.
    Unfortunately, some people use the "save password" feature in the encoding program and use the same password for multiple images. By observing the encoding of a well-chosen test image, any image encoded with the same password can be decoded without the password. In this test image, each pixel has a distinct intensity from 0 to n^2-1 arranged from left-to-right, top-to-bottom in increasing order. An example for n = 16 is given below: 
D3
    You managed to gain access to the encoding program, and used it to encode the test image. Given this output, write a program to decode any other images encoded with the same password.

Input

    You will be given a number of cases in the input. The first line of input consists of a positive integer indicating the number of test cases to follow. Each test case starts with a line containing n, followed by the quadtree encoding of the test image and the quadtree encoding of the secret image to be decoded. Each quadtree encoding starts with a line containing a positive integer m indicating the number of leaf nodes in the tree. The next m lines are of the form 

k intensity

    which specifies that node number k is a leaf node with the specified intensity as the associated leaf value. Nodes not specified are either internal nodes or absent in the quadtree.
    You may assume that all intensities are between 0 and 255, inclusively. You may also assume that each quadtree encoding is a valid output obtained from the encoding algorithm described above.

Output

    For each test case, print the case number followed by a blank line. Then, print the intensities of the pixels of the decoded image one row at a time. Print each intensity right-justified in a field of width 4, with no extra spaces between fields. Insert a blank line between cases.

Sample Input

3241 32 23 04 141 232 1233 2534 404165 86 97 138 129 010 411 112 513 214 315 716 617 1018 1119 1520 1472 103 204 305 416 427 448 434165 86 97 138 129 010 411 112 513 214 315 716 617 1018 1119 1520 14102 2554 1405 406 407 428 4113 3014 3015 2816 28

Sample Output

Case 1 253  40 123  23Case 2  10  10  20  20  10  10  20  20  41  42  30  30  43  44  30  30Case 3 255 255  30  30 255 255  28  28  40  40 140 140  41  42 140 140

  说句老实话,题面看了我将近1H,还是自己菜

  题目的意思是定义了一个叫“四叉树”的数据结构,用来加密一张图片,对于图片上的每一个像素格都有一个value

  然后根据一张图来建立一个四叉树的递归步骤是:

     1、这棵树有一个根节点,编号0,来表示一整张图

     2、对于一个节点K表示的图,假若存在一个点使得这个点与其他的点的value不一样时,这个节点就要衍生出四个子节点,这四个子节点分别代表原图从中央按十字分割后的四块,从左到右分别代表左上角,右上角,左下角,右下角,其编号分别为4*K+1,4*K+2,4*K+3,4*K+4,否则该节点有一个value,其value等于其表示的图上的像素格的value

  想一想就可以知道,我们只要知道这颗四叉树的所有叶子节点和叶子节点上的值,就可以重构出原图。

  现在看题面,可以看到有一个图片,每一行都是按照递增顺序并且中间还有省略号的那一张图,记做G。

  然后任务是:

  先告诉你一颗四叉树,这颗四叉树复原为图后是图G的一个重排,也就是把图G打乱了,这张乱图记做H

  然后告诉你第二颗四叉树,表示待重排的图I

  因为H是G的一个重排,那么我们可以不断的交换使得H变回G,现在要对I做同样的交换,输出I交换后的图。

  或许我的表述可能让你思考起来有点麻烦,不过题目描述的确实是这样一个意思。

  开一个新数组储存和G在同样位置上的I的格子的值,然后按顺序输出即可。

  其实代码很好写

#include <iostream>#include <cstring>#include <algorithm>#include <iomanip>#include <map>using namespace std;typedef pair<int,int> pa;int arr[300],raw[20][20],board[20][20],n,k,kcase;map<int,int> ma;void create(int arr[][20],pa le=make_pair(1,1),int len=n,int node=0);int main(){    ios_base::sync_with_stdio(false);    int times;    cin>>times;    while(times--){        cout<<"Case "<<++kcase<<endl<<endl;        cin>>n;        cin>>k;        for(int i=0,a,b;i<k;++i)            cin>>a>>b,ma.insert(make_pair(a,b));        create(raw);        ma.clear();        cin>>k;        for(int i=0,a,b;i<k;++i)            cin>>a>>b,ma.insert(make_pair(a,b));        create(board);        ma.clear();        for(int i=1;i<=n;++i)        for(int j=1;j<=n;++j)            arr[raw[i][j]]=board[i][j];        for(int i=1,tmp=0;i<=n;++i){            for(int j=1;j<=n;++j)                cout<<setw(4)<<arr[tmp++];            cout<<endl;        }        cout<<endl;    }    return 0;}void create(int arr[][20],pa le,int len,int node){    if(ma.find(node)!=ma.end()){        int tmp=ma[node];        for(int i=0;i<len;++i)        for(int j=0;j<len;++j)            arr[le.first+i][le.second+j]=tmp;    }else{        create(arr,le,(len>>1),(node<<2)+1);        create(arr,make_pair(le.first+(len>>1),le.second),(len>>1),(node<<2)+2);        create(arr,make_pair(le.first,(le.second)+(len>>1)),(len>>1),(node<<2)+3);        create(arr,make_pair(le.first+(len>>1),le.second+(len>>1)),(len>>1),(node<<2)+4);    }}


  


0 0
原创粉丝点击