题意:
分析:
类似UVa 297, 模拟四分树四分的过程, 就是记录一个左上角, 记录宽度wideth, 然后每次w/2这样递归下去。 注意全黑是输出0, 不是输出1234。
1 #include <bits/stdc++.h> 2 using namespace std; 3 // 1 2 4 // 3 4 5 const int base5[8] = {1,5,25,125,625,3125,15625,78125}; 6 int n; 7 string G[70]; 8 vector<int> code; 9 int dfs(int r, int c, int w,int num,int dep){ 10 int lt[5][2] = {{},{r,c},{r,c+w/2},{r+w/2,c},{r+w/2, c+w/2}}; 11 12 for(int i = 1; i <= 4; i++){ 13 int all_black = 1, have_black = 0; 14 15 for(int j = lt[i][0]; j < lt[i][0] + w/2; j++){ 16 for(int k = lt[i][1]; k < lt[i][1] + w/2; k++){ 17 if(G[j][k] == ‘0‘){ 18 all_black = 0; 19 } 20 else { 21 have_black = 1; 22 } 23 } 24 } 25 if(all_black == 0){ 26 27 if(w == 2){//宽度为2四分,只有一个格子, 只能为白 28 continue; 29 } 30 if(have_black) 31 dfs(lt[i][0], lt[i][1], w/2, num + base5[dep]*i, dep + 1 ); 32 } 33 else{ 34 code.push_back(num + base5[dep] * i); 35 } 36 } 37 } 38 void print(int r, int c, int w, int dep,int decode, int dec){ 39 int lt[5][2] = {{},{r,c},{r,c+w/2},{r+w/2,c},{r+w/2, c+w/2}}; 40 if(decode == 0){ 41 for(int i = r; i < r + w; i++){ 42 for(int j = c; j < c + w; j++ ){ 43 G[i][j] = ‘1‘; 44 } 45 } 46 return; 47 } 48 49 dec = decode % 5; 50 decode /= 5; 51 52 53 for(int i = 1; i <= 4; i++){ 54 if(dec == i){ 55 print(lt[i][0], lt[i][1], w/2 , dep + 1,decode, dec); 56 } 57 else continue; 58 } 59 } 60 int main(){ 61 ios::sync_with_stdio(false); 62 int kase = 1; 63 while(cin >> n && n){ 64 65 if(kase > 1) cout <<‘\n‘; 66 cout << "Image "<<kase ++ << ‘\n‘; 67 68 if(n > 0){ 69 int flag = 0; 70 for(int i = 0; i < n; i++){ 71 cin >> G[i]; 72 for(int j = 0; j < G[i].size(); j++){ 73 if(G[i][j] == ‘0‘) flag = 1; 74 } 75 } 76 if(!flag){//特判全黑 77 cout << "0\n"; 78 cout << "Total number of black nodes = 1"<< "\n"; 79 continue; 80 81 } 82 83 if(n == 1){//特判n = 1 全白。 84 cout << "Total number of black nodes = 0" << "\n"; 85 continue; 86 } 87 88 89 dfs(0,0,n,0,0); 90 sort(code.begin(), code.end()); 91 for(int i = 0; i < code.size(); i++){ 92 cout << code[i]; 93 if((i + 1) % 12 == 0 || i == code.size() - 1) 94 cout <<‘\n‘; 95 else cout << ‘ ‘; 96 } 97 98 cout << "Total number of black nodes = "<< code.size() << ‘\n‘; 99 code.clear(); 100 } 101 102 else { 103 n = -n; 104 for(int i = 0; i < n; i++){ 105 G[i] = ""; 106 G[i]. resize(n +7);//string要更改size才能下标访问 107 } 108 int decode; 109 while(cin >> decode && decode != -1){ 110 print(0,0,n,0,decode, 0); 111 } 112 for(int i = 0; i < n; i++){ 113 for(int j = 0; j < n; j++){ 114 if(G[i][j] == ‘1‘){ 115 cout << ‘*‘; 116 } 117 else cout << ‘.‘; 118 } 119 cout << ‘\n‘; 120 } 121 } 122 } 123 124 125 return 0; 126 }
时间: 2024-10-25 04:15:43