UVa 806 四分树
题意:

分析:
类似UVa 297, 模拟四分树四分的过程, 就是记录一个左上角, 记录宽度wideth, 然后每次w/2这样递归下去。 注意全黑是输出0, 不是输出1234。
#include <bits/stdc++.h>
using namespace std;
// 1 2
// 3 4
const int base5[] = {,,,,,,,};
int n;
string G[];
vector<int> code;
int dfs(int r, int c, int w,int num,int dep){
int lt[][] = {{},{r,c},{r,c+w/},{r+w/,c},{r+w/, c+w/}}; for(int i = ; i <= ; i++){
int all_black = , have_black = ; for(int j = lt[i][]; j < lt[i][] + w/; j++){
for(int k = lt[i][]; k < lt[i][] + w/; k++){
if(G[j][k] == ''){
all_black = ;
}
else {
have_black = ;
}
}
}
if(all_black == ){ if(w == ){//宽度为2四分,只有一个格子, 只能为白
continue;
}
if(have_black)
dfs(lt[i][], lt[i][], w/, num + base5[dep]*i, dep + );
}
else{
code.push_back(num + base5[dep] * i);
}
}
}
void print(int r, int c, int w, int dep,int decode, int dec){
int lt[][] = {{},{r,c},{r,c+w/},{r+w/,c},{r+w/, c+w/}};
if(decode == ){
for(int i = r; i < r + w; i++){
for(int j = c; j < c + w; j++ ){
G[i][j] = '';
}
}
return;
} dec = decode % ;
decode /= ; for(int i = ; i <= ; i++){
if(dec == i){
print(lt[i][], lt[i][], w/ , dep + ,decode, dec);
}
else continue;
}
}
int main(){
ios::sync_with_stdio(false);
int kase = ;
while(cin >> n && n){ if(kase > ) cout <<'\n';
cout << "Image "<<kase ++ << '\n'; if(n > ){
int flag = ;
for(int i = ; i < n; i++){
cin >> G[i];
for(int j = ; j < G[i].size(); j++){
if(G[i][j] == '') flag = ;
}
}
if(!flag){//特判全黑
cout << "0\n";
cout << "Total number of black nodes = 1"<< "\n";
continue; } if(n == ){//特判n = 1 全白。
cout << "Total number of black nodes = 0" << "\n";
continue;
} dfs(,,n,,);
sort(code.begin(), code.end());
for(int i = ; i < code.size(); i++){
cout << code[i];
if((i + ) % == || i == code.size() - )
cout <<'\n';
else cout << ' ';
} cout << "Total number of black nodes = "<< code.size() << '\n';
code.clear();
} else {
n = -n;
for(int i = ; i < n; i++){
G[i] = "";
G[i]. resize(n +);//string要更改size才能下标访问
}
int decode;
while(cin >> decode && decode != -){
print(,,n,,decode, );
}
for(int i = ; i < n; i++){
for(int j = ; j < n; j++){
if(G[i][j] == ''){
cout << '*';
}
else cout << '.';
}
cout << '\n';
}
}
} return ;
}
UVa 806 四分树的更多相关文章
- UVa 297 (四分树 递归) Quadtrees
题意: 有一个32×32像素的黑白图片,用四分树来表示.树的四个节点从左到右分别对应右上.左上.左下.右下的四个小正方区域.然后用递归的形式给出一个字符串代表一个图像,f(full)代表该节点是黑色的 ...
- UVa 297 四分树
感觉特别像那个分治的日程表问题.是f的话就填,否则就不填,然后同一个表填两次.那么就是最后的结果. #include <iostream> #include <cstring> ...
- UVA.297 Quadtrees (四分树 DFS)
UVA.297 Quadtrees (四分树 DFS) 题意分析 将一个正方形像素分成4个小的正方形,接着根据字符序列来判断是否继续分成小的正方形表示像素块.字符表示规则是: p表示这个像素块继续分解 ...
- UVA - 297 Quadtrees (四分树)
题意:求两棵四分树合并之后黑色像素的个数. 分析:边建树边统计. #include<cstdio> #include<cstring> #include<cstdlib& ...
- 四分树 (Quadtrees UVA - 297)
题目描述: 原题:https://vjudge.net/problem/UVA-297 题目思路: 1.依旧是一波DFS建树 //矩阵实现 2.建树过程用1.0来填充表示像素 #include < ...
- 搜索(四分树):BZOJ 4513 [SDOI2016 Round1] 储能表
4513: [Sdoi2016]储能表 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 395 Solved: 213[Submit][Status] ...
- [C++]四分树(Quadtrees)
[本博文非博主原创,思路与题目均摘自 刘汝佳<算法竞赛与入门经典(第2版)>] 四分树Quadtrees 一幅图有1024个点, 可以对图平均分成4块, 并且子图也可以再往下分, 直到一个 ...
- UVA806-Spatial Structures(四分树)
Problem UVA806-Spatial Structures Accept:329 Submit:2778 Time Limit: 3000 mSec Problem Description ...
- Uva297 Quadtrees【递归建四分树】【例题6-11】
白书 例题6-11 用四分树来表示一个黑白图像:最大的图为根,然后按照图中的方式编号,从左到右对应4个子结点.如果某子结点对应的区域全黑或者全白,则直接用一个黑结点或者白结点表示:如果既有黑又有白,则 ...
随机推荐
- [Usaco2017 Feb]Why Did the Cow Cross the Road III (Gold)
Description 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai < aj < bi < bj的对数 Sample Input ...
- C#不允许在foreach循环中改变数组或集合中元素的值(注:成员的值不受影响)
C#不允许在foreach循环中改变数组或集合中元素的值(注:成员的值不受影响),如以下代码将无法通过编译. foreach (int x in myArray) { x++; //错误代码,因为改变 ...
- js 宿主对象的属性和方法总结
(1)属性: //height,width; a=document.documentElement.clientHeight; //文档可视高度,由 ...
- Linux 之 2>&1
我们在Linux下经常会碰到nohup command>/dev/null 2>&1 &这样形式的命令.首先我们把这条命令大概分解下首先就是一个nohup表示当前用户和系统 ...
- call方法的使用bug--参数undefined
call/apply是函数原型定义的方法(Function.prorotype),在使用时要注意第一个形参(args[0]),一定是指向函数所要挂载的上下文对象--context,若对象非必须,则要将 ...
- R Programming week 3-Loop functions
Looping on the Command Line Writing for, while loops is useful when programming but not particularly ...
- iOS - 事件处理全过程(补充)
事件处理的完整过程 1> 先将事件对象由上往下传递(由父控件传递给子控件),找到最合适的控件来处理这个事件. 2> 调用最合适控件的touches….方法 3> 如果调用了[supe ...
- Shorthand Argument Names $0 : 只用于指代Closer声明中的形参
Shorthand Argument Names Swift automatically provides shorthand argument names to inline closures, w ...
- Swift 命名空间形式扩展的实现
Swift 的 extension 机制很强大,不仅可以针对自定义的类型,还能作用于系统库的类型,甚至基础类型比如 Int.当在对系统库做 extension 的时候,就会涉及到一个命名冲突的问题.O ...
- Linux——网络编程线程池机制
#include <stdlib.h>#include <pthread.h>#include <unistd.h>#include <assert.h> ...