题目链接
题目大意:
用四叉树可以表示黑白图像,对于一个黑白图像等分四部分,左上、右上、左下、右下。如果一个区域同为一种颜色,那么就不再分叉。现在从根节点开始,走左上记1、右上记2、左下记3、右下记4。一个节点的路径就可以用这些数连起来构成一个五进制的数。输入有两种情况,一种是给n*n的0/1点阵,输出所有黑色节点的路径(10进制);一种是给出黑色节点的路径(10进制),输出n*n的点阵。
分析:
分治,递归思想。把矩阵分为长度为1*1的再操作。
除了有些麻烦,要注意细节和特殊情况,还要注意输出,这道题输出有点坑!!
其他完全就是Quadtrees UVA - 297(题解:点击这里)的升级版,这道题正好可以考察那道题是否完全理解。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 64;
int n,cnt;
vector<string> ten;
vector<int> five;
int matrix[maxn][maxn];
char pic[maxn][maxn];
void solve(int x, int y, int len, int no, int sum) {
if(len>=1) {
int num = 0;
for(int i = x; i < x+len; i++) {
for(int j = y; j < y+len; j++) {
if(matrix[i][j]) num++;//为黑块++
else num--;
}
}
if(num+len*len!=0) {//不全是白块
if(num==len*len) { //全是黑块,cnt++,把黑块位置压入容器
sum = sum*10 + no;
five.push_back(sum);
cnt++;
}
else {
sum = sum*10 + no;
solve(x, y, len/2,1,sum);//1
solve(x, y+len/2, len/2,2,sum);//2
solve(x+len/2, y, len/2,3,sum);//3
solve(x+len/2, y+len/2, len/2,4,sum);//4
}
}
}
}
void draw(string str, int pos, int x, int y, int len) {
if(pos==(int)str.length()) {//结束
for(int i = y; i < y+len; i++) {
for(int j = x; j < x+len; j++)
pic[i][j] = '*';//涂黑块
}
return;
}
if(str[pos]=='1') draw(str, pos+1, x, y, len/2);
if(str[pos]=='2') draw(str, pos+1, x+len/2, y, len/2);
if(str[pos]=='3') draw(str, pos+1, x, y+len/2, len/2);
if(str[pos]=='4') draw(str, pos+1, x+len/2, y+len/2, len/2);
}
void ten_five() {//进制转换
vector<int> temp;
for(int i = 0; i < (int)five.size(); i++) {
stringstream ss;
ss<<five[i];
string str = ss.str();
int sum = 0;
for(int j = (int)str.length()-1; j >= 0; j--)
sum = sum*5 + str[j]-'0';
temp.push_back(sum);
}
sort(temp.begin(), temp.end());
bool flag = false;
for(int i = 0, j = 0; i < (int)temp.size(); i++, j++) {
if(j%12==0 && j) { cout << endl; flag = false; }
if(!flag) {
cout << temp[i];
flag = true;
}
else cout << " " << temp[i];
}
if(five.size()) cout << endl;
}
string five_ten(int num) {//进制转换
int n = 0;
while(num>0) {
int t = num%5;
n = n*10 + t;
num /= 5;
}
stringstream ss;
ss<<n;
string str = ss.str();
return str;
}
int main()
{
freopen("i.txt","r",stdin);
freopen("o.txt","w",stdout);
int rnd = 0;
while(cin >> n && n) {
if(rnd) cout << endl;
cout << "Image " << ++rnd << endl;
if(n>0) {
memset(matrix, 0, sizeof(matrix));
five.clear();
cnt = 0; char ch;
int num = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cin >> ch;
matrix[i][j] = ch-'0';
if(!matrix[i][j]) num++;
}
}
//全为白块
if(num==n*n) cout << "Total number of black nodes = 0" << endl;
else {
solve(0,0,n,0,0);
ten_five();
cout << "Total number of black nodes = " << cnt << endl;
}
}
else {
n = abs(n);
ten.clear();
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++)
pic[i][j] = '.';
}
int t; vector<int> temp;
while(cin >> t && t!=-1)
temp.push_back(t);
for(int i = 0; i < (int)temp.size(); i++)
ten.push_back(five_ten(temp[i]));
if(ten.size()==1 && ten[0]=="0") {//全为黑块
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++)
pic[i][j] = '*';
}
}
else{
for(int i = 0; i < (int)temp.size(); i++)
draw(ten[i],0,0,0,n);
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++)
cout << pic[i][j];
cout << endl;
}
}
}
return 0;
}