본문 바로가기
Algorithms/simulation

7 Problem B : Bit_ImageMap2

by OKOK 2019. 11. 13.

영문 대문자 100개로 이루어진 STR배열

문자를 복원할 수 있도록 100*100 MAP 공간에 비트 형태로 저장하는 encode() 함수 설계

랜덤으로 생성된 200*200 IMG 이미지의 임의의 위치에 복사한 후,

노이즈 1을 1/3 확률로 넣음 그리고 4개의 bit씩 묶어서 그 합을 100*100 CNT 에 보관함

이 CNT 배열을 분석하여, 최초 저장된 STR 문자 배열을 복원하는 decode()함수 작성

 

- encode() : STR배열을 받아서 4*4을 1로 만드는 패딩을 만든다음에, 하나의 캐릭터를 받아서 3*5배열에 정상bit를 넣고 3*1을 1로 만들음

- 그리고 임의의 IMG 가 생성된 뒤에 오프셋된 위치에 encode를 통한 MAP을 붙이고 난 뒤

- 새로운 노이즈 1이 들어가게 됨

- 그리고 img_cnt()에 의해 CNT 배열을 얻게 됨

- CNT를 얻어서 검사를 진행하여 붙인 위치를 찾아냄 그리고 시작 위체에서 부터 다시 대문자 배열을 만들어 냄

 

- decode() 에서 k 변수의 사용처

 

/*


*/

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

using namespace std;
const int T = 1;
extern void encode(char MAP[100][100], char STR[100]);
extern void decode(char CNT[100][100], char USER[100]);

static char MAP[T][100][100];
static char IMG[T][200][200];
static char CNT[T][100][100];
static char STR[T][100];
static char USER[T][100];

void img_cnt(int i) {
	for (register int y = 0; y < 100; y++) {
		for (register int x = 0; x < 100; x++) {
			CNT[i][y][x] = 0;
			CNT[i][y][x] += IMG[i][y * 2 + 0][x * 2 + 0];
			CNT[i][y][x] += IMG[i][y * 2 + 0][x * 2 + 1];
			CNT[i][y][x] += IMG[i][y * 2 + 1][x * 2 + 0];
			CNT[i][y][x] += IMG[i][y * 2 + 1][x * 2 + 1];
		}
	}
}

int main(void)
{
	freopen("output.txt", "w", stdout);
	srand(7);
	for (register int c = 0; c < T; c++) {
		for (register int l = 0; l < 100; l++) {
			STR[c][l] = 'A' + (rand() % 26);
		}
	}

	char TMP[100];
	for (register int c = 0; c < T; c++) {
		memcpy(TMP, STR[c], sizeof(STR[0]));
		encode(MAP[c], TMP);
	}

	for (register int c = 0; c < T; 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 < T; 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] = MAP[c][y][x] == 0 ? 0 : 1; // map 100x100 임 
	}


	printf("IMG 200x200ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ\n");
	for (int i = 0; i < 200; i++) {
		for (int j = 0; j < 200; j++) {
			printf("%2d", IMG[0][i][j]);
		}
		printf("\n");
	}

	for (register int c = 0; c < T; c++) {
		for (register int l = 0; l < 13000; l++)
			IMG[c][rand() % 200][rand() % 200] = 1;
		img_cnt(c);
	}

	printf("noise IMG 200x200ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ\n");
	for (int i = 0; i < 200; i++) {
		for (int j = 0; j < 200; j++) {
			printf("%2d", IMG[0][i][j]);
		}
		printf("\n");
	}

	clock_t TIME = clock();

	for (register int c = 0; c < T; c++)
		decode(CNT[c], USER[c]);
	TIME = (clock() - TIME) / (CLOCKS_PER_SEC / T);
	int FAIL = 0;

	for (register int c = 0; c < T; c++) {
		FAIL += memcmp(STR[c], USER[c], 100) == 0 ? 0 : 1;
		printf("%d : %d\n", c, FAIL);
	}
	cout << (FAIL * 10000 + TIME) << endl;
	return 0;
}

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

#include <stdio.h>

void encode(char MAP[100][100], char STR[100])
{
	int i, j, k, r, c, bit, y = 4, x = 4;
	for (i = 0; i < 4; i++) {
		for (j = 0; j < 100; j++) {
			MAP[i][j] = MAP[j][i] = 1;
		}
	}

	for (i = 0; i < 100; i++) {
		for (j = 4; j >= 0; j--) {
			bit = ((STR[i] - 'A') >> j) & 1;
			for (r = 0; r < 3; r++) {
				for (c = 0; c < 5; c++) {
					MAP[y + r][x + c] = bit;
				}
				MAP[y + r][x + c] = 1;
			}
			x += 6;
			if (x >= 100) x = 4, y += 3;
		}
	}
	for (int i = 0; i < 100; i++) {
		for (int j = 0; j < 100; j++)
			printf("%2d", MAP[i][j]);
		printf("\n");
	}
}

void output(char cnt[100][100])
{
	printf("ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ\n");
	for (int i = 0; i < 100; i++) {
		for (int j = 0; j < 100; j++) {
			printf("%2d", cnt[i][j]);
		}
		printf("\n");
	}
}

void output2(int row[101][101])
{
	printf("ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ\n");
	for (int i = 0; i < 101; i++) {
		for (int j = 0; j < 101; j++) {
			printf("%2d", row[i][j]);
		}
		printf("\n");
	}
}

void decode(char CNT[100][100], char USER[100])
{
	output(CNT);
	
	int row[101][101] = { {0} }, col[101][101] = { {0} };
	int i, j, k = 0, sy, sx, bit, n = 0;
	for (i = 99; i >= 0; i--) {
		for (j = 99; j >= 0; j--) {
			if (CNT[i][j] == 4)
				row[i][j] = row[i][j + 1] + 1, col[i][j] = col[i + 1][j] + 1;
		}
	}

	output2(row);
	output2(col);

	for (i = 0; i < 100; i++) {
		for (j = 0; j < 100; j++) {
			if (row[i][j] >= 49 && col[i][j] >= 49)
				sy = i, sx = j;
		}
	}

	if (row[sy - 1][sx] >= 49 && col[sy - 1][sx] >= 49)
		k = 2;

	for (i = sy + 1; n < 500; i++) {
		if (k == 0) k++, i++;
		for (j = sx + 1; j < sx + 48 && n < 500; j += 3) {
			bit = 1;
			for (int c = 0; c < 3; c++) { // 이게 어떻게 noise 를 제거한 원복 데이터를 보장하는 것이지.
				if (CNT[i][j + c] < 4)
					bit = 0;
			}
			USER[n / 5] = (USER[n / 5] << 1) + bit;
			n++;
		}
		k = (k + 1) % 3;
	}
	for (i = 0; i < 100; i++) USER[i] += 'A';
}

'Algorithms > simulation' 카테고리의 다른 글

책꽂이 만들기  (0) 2020.01.09
5 Problem C : 루빅의 사각형  (0) 2019.11.13
7 Problem A : Bit_ImageMap1  (0) 2019.11.13
6 Problem B : 개미마을4  (0) 2019.11.12
6 Problem A : 개미마을3  (0) 2019.11.12

댓글