紫书数据结构那章的题,今天个人赛开了出来,纸上模拟实现了一遍,因为格式问题 PE 了好几次,中间还有两次 忘记删除 文件测试那两行
写完了 看一下感觉有点丑,但好歹思路还算清晰,贴一哈
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
using namespace std;
const int maxn = 100 + 7;
int n, a[maxn][maxn]; // shuzu ji daxiao
set<int> st, stans;
int cnt;
char s[1000];
void init() {
st.clear();
stans.clear();
cnt = 0;
memset(a, 0, sizeof a);
if(n > 0) {
for(int i = 0; i < n; ++i) {
scanf("%s", s);
for(int j = 0; j < n; ++j) {
a[i][j] = s[j] - '0';
}
//scanf("%d", &a[i][j]);// >> a[i][j]
}
}
else {
//n = -1*n;
int x;
while(scanf("%d", &x) && x != -1) {
st.insert(x); // mingling jihe
}
}
}
void dfs(int x, int y, int d, int id) {
int num = 0;
for(int i = x; i < x+d; ++i) {
for(int j = y; j < y+d; ++j) {
if(a[i][j] == 1) num++; // heidian geshu
}
}
if(num == d*d) {
cnt++; // hei jiedian geshu
st.insert(id);
}
else if(num == 0) {
return;
}
else {
dfs(x, y, d/2, id*10+1); // NO. x hang, y lie
dfs(x, y+d/2, d/2, id*10+2);
dfs(x+d/2, y, d/2, id*10+3);
dfs(x+d/2, y+d/2, d/2, id*10+4);
}
}
void dfs2(int x, int y, int d, int id) {
if(id == 0) {
for(int i = x; i < x+d; ++i) {
for(int j = y; j < y+d; ++j) {
a[i][j] = 1;
}
}
}
else {
int t = id % 5;
if(t == 1) dfs2(x, y, d/2, id/5); // NO. x hang, y lie
if(t == 2) dfs2(x, y+d/2, d/2, id/5);
if(t == 3) dfs2(x+d/2, y, d/2, id/5);
if(t == 4) dfs2(x+d/2, y+d/2, d/2, id/5);
}
}
void solve() {
if(n > 0) {
dfs(0, 0, n, 0);
if(cnt == 0) {
printf("Total number of black nodes = 0\n"); // ans1
}
else {
set<int>::iterator it = st.begin();
for( ; it != st.end(); ++it) {
int t = *it;
int sum = 0;
while(t) {
sum = sum*5 + t%10;
t /= 10;
}
stans.insert(sum);
}
int tt = 0;
for(it = stans.begin(); it != stans.end(); ++it) {
if(tt) cout << ' ';
cout << *it;
tt++;
if(tt >= 12) { tt = 0; cout << endl; }
}
if(tt != 0) cout << endl;
printf("Total number of black nodes = %d\n", cnt); // nas2
}
}
else {
n = -1*n;
set<int>::iterator it = st.begin();
for( ; it != st.end(); ++it) {
int t = *it;
if(t == 0) {
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
//if(j) cout << " ";
cout << '*' ;
}
cout << endl;
}
//cout << endl;
return;
}
else {
dfs2(0, 0, n, t);
}
}
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j)
if(a[i][j] == 0) cout << '.';
else if(a[i][j] == 1) cout << '*';
cout << endl;
}
//cout << endl;
}
}
int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int kase = 1;
while(scanf("%d", &n) != EOF && n) {
init();
if(kase != 1) cout << endl;
printf("Image %d\n", kase++);
solve();
}
return 0;
}