四叉树 bnuoj
建树+广搜一棵树;最下面有更短代码(很巧妙)。
#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm> using namespace std; int maze[][];
struct node
{
int num;
node *next[];
};
void init_tree(node *root)
{
int i;
for(i=; i<; i++)
root->next[i] = NULL;
}
void built(int istart,int iend,int jstart,int jend,node *root)
{
int dive,i,j;
int istart1=istart,iend1=iend,jstart1=jstart,jend1=jend;
for(dive=; dive<; dive++)
{
if(dive==) istart=istart1,iend=istart1+(iend1-istart1+)/-,jstart=jstart1,jend=jstart1+(jend1-jstart1+)/-;
else if(dive==) istart=istart1,iend=istart1+(iend1-istart1+)/-,jstart=jstart1+(jend1-jstart1+)/,jend=jend1;
else if(dive==) istart=istart1+(iend1-istart1+)/,iend=iend1,jstart=jstart1,jend=jstart1+(jend1-jstart1+)/-;
else istart=istart1+(iend1-istart1+)/,iend=iend1,jstart=jstart1+(jend1-jstart1+)/,jend=jend1;
int flag0=,flag1=,tree_point;
for(i=istart; i<=iend; i++)
{
for(j=jstart; j<=jend; j++)
{
if(maze[i][j]==) flag0=;
if(maze[i][j]==) flag1=;
}
}
if(flag0&&flag1) tree_point=;
else if(flag0&&!flag1) tree_point=;
else if(!flag0&&flag1) tree_point=;
node *p;
p=new node;
init_tree(p);
root->next[dive]=p;
p->num=tree_point;
if(tree_point==||tree_point==) continue ;
built(istart,iend,jstart,jend,p);
}
}
void print(node *root)
{
node *ss,*tt;
queue<node*>q;
q.push(root);
while(!q.empty())
{
ss=q.front();
int su=ss->num;
if(su==) printf("");
else if(su==) printf("");
else if(su==) printf("");
q.pop();
for(int i=; i<; i++)
{
if(ss->next[i] !=NULL)
{
tt=ss->next[i];
q.push(tt);
}
}
}
printf("\n");
}
int check_map(int n)
{
int i,j,sum=;
for(i=; i<=n; i++)
for(j=; j<=n; j++)
sum+=maze[i][j];
if(sum==n*n) return ;
else if(sum==) return ;
else return -;
}
void delete_tree(node *root)
{
int i;
for(i=; i<; i++)
{
if(root->next[i]!=NULL)
{
delete_tree(root->next[i]);
}
}
delete root;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int i,j;
memset(maze,,sizeof(maze));
for(i=; i<=n; i++)
{
for(j=; j<=n; j++)
scanf("%d",&maze[i][j]);
}
int ans=check_map(n);
if(ans==) printf("00\n");
else if(ans==) printf("01\n");
else
{
node *tree;
tree=new node;
init_tree(tree);
tree->num=;
built(,n,,n,tree);
print(tree);
delete_tree(tree);
}
}
return ;
}
短代码:
#include <cstring>
#include <vector>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstring>
//#define N 10005
using namespace std;
int zu[][],n;
struct sb
{
int x,y,x1,y1;
bool Get()
{
int ans=;
for(int i=x;i<=x1;i++)
for(int j=y;j<=y1;j++)
ans+=zu[i][j];
if(!ans)
return true;
if(ans==(x1-x+)*(y1-y+))
return true;
return false;
}
};
int main()
{
while(scanf("%d",&n)==)
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&zu[i][j]);
//
sb s1,s2;
s1.x=;
s1.y=;
s1.x1=n;
s1.y1=n;
queue<sb>Q;
Q.push(s1);
while(!Q.empty())
{
s1=Q.front();
Q.pop();
if(s1.Get())
{
printf("0%d",zu[s1.x1][s1.y1]);
continue;
}
printf("");
int xx=(s1.x+s1.x1)/;
int yy=(s1.y+s1.y1)/;
s2=s1;
s2.y1=yy;
s2.x1=xx;
Q.push(s2);
//
s2=s1;
s2.x1=xx;
s2.y=yy+;
Q.push(s2);
//
s2=s1;
s2.x=xx+;
s2.y1=yy;
Q.push(s2);
//
s2=s1;
s2.x=xx+;
s2.y=yy+;
Q.push(s2);
}
printf("\n");
}
}
四叉树 bnuoj的更多相关文章
- BNUOJ 4049 四叉树
四叉树 Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class name: Ma ...
- 地图四叉树一般用在GIS中,在游戏寻路中2D游戏中一般用2维数组就够了
地图四叉树一般用在GIS中,在游戏寻路中2D游戏中一般用2维数组就够了 四叉树对于区域查询,效率比较高. 原理图
- HTML5实现3D和2D可视化QuadTree四叉树碰撞检测
QuadTree四叉树顾名思义就是树状的数据结构,其每个节点有四个孩子节点,可将二维平面递归分割子区域.QuadTree常用于空间数据库索引,3D的椎体可见区域裁剪,甚至图片分析处理,我们今天介绍的是 ...
- HT for Web可视化QuadTree四叉树碰撞检测
QuadTree四叉树顾名思义就是树状的数据结构,其每个节点有四个孩子节点,可将二维平面递归分割子区域.QuadTree常用于空间数据库索引,3D的椎体可见区域裁剪,甚至图片分析处理,我们今天介绍的是 ...
- BNUOJ 52325 Increasing or Decreasing 数位dp
传送门:BNUOJ 52325 Increasing or Decreasing题意:求[l,r]非递增和非递减序列的个数思路:数位dp,dp[pos][pre][status] pos:处理到第几位 ...
- bnuoj 24251 Counting Pair
一道简单的规律题,画出二维表将数字分别相加可以发现很明显的对称性 题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=24251 #include< ...
- [转]一个四叉树Demo学习
程序代码: http://www.codeproject.com/Articles/30535/A-Simple-QuadTree-Implementation-in-C 四叉树: using Sys ...
- [转]基于四叉树(QuadTree)的LOD地形实现
实现基于四叉树的LOD地形时,我遇到的主要问题是如何修补地形裂缝. 本段我将描述使用LOD地形的优势,我实现LOD地形的思路,实现LOD地形核心模块的详细过程,以及修补地形裂缝的思路. 首先,LOD地 ...
- 线性四叉树十进制Morton码计算示例
线性四叉树十进制Morton码计算,具体算法描述龚健雅<地理信息系统基础>P108
随机推荐
- 你需要的130个vim命令
参考文章:http://www.oschina.net/news/43167/130-essential-vim-commands,其中有些我以为需要补充的及时补充或修改 从 1970 年开始,vi ...
- 再看c语言之getchar/putchar
- #ifdef #endif #if #endif
c语言里所有以#开头的都是预编译指令,就是在正式编译之前,让编译器做一些预处理的工作. #ifdef DEBUG printf("variable x has value = %d\n&qu ...
- luogu P2296 寻找道路
题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点连通. 2 .在满足条 ...
- Linux下使用curl进行http请求(转)
curl在Linux下默认已经安装,Windows需要自行安装. 下载地址:https://curl.haxx.se/download.html Windows离线版本:链接:http://pan.b ...
- docker下用keepalived+Haproxy实现高可用负载均衡集群
启动keepalived后宿主机无法ping通用keepalived,报错: [root@localhost ~]# ping 172.18.0.15 PING () bytes of data. F ...
- 向C#的选项卡中添加自定义窗体
一.自定义窗体的搭建 这个比较简单,添加一个WinForm窗体就行了,设置一个名字EditPanel,然后在窗体上画需要的控件. 二.将自定义窗体添加到选项卡 // 新建窗体加入到选项卡中 EditP ...
- bzoj1601【Usaco2008 Oct】灌水
1601: [Usaco2008 Oct]灌水 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 1589 Solved: 1035 [Submit][ ...
- iOS多线程编程(四)------ GCD(Grand Central Dispatch)
一.简单介绍 是基于C语言开发的一套多线程开发机制.也是眼下苹果官方推荐的多线程开发方法.用起来也最简单.仅仅是它基于C语言开发,并不像NSOperation是面向对象的开发.而是全然面向过程的.假设 ...
- Windows7和Ubuntu12.04无法选择系统
Windos7 旗舰版 Ubuntu12.04LTS 64位版本号 硬件挂载两个硬盘 SSD+机械 Windows7和Ubuntu12.04都装在SSD上.眼下先装好了Windows7,打算装Ubun ...