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 3≤N≤200。
//1320
#include <cstdio>
#include <vector>
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;
}