Problem UVA806-Spatial Structures
Accept:329 Submit:2778
Time Limit: 3000 mSec
Problem Description
Input
The input contains one or more images. Each image is square, and the data for an image starts with an integer n, where |n| is the length of a side of the square (always a power of two, with |n| < 64) followed by a representation of the image. A representation is either a sequence of n2 zeros and ones comprised of |n| lines of |n| digits per line, or the sequence of numbers that represent the root-to-leaf paths of each black node in the quadtree that represents the image. If n is positive, the zero/one representation follows; if n is negative, the sequence of black node path numbers (in base 10) follows. The sequence is terminated by the number -1. A one-node tree that represents an all-black image is represented by the number 0. A one-node tree that represents an all-white image is represented by an empty sequence (no numbers). The end of data is signaled by a value of 0 for n.
Output
For each image in the input, first output the number of the image, as shown in the sample output. Then output the alternate form of the image. If the image is represented by zeros and ones, the output consists of root-to-leaf paths of all black nodes in the quadtree that represents the image. The values should be base 10 representations of the base 5 path numbers, and the values should be printed in sorted order. If there are more than 12 black nodes, print a newline after every 12 nodes. The total number of black nodes should be printed after the path numbers. If the image is represented by the root-to-leaf paths of black nodes, the output consists of an ASCII representation of the image with the character ‘.’ used for white/zero and the character ‘*’ used for black/one. There should be n characters per line for an n×n image.
Sample Input
Sample Ouput
Image 1
9 14 17 22 23 44 63 69 88 94 113
Total number of black nodes = 11
Image 2
........
........
....****
....****
...*****
..******
..****..
..***...
Image 3
Total number of black nodes = 0
Image 4
****
****
****
****
题解:这个题如果没有lrj前面例题的铺垫,我自己估计是搞不定,不过做了那个例题之后,这个题就是稍微麻烦一点,没什么特殊的地方,重在代码基本功。
这个题的输出格式有点坑,总的来说就是题中没说的换行不要有,尤其是最后一个Case。
四分树例题:UVA297:Quadtrees
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
using namespace std; const int maxn = ;
int n,cnt;
char gra[maxn][maxn];
vector<int> ans; bool is_black(int r,int c,int wide){
for(int i = r;i < r+wide;i++){
for(int j = c;j < c+wide;j++){
if(gra[i][j] == '') return false;
}
}
return true;
} bool is_white(int r,int c,int wide){
for(int i = r;i < r+wide;i++){
for(int j = c;j < c+wide;j++){
if(gra[i][j] == '') return false;
}
}
return true;
} int consumption(string &ss){
int res = ;
for(int i = ss.size()-;i >= ;i--){
res *= ;
res += ss[i]-'';
}
return res;
} void cal(string str,int r,int c,int wide){
if(is_black(r,c,wide)){
ans.push_back(consumption(str));
return;
}
else if(is_white(r,c,wide)) return;
else{
cal(str+"",r,c,wide/);
cal(str+"",r,c+wide/,wide/);
cal(str+"",r+wide/,c,wide/);
cal(str+"",r+wide/,c+wide/,wide/);
}
} int solve(int n){
cnt = ;
ans.clear();
for(int i = ;i < n;i++){
scanf("%s",gra[i]);
}
string str = "";
if(is_black(,,n)) return ;
if(is_white(,,n)) return -;
cal(str+"",,,n/);
cal(str+"",,n/,n/);
cal(str+"",n/,,n/);
cal(str+"",n/,n/,n/);
return ;
} vector<int> num; void converse(int val,string &ss){
while(val){
ss += (val%+'');
val /= ;
}
} void draw(string &ss,int pos,int r,int c,int wide){
if(pos == ss.size()){
for(int i = r;i < r+wide;i++){
for(int j = c;j < c+wide;j++){
gra[i][j] = '*';
}
}
return;
}
if(ss[pos] == ''){
draw(ss,pos+,r,c,wide/);
}
else if(ss[pos] == ''){
draw(ss,pos+,r,c+wide/,wide/);
}
else if(ss[pos] == ''){
draw(ss,pos+,r+wide/,c,wide/);
}
else draw(ss,pos+,r+wide/,c+wide/,wide/);
} void solve2(int n){
int x;
num.clear();
memset(gra,,sizeof(gra));
while(scanf("%d",&x) && x!=-) num.push_back(x);
if(num.size()== && num[]==){
for(int i = ;i < n;i++){
for(int j = ;j < n;j++){
printf("*");
}
printf("\n");
}
return;
}
for(int i = ;i < num.size();i++){
string ss = "";
converse(num[i],ss);
draw(ss,,,,n);
}
for(int i = ;i < n;i++){
for(int j = ;j < n;j++){
if(gra[i][j] == '*') printf("%c",gra[i][j]);
else printf(".");
}
printf("\n");
}
} int iCase = ; int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
bool flag = false;
while(~scanf("%d",&n) && n){
if(flag) printf("\n");
flag = true;
if(n > ){
int flag = solve(n);
printf("Image %d\n",iCase++);
if(flag == ){
printf("%d\n",);
printf("Total number of black nodes = %d\n",);
}
else if(flag == -){
printf("Total number of black nodes = %d\n",);
}
else{
sort(ans.begin(),ans.end());
int cnt = ;
for(int i = ;i < ans.size();i++){
if(cnt == ) printf("%d",ans[i]);
else printf(" %d",ans[i]);
cnt++;
if(cnt == ) cnt = ,printf("\n");
}
if(ans.size()%)printf("\n");
printf("Total number of black nodes = %d\n",ans.size());
}
}
else{
printf("Image %d\n",iCase++);
solve2(-n);
}
}
return ;
}