본문 바로가기
Computer Science

이미지 복원하기2

by OKOK 2019. 3. 15.


[ 주의 사항 ]

1. 본 검정은 C++ 언어만 지원한다.

2. Input 파일은 사용되지 않는다.

3. User Code 내 malloc.h 를 제외한 어떠한 헤더 파일도 추가할 수 없다.

4. User Code 내 전역 변수를 사용해서는 안 된다.
   제출된 코드에 전역 변수가 포함된 경우 실격으로 처리한다.


5. User Code 내 static 키워드를 사용해서는 안 된다.
   제출된 코드에 static 키워드가 포함된 경우 실격으로 처리한다.


6. Main 은 수정할 수 없으며, 실제 채점 시에도 그대로 사용된다.
   단, srand(3) 의 파라미터와 dummy1 ~ dummy5 변수의 크기는 변경된다.

7. 본 검정은 대단히 엄격한 코드 리뷰를 실시한다.
   어떠한 경우에도 원본 이미지를 저장하고 있거나 직접 접근을 시도해서는 안 된다.
   복원은 반드시 GRY[100][100] 데이터만을 이용해야 하며,

   그 외의 어떠한 시도도 모두 실격으로 처리한다.




▶ Main 을 분석하여 encode 와 decode 함수를 구현하라!

     RESULT 가 낮을수록 높은 점수를 부여한다. 


sample_user 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// do not use static keyword
// do not use global variable
 
 
void encode(char QRC[100][100], char SRC[100])
{
    // do not use static keyword
}
 
 
// do not use global variable
 
 
void decode(char GRY[100][100], char DST[100])
{
    // do not use static keyword
}
 
 
// do not use global variable
 
cs




sample_main 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <stdlib.h>
#include <memory.h>
#include <time.h>
 
 
using namespace std;
 
 
extern void encode(char QRC[100][100], char SRC[100]);
extern void decode(char GRY[100][100], char DST[100]);
 
 
static char QRC[1000][100][100];
    static char dummy1[12];  // the size will be changed
static char IMG[1000][200][200];
    static char dummy2[34];  // the size will be changed
static char GRY[1000][100][100];
    static char dummy3[56];  // the size will be changed
static char SRC[1000][100];
    static char dummy4[78];  // the size will be changed
static char DST[1000][100];
    static char dummy5[90];  // the size will be changed
 
 
