递归调用

Image Compression

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 29   Solved: 21
[ Submit][ Status][ Web Board]

Description

Strategies for compressing two-dimensional images are often based on finding regions with high similarity. In this problem, we explore a particular approach based on a hierarchical decomposition of the image. For simplicity, we consider only bitmapped images such as the following:

The image is encoded as a tree, with the root representing the entire image region. If a region is monochromatic, then the node for that region is a leaf storing the color of the region. Otherwise, the region is divided into four parts about its center, and the approach is applied recursively to each quadrant. For a non-leaf node, its four children represent the four quadrants ordered as upper-right, upper-left, lower-left, lower-right respectively. As an example, here is the tree encoding of the above image.

The original image is not monochromatic, so we considered the four quadrants. The top-right quadrant is monochromatic white, so the first child of the root node is a leaf with value 0. The top-left quadrant is not monochromatic, so it is further divided into four subquadrants, each of which is trivially monochromatic. This results in the subtree with leaf values 0, 0, 1, 0. The final two quadrants are monochromatic with respective values 0 and 1.

As a larger example, here is an 8x8 image and the tree encoding of it.

 

Thus far we have described a lossless compression scheme, but the approach can be used for lossy compression with the following adjustment. Instead of continuing the decomposition until reaching a monochromatic region, a threshold such as 75% is used, and a leaf is created whenever a region has at least that percentage of either color. As an example, here is the encoding of the above 8x8 image if using 75% as the threshold.

Notice that 75% of the top-left quadrant of the full image is black, and therefore the second child of the root is 1, and that more than 75% of the bottom-left quadrant of the full image is white, and therefore the third child of the root is 0. However, neither white nor black reaches 75% in the top-right quadrant, so the recursive decomposition continues, but all four of those subquadrants achieve the 75% threshold and become leaves. If we were to uncompress the image based on this new lossy encoding, we get back the following result.

Your goal is to determine the image that results from this lossy compression scheme, given an original bitmap image and a specific threshold percentage.

Input

The input will consist of a series of data sets, followed by a line containing only 0. Each data set begins with a line containing values W and T, where W is the width of the bitmap and T is the threshold percentage. Images will always be square with 1 ≤ W ≤ 64 being a power of two. Threshold T will be an integer with51 ≤ T ≤ 100. Following the specification of W and T are W additional lines, each of which is a string of width W containing only characters 0 and 1, representing a row of the image bitmap, from top to bottom.

Output

For each data set, you should print an initial line of the form "Image 1:" numbering the images starting with 1. Following that should be W lines, with each line representing a row of the resulting bitmap as a string of characters 0 and 1, from top to bottom.

Sample Input

4 80
0000
1000
0011
0011
8 75
11111000
11110000
11000011
11000011
11000100
00000100
00010011
00010011
4 75
1101
1111
0111
0011
0

Sample Output

Image 1:
0000
1000
0011
0011
Image 2:
11110000
11110000
11110011
11110011
00000100
00000100
00000011
00000011
Image 3:
1111
1111
1111
1111

这题题目描述很长,群赛的时候没敢做,赛后才做这题,1A好开心……明显的递归调用,就是我的手速太慢,调试了半天
View Code
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
const int N=65;
bool map[N+10][N+10];
int num[N+10][N+10],n;
double per;
void dfs(int sx,int sy,int width)
{
if(width<=1) return;
int n1=num[sx+width-1][sy+width-1]-num[sx+width-1][sy-1]-num[sx-1][sy+width-1]+num[sx-1][sy-1];
int tot=width*width;
// cout<<sx<<" "<<sy<<" "<<n1<<" "<<tot<<endl;
if(n1*100>=per*tot)
{
// puts("yes1");
for(int i=0;i<width;i++)
for(int j=0;j<width;j++)
map[i+sx][sy+j]=1;
return;
}
if((tot-n1)*100>=per*tot)
{
for(int i=0;i<width;i++)
for(int j=0;j<width;j++)
map[i+sx][sy+j]=0;
return;
}
dfs(sx,sy,width/2);
dfs(sx,sy+width/2,width/2);
dfs(sx+width/2,sy,width/2);
dfs(sx+width/2,sy+width/2,width/2);
}
int main()
{
char cc;
int ca=1;
while(scanf("%d",&n)==1&&n)
{
cin>>per;
printf("Image %d:\n",ca++);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
cin>>cc;
if(cc=='0') map[i][j]=0;
else map[i][j]=1;
}
memset(num,0,sizeof(num));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
num[i][j]=num[i][j-1]+num[i-1][j]+map[i][j]-num[i-1][j-1];
}
dfs(1,1,n);
// cout<<endl<<endl;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
printf("%d",map[i][j]);
puts("");
}

}
return 0;
}


转载于:https://www.cnblogs.com/one--world--one--dream/archive/2012/03/31/2427084.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值