您的位置:首页 > 其它

uva297(quadtree)

2015-04-26 08:49 183 查看
给我们两棵quadtree的前序遍历,要我吗求原来32*32的矩阵有多少个位置是黑的

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: