본문 바로가기
Computer Science

8 Problem A : Bit_ImageMap1

by OKOK 2020. 2. 10.

Problem A : Bit_ImageMap1

 

제한시간: 0 ms    메모리제한: 0 MB

 

 

가로 세로 각각 256개의 bit로 이루어진 imagemap이 있다.
 
unsigned char imagemap[256][32];
 
이 imagemap 내에 0 ~ 14 사이의 정수 4096개를 저장하는 encode() 함수를 설계하라.
 
반환된 imagemap 이미지는 전송 과정에서 노이즈에 의해 일부 bit가 임의의 0 또는 1로 변경될 수 있고,
90, 180, 270도로 회전될 수도 있으며, 가로 또는 세로 방향 대칭으로 변경될 수 있다.
 
이 imagemap 데이터를 분석하여, 최초 저장된 4096개 정수를 반환하는 decode() 함수를 작성하라.

 

//--------------main.cpp-----------------​

 

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <memory.h>

 

extern void encode(unsigned char imagemap[256][32], unsigned char data[4096]);

extern void decode(unsigned char data[4096], unsigned char imagemap[256][32]);

 

static void setpixel(unsigned char imagemap[256][32], int x, int y, int pixel)

{

    unsigned char mask = 0x80 >> (x % 8);

 

    if (pixel == 0) imagemap[y][x / 8&= ~mask;

    else imagemap[y][x / 8|= mask;

}

 

static int getpixel(unsigned char imagemap[256][32], int x, int y)

{

    unsigned char mask = 0x80 >> (x % 8);

 

    if ((imagemap[y][x / 8& mask) == 0return 0;

    else return 1;

}

 

static void noise(unsigned char imagemap[256][32])

{

    for (register int y = 0; y < 256; y += 2)

    for (register int x = 0; x < 256; x += 2)

    {

        setpixel(imagemap, x + rand() % 2, y + rand() % 20);

        setpixel(imagemap, x + rand() % 2, y + rand() % 21);

    }

}

 

static void rotate(unsigned char imagemap[256][32], int angle)

{

    static unsigned char buffer[256][32];

 

    if (angle != 0)

    {

        switch (angle)

        {

        case 1// 90

            for (register int y = 0; y < 256; y++)

            for (register int x = 0; x < 256; x++)

                setpixel(buffer, x, y, getpixel(imagemap, y, 255 - x));

            break;

        case 2// 180

            for (register int y = 0; y < 256; y++)

            for (register int x = 0; x < 256; x++)

                setpixel(buffer, x, y, getpixel(imagemap, 255 - x, 255 - y));

            break;

        case 3// 270

            for (register int y = 0; y < 256; y++)

            for (register int x = 0; x < 256; x++)

                setpixel(buffer, x, y, getpixel(imagemap, 255 - y, x));

            break;

        }

 

        for (register int y = 0; y < 256; y++)

        for (register int x = 0; x < 256; x++)

            setpixel(imagemap, x, y, getpixel(buffer, x, y));

    }

}

 

static void flip(unsigned char imagemap[256][32], int mode)

{

    static unsigned char buffer[256][32];

 

    if (mode != 0)

    {

        switch (mode)

        {

        case 1// horizontal

            for (register int x = 0; x < 256; x++)

            for (register int y = 0; y < 256; y++)

                setpixel(buffer, x, y, getpixel(imagemap, x, 255 - y));

            break;

        case 2// vertical

            for (register int y = 0; y < 256; y++)

            for (register int x = 0; x < 256; x++)

                setpixel(buffer, x, y, getpixel(imagemap, 255 - x, y));

            break;

        }

 

        for (register int y = 0; y < 256; y++)

        for (register int x = 0; x < 256; x++)

            setpixel(imagemap, x, y, getpixel(buffer, x, y));

    }

}

 

int main(void)

{

    static const int TCSIZE = 1024;

 

    static unsigned char data[TCSIZE][4096= { 0 };

    static unsigned char org[TCSIZE][4096= { 0 };

    static unsigned char result[TCSIZE][4096= { 0 };

    static unsigned char imagemap[TCSIZE][256][32= { 0 };

 

    srand(3); // The seed will be changed.

 

    for (register int TC = 0; TC < TCSIZE; TC++)

    for (register int c = 0; c < 4096; c++)

        org[TC][c] = data[TC][c] = rand() % 15;

 

    int PERFORMANCE = 10000;

 

    time_t start = clock();

    for (register int TC = 0; TC < TCSIZE; TC++)

        encode(imagemap[TC], data[TC]);

    PERFORMANCE -= (clock() - start) / (CLOCKS_PER_SEC / 1000);

 

    for (register int TC = 0; TC < TCSIZE; TC++)

    {

        noise(imagemap[TC]);

        rotate(imagemap[TC], rand() % 4);

        flip(imagemap[TC], rand() % 3);

    }

 

    start = clock();

    for (register int TC = 0; TC < TCSIZE; TC++)

        decode(result[TC], imagemap[TC]);

    PERFORMANCE -= (clock() - start) / (CLOCKS_PER_SEC / 1000);

 

    int PASS = 0;

 

    for (register int TC = 0; TC < TCSIZE; TC++)

    if (memcmp(org[TC], result[TC], 4096== 0) PASS++;

 

    printf("SCORE: %d\n", PASS * 10000 + PERFORMANCE);

 

    return 0;

}

 

 

//--------------user.cpp-----------------​

 

void encode(unsigned char imagemap[256][32], unsigned char data[4096])

{

}

 

void decode(unsigned char data[4096], unsigned char imagemap[256][32])

{

}

 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <memory.h>

extern void encode(unsigned char imagemap[256][32], unsigned char data[4096]);
extern void decode(unsigned char data[4096], unsigned char imagemap[256][32]);

static void setpixel(unsigned char imagemap[256][32], int x, int y, int pixel)
{
	unsigned char mask = 0x80 >> (x % 8);

	if (pixel == 0) imagemap[y][x / 8] &= ~mask;
	else imagemap[y][x / 8] |= mask;
}

static int getpixel(unsigned char imagemap[256][32], int x, int y)
{
	unsigned char mask = 0x80 >> (x % 8);

	if ((imagemap[y][x / 8] & mask) == 0) return 0;
	else return 1;
}

static void noise(unsigned char imagemap[256][32])
{
	for (register int y = 0; y < 256; y += 2)
	for (register int x = 0; x < 256; x += 2)
	{
		setpixel(imagemap, x + rand() % 2, y + rand() % 2, 0);
		setpixel(imagemap, x + rand() % 2, y + rand() % 2, 1);
	}
}

static void rotate(unsigned char imagemap[256][32], int angle)
{
	static unsigned char buffer[256][32];

	if (angle != 0)
	{
		switch (angle)
		{
		case 1: // 90
			for (register int y = 0; y < 256; y++)
			for (register int x = 0; x < 256; x++)
				setpixel(buffer, x, y, getpixel(imagemap, y, 255 - x));
			break;
		case 2: // 180
			for (register int y = 0; y < 256; y++)
			for (register int x = 0; x < 256; x++)
				setpixel(buffer, x, y, getpixel(imagemap, 255 - x, 255 - y));
			break;
		case 3: // 270
			for (register int y = 0; y < 256; y++)
			for (register int x = 0; x < 256; x++)
				setpixel(buffer, x, y, getpixel(imagemap, 255 - y, x));
			break;
		}

		for (register int y = 0; y < 256; y++)
		for (register int x = 0; x < 256; x++)
			setpixel(imagemap, x, y, getpixel(buffer, x, y));
	}
}

static void flip(unsigned char imagemap[256][32], int mode)
{
	static unsigned char buffer[256][32];

	if (mode != 0)
	{
		switch (mode)
		{
		case 1: // horizontal
			for (register int x = 0; x < 256; x++)
			for (register int y = 0; y < 256; y++)
				setpixel(buffer, x, y, getpixel(imagemap, x, 255 - y));
			break;
		case 2: // vertical
			for (register int y = 0; y < 256; y++)
			for (register int x = 0; x < 256; x++)
				setpixel(buffer, x, y, getpixel(imagemap, 255 - x, y));
			break;
		}

		for (register int y = 0; y < 256; y++)
		for (register int x = 0; x < 256; x++)
			setpixel(imagemap, x, y, getpixel(buffer, x, y));
	}
}

int main(void)
{
	static const int TCSIZE = 1;

	static unsigned char data[TCSIZE][4096] = { 0 };
	static unsigned char org[TCSIZE][4096] = { 0 };
	static unsigned char result[TCSIZE][4096] = { 0 };
	static unsigned char imagemap[TCSIZE][256][32] = { 0 };

	srand(3); // The seed will be changed.

	for (register int TC = 0; TC < TCSIZE; TC++)
	for (register int c = 0; c < 4096; c++)
		org[TC][c] = data[TC][c] = rand() % 15;

	int PERFORMANCE = 10000;

	time_t start = clock();
	for (register int TC = 0; TC < TCSIZE; TC++)
		encode(imagemap[TC], data[TC]);
	PERFORMANCE -= (clock() - start) / (CLOCKS_PER_SEC / 1000);

	for (register int TC = 0; TC < TCSIZE; TC++)
	{
		noise(imagemap[TC]);
		rotate(imagemap[TC], rand() % 4);
		flip(imagemap[TC], rand() % 3);
	}

	start = clock();
	for (register int TC = 0; TC < TCSIZE; TC++)
		decode(result[TC], imagemap[TC]);
	PERFORMANCE -= (clock() - start) / (CLOCKS_PER_SEC / 1000);

	int PASS = 0;

	for (register int TC = 0; TC < TCSIZE; TC++)
	if (memcmp(org[TC], result[TC], 4096) == 0) PASS++;

	printf("SCORE: %d\n", PASS * 10000 + PERFORMANCE);

	return 0;
}

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

#include <stdio.h>
void set(unsigned char imagemap[256][32], int x, int y, int pixel)
{
	unsigned char mask = 0x80 >> (x % 8);

	if (pixel == 0) imagemap[y][x / 8] &= ~mask;
	else imagemap[y][x / 8] |= mask;
}

int get(unsigned char imagemap[256][32], int x, int y)
{
	unsigned char mask = 0x80 >> (x % 8);

	if ((imagemap[y][x / 8] & mask) == 0) return 0;
	else return 1;
}

void myrotate(unsigned char imagemap[256][32], int angle)
{
	static unsigned char buffer[256][32];

	if (angle != 0)
	{
		switch (angle)
		{
		case 1: // 90
			for (register int y = 0; y < 256; y++)
			for (register int x = 0; x < 256; x++)
				set(buffer, x, y, get(imagemap, y, 255 - x));
			break;
		case 2: // 180
			for (register int y = 0; y < 256; y++)
			for (register int x = 0; x < 256; x++)
				set(buffer, x, y, get(imagemap, 255 - x, 255 - y));
			break;
		case 3: // 270
			for (register int y = 0; y < 256; y++)
			for (register int x = 0; x < 256; x++)
				set(buffer, x, y, get(imagemap, 255 - y, x));
			break;
		}

		for (register int y = 0; y < 256; y++)
		for (register int x = 0; x < 256; x++)
			set(imagemap, x, y, get(buffer, x, y));
	}
}

void myflip(unsigned char imagemap[256][32])
{
	static unsigned char buffer[256][32];

	for (register int y = 0; y < 256; y++)
	for (register int x = 0; x < 256; x++)
		set(buffer, x, y, get(imagemap, 255 - x, y));

	for (register int y = 0; y < 256; y++)
	for (register int x = 0; x < 256; x++)
		set(imagemap, x, y, get(buffer, x, y));
}

int search(unsigned char imagemap[256][32])
{
	int cnt[8] = { 0 }, pattern = 0, i, j, k;
	int dy[8] = { 0, 129, 193, 64, 0, 129, 193, 64 }, dx[8] = { 64, 0, 129, 193, 129, 193, 64, 0 };
	for (i = 0; i < 64; i += 2) for (j = 0; j < 64; j += 2) {
		for (k = 0; k < 8; k++) cnt[k] += get(imagemap, j + dx[k], i + dy[k]);
	}
	/*
	for (i = 0; i < 64; i += 2) for (j = 0; j < 64; j += 2) {
	cnt[0] += get(imagemap, j + 64, i);
	cnt[1] += get(imagemap, j, i + 129);
	cnt[2] += get(imagemap, j + 129, i + 193);
	cnt[3] += get(imagemap, j + 193, i + 64);
	cnt[4] += get(imagemap, j + 129, i);
	cnt[5] += get(imagemap, j + 193, i + 129);
	cnt[6] += get(imagemap, j + 64, i + 193);
	cnt[7] += get(imagemap, j, i + 64);
	}*/
	for (i = 1; i < 8; i++) if (cnt[pattern] < cnt[i]) pattern = i;
	return pattern;
}

void output(unsigned char imagemap[256][32])
{
	for (int i = 0; i < 256; i += 2) {
		for (int j = 0; j < 256; j += 2) {
			printf("%d", get(imagemap, j + j / 128, i + i / 128));
		}
		printf("\n");
	}
	printf("\n");
}

void encode(unsigned char imagemap[256][32], unsigned char data[4096])
{
	int i, j, r, c, bit, y = 0, x = 0;
	for (i = 0; i < 4096; i++) {
		for (j = 3; j >= 0; j--) {
			bit = (data[i] >> j) & 1;
			for (r = 0; r < 2; r++) for (c = 0; c < 2; c++) set(imagemap, x + c, y + r, bit);
			set(imagemap, x + x / 128, y + y / 128, 0);
			x += 2;
			if (x >= 256) x = 0, y += 2;
		}
	}
	for (i = 0; i < 64; i += 2) for (j = 64; j < 128; j += 2) set(imagemap, j, i, 1);
	//output(imagemap);
}

void decode(unsigned char data[4096], unsigned char imagemap[256][32])
{
	//output(imagemap);
	int pattern = search(imagemap);
	if (pattern >= 4) myflip(imagemap);
	myrotate(imagemap, pattern % 4);

	int i, j, r, c, k = 0, cnt;
	for (i = 0; i < 256; i += 2) for (j = 0; j < 256; j += 2) {
		cnt = 0;
		for (r = 0; r < 2; r++) for (c = 0; c < 2; c++)
			cnt += get(imagemap, j + c, i + r);
		cnt -= get(imagemap, j + j / 128, i + i / 128);
		data[k / 4] = data[k / 4] * 2 + cnt / 2;
		k++;
	}
}

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

dart  (0) 2020.05.03
파이썬 키움 api 사용  (0) 2020.03.25
5 Problem C : 루빅의 사각형  (0) 2020.02.04
5 Problem B : 최단경로  (0) 2020.02.04
5 Problem A : 지하철  (0) 2020.02.04

댓글