Code: Select all
#include <stdio.h>
#define MAXN 64
#define white 0
#define black 1
typedef int color;
color pixel[MAXN][MAXN]; /* for storing the image */
long sequence[MAXN * MAXN + 1];
char line[MAXN + 1]; /* for reading the input */
unsigned int size; /* saves the size of the sequence */
int n; /* size of the square */
void init() /* draws the image in white */
{
unsigned int i, j;
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
pixel[i][j] = white;
}
}
}
void swap(unsigned long *a, unsigned long *b)
{
unsigned long tmp = *a;
*a = *b;
*b = tmp;
}
void sort() /* prints the sequence in ascending order */
{
unsigned int i, j;
unsigned long imin;
for(i = 0; i < size - 1; i++) {
imin = i;
for(j = i + 1; j < size; j++) {
if (sequence[imin] > sequence[j]) {
imin = j;
}
}
if (imin != i) {
swap(&sequence[imin], &sequence[i]);
}
}
}
void printSequence()
{
unsigned int i, j, k = 0;
for(i = 0; i < (size / 12); i++) {
for(j = 0; j < 12; j++) {
printf("%ld ", sequence[k++]);
}
printf("\n");
}
if ((size % 12) == 0) return;
for(i = 0; i < (size % 12); i++) {
printf("%d ", sequence[k++]);
}
printf("\n");
}
void printImage()
{
unsigned int i, j;
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
printf("%c", pixel[i][j] == white ? '.' : '*' );
}
printf("\n");
}
}
unsigned long base5Tobase10(unsigned long num)
{
unsigned long new_num = 0;
unsigned long base = 1;
while (num != 0) {
new_num = new_num + (num % 10) * base;
base *= 5;
num /= 10;
}
return new_num;
}
unsigned long base10Tobase5(unsigned long num)
{
unsigned long new_num = 0;
unsigned long base = 1;
while (num != 0) {
new_num = new_num + base * (num % 5);
num = (num - (num % 5)) / 5;
base *= 10;
}
return new_num;
}
void descryptSequence()
{
unsigned int i;
for(i = 0; i < size; i++) {
sequence[i] = base10Tobase5(sequence[i]);
}
}
/* calculates the square to be filled black */
void calcSquare(int* x1, int* y1, int* x2, int* y2, unsigned long path)
{
while (path != 0) {
switch (path % 10) {
case 1: *x2 = (*x1 + *x2 - 1) / 2;
*y2 = (*y1 + *y2 - 1) / 2;
break;
case 2:
*x1 = (*x1 + *x2 + 1) / 2;
*y2 = (*y1 + *y2 - 1) / 2;
break;
case 3:
*y1 = (*y1 + *y2 + 1) / 2;
*x2 = (*x1 + *x2 - 1) / 2;
break;
case 4:
*x1 = (*x1 + *x2 + 1) / 2;
*y1 = (*y1 + *y2 + 1) / 2;
break;
}
path /= 10;
}
}
/* (x1, y1) (x2, y2) are the coordinates of the square */
/* fills the square black */
void fillSquare(int x1, int y1, int x2, int y2)
{
int i, j;
for(i = y1; i <= y2; i++) {
for(j = x1; j <= x2; j++) {
pixel[i][j] = black;
}
}
}
void drawImage() /* prints the image to the screen */
{
unsigned int i;
int x1, y1, x2, y2;
for(i = 0; i < size; i++) {
x1 = 0;
y1 = 0;
x2 = n - 1;
y2 = n - 1;
calcSquare(&x1, &y1, &x2, &y2, sequence[i]);
fillSquare(x1, y1, x2, y2);
}
}
/* checks if the square is one color on success returns in c the color */
int OneColor(int x1, int y1, int x2, int y2, color* c)
{
int i, j;
color square_color = pixel[y1][x1];
for(i = y1; i <= y2; i++) {
for(j = x1; j <= x2; j++) {
if (pixel[i][j] != square_color) {
return 0;
}
}
}
(*c) = square_color;
return 1;
}
void Image2Numbers(int x1, int y1, int x2, int y2, unsigned long path, unsigned long base)
{
color c;
if ( OneColor(x1, y1, x2, y2, &c) ) {
if (c == black) {
sequence[size] = base5Tobase10(path);
size++;
}
return;
}
if (base != 0) {
base *= 10;
}
else {
base = 1;
}
Image2Numbers(x1, y1, (x1 + x2 - 1) / 2, (y1 + y2 - 1) / 2, path + base * 1, base);
Image2Numbers((x1 + x2 + 1) / 2, y1, x2, (y1 + y2 - 1) / 2, path + base * 2, base);
Image2Numbers(x1, (y1 + y2 + 1) / 2, (x1 + x2 - 1) / 2, y2, path + base * 3, base);
Image2Numbers((x1 + x2 + 1) / 2, (y1 + y2 + 1) / 2, x2, y2, path + base * 4, base);
}
int main()
{
unsigned int i, j, case_num = 0;
scanf("%d", &n);
while (n != 0) {
if (n > 0) { /* convert image to sequence */
for(i = 0; i < n; i++) { /* get image */
scanf("%s", &line);
for(j = 0; j < n; j++) {
pixel[i][j] = (color)(line[j] - '0');
}
}
size = 0;
Image2Numbers(0, 0, n - 1, n - 1, 0, 0);
sort();
printf("Image %d\n", ++case_num);
printSequence();
printf("Total number of black nodes = %d\n", size);
}
else { /* n < 0 */
n = -n;
size = 0;
init();
while (1) {
scanf("%ld", &sequence[size++]);
if (sequence[size - 1] == -1) {
size--; /* we don't use the -1 */
descryptSequence();
printf("Image %d\n", ++case_num);
drawImage();
printImage();
break;
}
}
}
scanf("%d", &n);
if (n != 0) { /* end of input */
printf("\n");
}
}
return 0;
}
i read the input!
please try to help me here!!!!!!!!!!!!!!!!!
![:)](./images/smilies/icon_smile.gif)