四叉树 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
随机推荐
- 转载 gdb调试程序
转载自csdn,作者haoel,链接http://blog.csdn.net/haoel/article/details/2879 用GDB调试程序 GDB概述———— GDB是GNU开源组织发布的一 ...
- Spring拦截器从Request中获取Json格式的数据
7 package com.newpp.core.interceptor; 8 9 import java.io.BufferedReader; 10 import java.io.ByteArray ...
- android 拍照预览
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...
- Blazor——Asp.net core的新前端框架
原文:Blazor--Asp.net core的新前端框架 Blazor是微软在Asp.net core 3.0中推出的一个前端MVVM模型,它可以利用Razor页面引擎和C#作为脚本语言来构建WEB ...
- sklearn 特征选择
1.移除低方差的特征(Removing features with low variance) VarianceThreshold 是特征选择中的一项基本方法.它会移除所有方差不满足阈值的特征.默认设 ...
- 使用fiddler进行手机数据抓取
使用fiddler进行手机数据抓取 学习了:https://blog.csdn.net/gld824125233/article/details/52588275 https://blog.csdn. ...
- lua 暂停写法
由于lua 不支持暂停 用其他方法变相实现 -- 暂停 hock 写法 function _M.sleep(n) if n > 0 then os.execute("ping -c & ...
- 【源代码】LruCache源代码剖析
上一篇分析了LinkedHashMap源代码,这个Map集合除了拥有HashMap的大部分特性之外.还拥有链表的特点,即能够保持遍历顺序与插入顺序一致. 另外.当我们将accessOrder设置为tr ...
- iOS 身份证验证
- (void)onClickButton:(id) sender{ || tmp_txt.text.length == ) { NSString *emailRegex = @"^[0-9 ...
- OpenStack源码系列---nova-compute
nova-compute运行的节点为计算节点,虚拟机运行于计算节点上.例如对于创建虚拟机请求,nova-api接收到客户端请求后,经过nova-scheduler调度器调度,再将请求发送给某个选定的n ...