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

8
00000000
00000000
00001111
00001111
00011111
00111111
00111100
00111000
-8
9 14 17 22 23 44 63 69 88 94 113 -1
2
00
00
-4
0 -1
0
 

 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 ;
}

UVA806-Spatial Structures(四分树)的更多相关文章

  1. UVA-806 Spatial Structures (四分树)

    题目大意:将一块图像上的黑点在两种表示法之间转换. 题目分析:递归下去... 注意:输出时要注意细节!!! 代码如下: # include<iostream> # include<c ...

  2. [刷题]算法竞赛入门经典(第2版) 6-8/UVa806 - Spatial Structures

    题意:黑白图像的路径表示法 代码:(Accepted,0.120s) //UVa806 - Spatial Structures //Accepted 0.120s //#define _XIENAO ...

  3. uva806 Spatial Structures 空间结构 (黑白图像的四分树表示)

    input 8 00000000 00000000 00001111 00001111 00011111 00111111 00111100 00111000 -8 9 14 17 22 23 44 ...

  4. UVa 297 (四分树 递归) Quadtrees

    题意: 有一个32×32像素的黑白图片,用四分树来表示.树的四个节点从左到右分别对应右上.左上.左下.右下的四个小正方区域.然后用递归的形式给出一个字符串代表一个图像,f(full)代表该节点是黑色的 ...

  5. UVA - 297 Quadtrees (四分树)

    题意:求两棵四分树合并之后黑色像素的个数. 分析:边建树边统计. #include<cstdio> #include<cstring> #include<cstdlib& ...

  6. 搜索(四分树):BZOJ 4513 [SDOI2016 Round1] 储能表

    4513: [Sdoi2016]储能表 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 395  Solved: 213[Submit][Status] ...

  7. [C++]四分树(Quadtrees)

    [本博文非博主原创,思路与题目均摘自 刘汝佳<算法竞赛与入门经典(第2版)>] 四分树Quadtrees 一幅图有1024个点, 可以对图平均分成4块, 并且子图也可以再往下分, 直到一个 ...

  8. Uva297 Quadtrees【递归建四分树】【例题6-11】

    白书 例题6-11 用四分树来表示一个黑白图像:最大的图为根,然后按照图中的方式编号,从左到右对应4个子结点.如果某子结点对应的区域全黑或者全白,则直接用一个黑结点或者白结点表示:如果既有黑又有白,则 ...

  9. 四分树 (Quadtrees UVA - 297)

    题目描述: 原题:https://vjudge.net/problem/UVA-297 题目思路: 1.依旧是一波DFS建树 //矩阵实现 2.建树过程用1.0来填充表示像素 #include < ...

随机推荐

  1. 光流法详解之二(HS光流)

    Horn–Schunck光流算法[1]是一种全局方法估算光流场. 参考博文:https://blog.csdn.net/hhyh612/article/details/79216021 假设条件: H ...

  2. [转]Angular 4|5 Material Dialog with Example

    本文转自:https://www.techiediaries.com/angular-material-dialogs/ In this tutorial, we're going to learn ...

  3. [转]angular2中ng alerts的使用教程

    本文转自:https://blog.csdn.net/m0_37981481/article/details/79281879 由于想要一个好看的alert,于是去npm上搜了一下,手动捂脸,npm上 ...

  4. oracle用户权限

    权限: create session create table unlimited tablespace connect resource dba 例: #sqlplus /nolog SQL> ...

  5. Java 基础系列合集

    Java基础知识你知道多少? Java特性 Java三大特性:封装,继承,多态 Java 抽象类与接口 Java 浅拷贝和深拷贝 Java static和final Java 内部类.静态内部类.匿名 ...

  6. layui 图片上传+表单提交+ Spring MVC

    Layui 的上传是最常用的, 不可或缺, 记录一下代码, 以后复制都能用!! 1.前端HTML: <div class="layui-form-item"> < ...

  7. spring资源访问接口和资源加载接口

    spring 资源访问接口 JDK提供的资源访问类,如java.net.URL.File等,不能很好地满足各种资源的访问需求,比如缺少从类路径或者Web容器的上下文中获取资源的操作类. 鉴于此,spr ...

  8. (网页)12种不宜使用的Javascript语法(转)

    转自阮一峰: 最近写的一些小东西,总是出各种各样的问题,用了angular.js反应居然比我的jQuery还慢,客户吐槽了,我又把一个小操作,改成了jQuery.浏览一下大神的的博客.转载一点东西: ...

  9. Spring Boot 中配置文件application.properties使用

    一.配置文档配置项的调用(application.properties可放在resources,或者resources下的config文件夹里) package com.my.study.contro ...

  10. MySQL GTID复制错误处理之跳过错误

    某Slave报错信息: mysql> show slave status\G; mysql> show slave status\G; ************************** ...