uva297(quadtree)
2015-04-26 08:49
183 查看
给我们两棵quadtree的前序遍历,要我吗求原来32*32的矩阵有多少个位置是黑的
View Code
import java.util.Scanner; public class Main { static Scanner cin = new Scanner(System.in); final static int LEN = 32; static int ans = 0; static String str; static int p; static int [][] mat = new int [LEN][LEN]; public static void dfs(int r, int c, int w){//(r,c)是左上角 char ch = str.charAt(p++); if(ch=='p'){ dfs(r,c+w/2,w/2);//对于图中区域1部分的, dfs(r,c,w/2);//对于图中区域2部分的, dfs(r+w/2,c,w/2);//对于图中区域3部分的, dfs(r+w/2,c+w/2,w/2);//对于图中区域4部分的, } else if(ch=='f'){//如果遇到黑色的结点,就填充矩形 for(int i=r; i<r+w; ++i) for(int j=c; j<c+w; ++j) if(mat[i][j]==0){//因为是两棵树合并,所以有可能重复填充,所以只有填充0的时候计算ans++ mat[i][j] = 1; ans++; } } } public static void init(){ ans = 0; for(int i=0; i<LEN; ++i) for(int j=0; j<LEN; ++j) mat[i][j] = 0; } public static void main(String[] args) { int n; n = cin.nextInt(); cin.nextLine(); for(int i=0; i<n; ++i){ init(); p = 0; str = cin.nextLine(); dfs(0,0,LEN); p = 0; str = cin.nextLine(); dfs(0,0,LEN); System.out.println("There are "+ans+" black pixels."); } } }
View Code
相关文章推荐
- UVA 297 - Quadtrees
- UVA 297 Quadtrees(四叉树)
- UVA - 297:Quadtrees
- UVa 297 Quadtrees -SilverN
- 例题6-11 四分树(Quadtrees, UVa 297)
- UVA-297 Quadtrees (四分树)
- uva-297 Quadtrees
- UVa 297 - Quadtrees解题报告
- Uva297——Quadtrees
- UVa297 例题6-11 四分树(Quadtrees)
- UVa 297 Quadtrees(模拟&四分树)
- 297 - Quadtrees (UVa)
- uva 297 quadtrees——yhx
- 紫书第六章-----数据结构基础(例题6-11 四分树Quadtrees UVA - 297)
- uva297 - Quadtrees
- uva 297 Quadtrees
- UVA 297 Quadtrees(四叉树建树、合并与遍历)
- UVA.297 Quadtrees (四分树 DFS)
- 6-11 四分树(Quadtrees, UVa 297)
- UVA 297 Quadtrees( 数据结构,树)