洛谷P1320 压缩技术(续集版)

P1320 压缩技术(续集版)

题目描述

设某汉字由 N × N N \times N N×N 0 \texttt 0 0 1 \texttt 1 1 的点阵图案组成。

我们依照以下规则生成压缩码。连续一组数值:从汉字点阵图案的第一行第一个符号开始计算,按书写顺序从左到右,由上至下。第一个数表示连续有几个 0 \texttt 0 0,第二个数表示接下来连续有几个 1 \texttt 1 1,第三个数再接下来连续有几个 0 \texttt 0 0,第四个数接着连续几个 1 \texttt 1 1,以此类推……

例如: 以下汉字点阵图案:

0001000
0001000
0001111
0001000
0001000
0001000
1111111

对应的压缩码是: 7   3   1   6   1   6   4   3   1   6   1   6   1   3   7 \texttt {7 3 1 6 1 6 4 3 1 6 1 6 1 3 7} 7 3 1 6 1 6 4 3 1 6 1 6 1 3 7 (第一个数是 N N N ,其余各位表示交替表示0和1 的个数,压缩码保证 N × N = N \times N= N×N= 交替的各位数之和)

输入格式

汉字点阵图(点阵符号之间不留空格)。

输出格式

输出一行,压缩码。

输入输出样例 #1

输入 #1

0001000
0001000
0001111
0001000
0001000
0001000
1111111

输出 #1

7 3 1 6 1 6 4 3 1 6 1 6 1 3 7

说明/提示

数据保证, 3 ≤ N ≤ 200 3\leq N\leq 200 3N200

//1320
#include 
#include 
using namespace std;

int main() {
    char grid[200][201]; // 最大行数200,每行最多200字符+1个结束符
    int N = 0;
    
    // 读取所有行直到输入结束
    while (scanf("%200s", grid[N]) == 1) {
        N++;
    }
    
    vector<int> res;
    int current_mode = 0; // 初始统计0的个数
    int current_count = 0;
    
    // 遍历所有字符,按行优先顺序
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            char c = grid[i][j];
            if (c == '0') 
			{
                if (current_mode == 0) 
				{                                                       
                    current_count++;
                } 
				else
				 {
                    res.push_back(current_count);
                    current_mode = 0;
                    current_count = 1;
                }
            } 
			else 
			{ // '1'
                if (current_mode == 1) 
				{
                    current_count++;
                } 
				else 
				{
                    res.push_back(current_count);
                    current_mode = 1;
                    current_count = 1;
                }
            }
        }
    }
    // 添加最后一个统计数
    res.push_back(current_count);
    
    // 输出结果
    printf("%d", N);
    for (int num : res) {
        printf(" %d", num);
    }
    printf("\n");
    
    return 0;
}
 

你可能感兴趣的:(算法,模拟,数组,字符串)