void img2gray(int i)
{
    for (register int y = 0; y < 100; y++)
        for (register int x = 0; x < 100; x++)
        {
            GRY[i][y][x] = 0;
            GRY[i][y][x] += IMG[i][y * 2 + 0][x * 2 + 0];
            GRY[i][y][x] += IMG[i][y * 2 + 0][x * 2 + 1];
            GRY[i][y][x] += IMG[i][y * 2 + 1][x * 2 + 0];
            GRY[i][y][x] += IMG[i][y * 2 + 1][x * 2 + 1];
        }
}
 
 
int main(void)
{
    srand(3);  // the seed will be changed
 
    cout << "---------------" << endl;
 
    for (register int c = 0; c < 1000; c++)
        for (register int l = 0; l < 100; l++)
            SRC[c][l] = 'A' + (rand() % 26);
 
    char TMP[100];
    for (register int c = 0; c < 1000; c++)
    {
        memcpy(TMP, SRC[c], sizeof(SRC[0]));
        encode(QRC[c], TMP);
    }
 
    for (register int c = 0; c < 1000; c++)
        for (register int y = 0; y < 200; y++)
            for (register int x = 0; x < 200; x++)
                IMG[c][y][x] = rand() % 2 == 0 ? 0 : 1;
 
    for (register int c = 0; c < 1000; c++)
    {
        register int offsetx = rand() % 101;
        register int offsety = rand() % 101;
 
        for (register int y = 0; y < 100; y++)
            for (register int x = 0; x < 100; x++)
                IMG[c][offsety + y][offsetx + x] = QRC[c][y][x] == 0 ? 0 : 1;
    }
 
    for (register int c = 0; c < 1000; c++)
    {
        for (register int l = 0; l < 13000; l++)
            IMG[c][rand() % 200][rand() % 200= 1;
 
        img2gray(c);
    }
 
    clock_t TIME = clock();
 
    for (register int c = 0; c < 1000; c++)
        decode(GRY[c], DST[c]);
 
    TIME = (clock() - TIME) / (CLOCKS_PER_SEC / 1000);
 
    int FAIL = 0;
 
    for (register int c = 0; c < 1000; c++)
        FAIL += memcmp(SRC[c], DST[c], 100== 0 ? 0 : 1;
 
    cout << "RESULT : " << (FAIL * 10000 + TIME) << endl;
 
    return 0;
}
 
cs

 


eval_main 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <stdlib.h>
#include <memory.h>
#include <time.h>
 
 
using namespace std;
 
 
extern void encode(char QRC[100][100], char SRC[100]);
extern void decode(char GRY[100][100], char DST[100]);
 
 
static char QRC[1000][100][100];
    static char dummy1[90];
static char IMG[1000][200][200];
    static char dummy2[12];
static char GRY[1000][100][100];
    static char dummy3[34];
static char SRC[1000][100];
    static char dummy4[56];
static char DST[1000][100];
    static char dummy5[78];
 
 
void img2gray(int i)
{
    for (register int y = 0; y < 100; y++)
        for (register int x = 0; x < 100; x++)
        {
            GRY[i][y][x] = 0;
            GRY[i][y][x] += IMG[i][y * 2 + 0][x * 2 + 0];
            GRY[i][y][x] += IMG[i][y * 2 + 0][x * 2 + 1];
            GRY[i][y][x] += IMG[i][y * 2 + 1][x * 2 + 0];
            GRY[i][y][x] += IMG[i][y * 2 + 1][x * 2 + 1];
        }
}
 
 
int main(void)
{
    srand(3);  // the seed will be changed
 
    cout << "---------------" << endl;
 
    for (register int c = 0; c < 1000; c++)
        for (register int l = 0; l < 100; l++)
            SRC[c][l] = 'A' + (rand() % 26);
 
    char TMP[100];
    for (register int c = 0; c < 1000; c++)
    {
        memcpy(TMP, SRC[c], sizeof(SRC[0]));
        encode(QRC[c], TMP);
    }
 
    for (register int c = 0; c < 1000; c++)
        for (register int y = 0; y < 200; y++)
            for (register int x = 0; x < 200; x++)
                IMG[c][y][x] = rand() % 2 == 0 ? 0 : 1;
 
    for (register int c = 0; c < 1000; c++)
    {
        register int offsetx = rand() % 101;
        register int offsety = rand() % 101;
 
        for (register int y = 0; y < 100; y++)
            for (register int x = 0; x < 100; x++)
                IMG[c][offsety + y][offsetx + x] = QRC[c][y][x] == 0 ? 0 : 1;
    }
 
    for (register int c = 0; c < 1000; c++)
    {
        for (register int l = 0; l < 13000; l++)
            IMG[c][rand() % 200][rand() % 200= 1;
 
        img2gray(c);
    }
 
    clock_t TIME = clock();
 
    for (register int c = 0; c < 1000; c++)
        decode(GRY[999 - c], DST[c]);
 
    TIME = (clock() - TIME) / (CLOCKS_PER_SEC / 1000);
 
    int FAIL = 0;
 
    for (register int c = 0; c < 1000; c++)
        FAIL += memcmp(SRC[999 - c], DST[c], 100== 0 ? 0 : 1;
 
    cout << "RESULT : " << (FAIL * 10000 + TIME) << endl;
 
    return 0;
}
 
cs

 





 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
int ctob[26][5= {  
    /*{ 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 1 }, { 0, 0, 0, 1, 0 },*/ { 00011 },  
    /*{ 0, 0, 1, 0, 0 },*/ { 00101 }, { 00110 }, { 00111 },  
    /*{ 0, 1, 0, 0, 0 },*/ { 01001 }, { 01010 }, { 01011 },  
      { 01100 },   { 01101 }, { 01110 }, { 01111 },  
    /*{ 1, 0, 0, 0, 0 },*/ { 10001 }, { 10010 }, { 10011 },  
      { 10100 },   { 10101 }, { 10110 }, { 10111 },  
      { 11000 },   { 11001 }, { 11010 }, { 11011 },  
      { 11100 },   { 11101 }, { 11110 }, { 11111 } };  
  
char btoc[33= "000A0BCD0EFGHIJK0LMNOPQRSTUVWXYZ";  
  
inline void mark0(char QRC[100][100], int y, int x) {  
    QRC[y][x] = QRC[y][x + 1= QRC[y][x + 2=  
    QRC[y + 1][x] = QRC[y + 1][x + 1= QRC[y + 1][x + 2=   
    QRC[y + 2][x] = QRC[y + 2][x + 1= QRC[y + 2][x + 2= 0;  
}  
  
void encode(char QRC[100][100], char SRC[100]) {  
    for (int i = 0; i < 100++i)  
        for (int k = 0; k < 100++k)  
            QRC[i][k] = 1;  
  
    int y = 3;  
    int x = 3;  
    for (int i = 0; i < 100++i) {  
        int num = SRC[i] - 'A';  
        for (int k = 0; k < 5++k) {  
            if (ctob[num][k] == 0)  
                mark0(QRC, y, x);  
            x += 4;  
            if (x > 95) {  
                x = 3;  
                y += 4;  
            }  
        }  
    }  
}  
  
int find_marker_y(char GRY[100][100], int sy, int sx) {  
    int cnt = 0;  
    for (int y = sy; y < 100++y) {  
        if (GRY[y][sx] == 4)  
            cnt++;  
        else  
            return 0;  
        if (cnt == 49)  
            return 1;  
    }  
    return 0;  
}  
  
void find_marker(char GRY[100][100], int *my, int *mx) {  
    *mx = *my = -1;  
    for (int y = 0; y <= 50++y) {  
        int cnt = 0;  
        int sx = -1;  
        for (int x = 0; x < 100++x) {  
            if (GRY[y][x] == 4) {  
                cnt++;  
                if (sx == -1)  
                    sx = x;  
                if (cnt == 49) {  
                    if (find_marker_y(GRY, y, sx) == 1) {  
                        *mx = sx;  
                        *my = y;  
                        return;  
                    }  
                    sx++;  
                    cnt--;  
                }  
            } else {  
                cnt = 0;  
                sx = -1;  
                if (x > 50)  
                    break;  
            }  
        }  
    }  
}  
  
int get(char GRY[100][100], int y, int x) {  
    int num = GRY[y][x] + GRY[y + 1][x] + GRY[y][x + 1+ GRY[y + 1][x + 1];  
    if (num == 16)  
        return 1;  
    return 0;  
}  
  
void decode(char GRY[100][100], char DST[100]) {  
    int mx, my;  
    find_marker(GRY, &my, &mx);  
  
    if (mx == -1)  
        return;  
    mx++;  
    my++;  
  
    int x = mx;  
  
    for (int i = 0; i < 100++i) {  
        int num = 0;  
        for (int k = 0; k < 5++k) {  
            int bit = get(GRY, my, x);  
            num = num * 2 + bit;  
            x += 2;  
            if (x > mx + 47) {  
                x = mx;  
                my += 2;  
            }  
        }  
  
        DST[i] = btoc[num];  
    }  
}
 
cs

 



'Computer Science' 카테고리의 다른 글

블록 조립  (0) 2019.03.15
Photo  (0) 2019.03.15
루빅스 큐브  (0) 2019.03.15
expert 제목 및 팁  (0) 2019.03.13
Disjoint-set Union-Find  (0) 2019.03.13

댓글