当前位置: 代码迷 >> 综合 >> 暑假提高五-- J - Image Compression(递归)
  详细解决方案

暑假提高五-- J - Image Compression(递归)

热度:50   发布时间:2023-12-26 10:19:50.0

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2927

【分析】

  1. 题意大概就是给一个n×n的棋盘,有0和1,再给出一个百分数,如果整个棋盘0或1的数目达到这个数就把整个棋盘换成0或1;如果都没达到,就进行分块,均分4份,按照右上、左上、左下、右下来进行计数。
  2. 用到递归(代码参考:https://blog.csdn.net/waterboy_cj/article/details/81568774)。
#include <bits/stdc++.h>
using namespace std;
char gra[110][110];
int m;
void f(int x1, int x2, int y1, int y2)
{int cnt_0 = 0, cnt_1 = 0;for (int i = x1; i <= x2; i++)for (int j = y1; j <= y2; j++){if (gra[i][j] == '0')cnt_0++;else cnt_1++;}double temp = cnt_0 * 1.0 / double(cnt_0 + cnt_1) * 100.0 - m * 1.0;if (temp > 0 || fabs(temp) <= 0){for (int i = x1; i <= x2; i++)for (int j = y1; j <= y2; j++)gra[i][j] = '0';return;}temp = cnt_1 * 1.0 / double(cnt_0 + cnt_1) * 100.0 - m * 1.0;if (temp > 0 || fabs(temp) <= 0){for (int i = x1; i <= x2; i++)for (int j = y1; j <= y2; j++)gra[i][j] = '1';return;}f(x1, (x1 + x2) / 2, (y1 + y2) / 2 + 1, y2);//右上f(x1, (x1 + x2) / 2, y1, (y1 + y2) / 2);//左上f((x1 + x2) / 2 + 1, x2, y1, (y1 + y2) / 2);//左下f((x1 + x2) / 2 + 1, x2, (y1 + y2) / 2 + 1, y2);//右下
}int main()
{int n, ca = 0;while (~scanf("%d", &n) && n){scanf("%d", &m);for (int i = 0; i <= n - 1; i++)scanf("%s", gra[i]);f(0, n - 1, 0, n - 1);//第一次当然是对整张图啦printf("Image %d:\n", ++ca);for (int i = 0; i <= n - 1; i++)puts(gra[i]);}return 0;
}

 

  相关解决方案