zoj 1788 Quad Trees
来源:互联网 发布:懂球帝是腾讯软件吗 编辑:程序博客网 时间:2024/05/17 18:25
四分树的题目
利用dfs建立一棵四分树。
然后bfs遍历四分树。
最后把遍历结果转换为十六进制输出。
#include <iostream>#include <cstdio>#include <algorithm>#include <set>#include <list>#include <queue>#include <stack>#include <vector>#include <cstring>#include <cmath>#include <map>#include <iomanip>#define pb push_back#define mp make_pair#define fi first#define se second#define all(a) (a).begin(),(a).end()#define FOR(i,a,b) for (int i=(a);i<(b);i++)#define FORD(i,a,b) for (int i=(a); i>=(b); i--)#define REP(i,b) FOR(i,0,b)#define sf scanf#define pf printfusing namespace std;#define N 550struct quad_tree{ char value[3]; quad_tree* child[4];};int store[N][N];bool cmp(quad_tree* lhs,quad_tree *rhs){ if (lhs->value[0]=='1'||rhs->value[0]=='1') return false; FOR(i,0,2) if(lhs->value[i]!=rhs->value[i]) return false; return true;}quad_tree* dfs(int r,int c,int s){ quad_tree* current = new quad_tree(); if (s==1) { current->value[0] = '0'; current->value[1] = '0'+store[r][c]; current->value[2] = '\0'; return current; } s /=2; current->child[0] = dfs(r,c,s); current->child[1] = dfs(r,c+s,s); current->child[2] = dfs(r+s,c,s); current->child[3] = dfs(r+s,c+s,s); bool f = true; FOR(i,1,4) { if (!cmp(current->child[0],current->child[i])) { f =false; break; } } if (f) { current->value[0] = '0'; current->value[1] = current->child[0]->value[1]; current->value[2] = '\0'; FOR(i,0,4) { delete current->child[i]; current->child[i]=0; } } else { current->value[0] = '1'; current->value[1] = '\0'; } return current;}queue<quad_tree*> bfslist;char result[N*N];int ct;char hexout[N*N];void bfs(quad_tree* root){ bfslist.push(root); quad_tree *tmp; while(!bfslist.empty()) { tmp = bfslist.front(); bfslist.pop(); if (tmp->value[0]=='1') { result[ct++] = '1'; FOR(i,0,4) { bfslist.push(tmp->child[i]); } } else { result[ct++] = '0'; result[ct++] = tmp->value[1]; } }}int main(){ int n,m; char hex []= {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; sf("%d",&n); while(n--) { sf("%d",&m); FOR(i,0,m) FOR(j,0,m) { sf("%d",&store[i][j]); } quad_tree *root; root=dfs(0,0,m); while(!bfslist.empty()) bfslist.pop(); ct = 0; bfs(root); //四位二进制数为一个十六进制,记录能否组成一个十六进制 int chex = 0; //记录二进制转十进制累计值 int dec = 0; //记录十六进制输出的数组规模 int ch = 0; for(int i=ct-1;i>=0;i--) { chex++; dec = dec+ ((result[i]-'0') <<(chex-1)); if (chex==4) { hexout[ch++] = hex[dec]; dec = 0 ; chex = 0 ; } } if (chex>0) { hexout[ch++] = hex[dec]; } for(int i=ch-1;i>=0;i--) { pf("%c",hexout[i]); } pf("\n"); } return 0;}
- zoj 1788 Quad Trees
- zoj 1788 Quad Trees
- zoj 1788 Quad Trees
- Quad Trees - ZOJ 1788 四分树
- zoj - 1788 - Quad Trees(四分树)
- ZOJ 1788 Quad Trees (朴素BFS)
- [zoj 1788] Quad Trees(四分树)
- zoj1788-Quad Trees
- POJ 1610 Quad Trees
- Quad
- Counting Factor Trees zoj 3405
- zoj 1453 Surround the Trees
- ZOJ 3602Count the Trees
- zoj 1102 Phylogenetic Trees Inherited
- ZOJ 3602 Count the Trees
- ZOJ 3602 Count the Trees
- zoj 1453 Surround the Trees
- zoj 1102 Phylogenetic Trees Inherited
- oracle定时备份-增量备份
- [推荐]centos6.0 安装apache php mysql subversion(svn) swftools(pdf转换swf)
- nohup
- Dom4j封装二进制图片数据
- 做个宽容的人
- zoj 1788 Quad Trees
- android 手势操作
- MS SQLServer 2005 分区表的使用(表分区)
- Java反射机制详解
- IEEE 802.11 MAC层介绍
- 搭建tiny6410驱动开发环境_NFS起文件系统_NoSharedLibs
- Python自动登录带密码的网站
- Machine Learning Resources机器学习的资料
- Android Handler notes