又是我自己坑自己,第三个测试点没过,最后一看,原来是进入BFS的点没有置成false
#include
#include
using namespace std;
bool slice[61][1290][130];
int m,n,l,t,total;
int x[6] = {0,0,0,0,1,-1};
int y[6] = {0,0,1,-1,0,0};
int z[6] = {1,-1,0,0,0,0};
struct pos{
pos(int a,int b,int c){
i = a,j = b,k = c;
}
int i,j,k;
};
bool check(int i, int j, int k){
if(i >= 0 && i < l && j >= 0 && j< m && k >= 0 && k q;
pos p(i,j,k);int sum = 1;
q.push(p);
while(q.size() != 0){
p = q.front();q.pop();
for(int i = 0; i < 6;i++)
if(check(p.i + x[i],p.j + y[i],p.k+z[i])){
sum++;
pos temp(p.i + x[i],p.j + y[i],p.k+z[i]);
q.push(temp);
}
}
return sum;
}
int main(){
scanf("%d %d %d %d",&m,&n,&l,&t);
for(int i = 0;i < l;i++)
for(int j = 0; j < m;j++)
for(int k = 0;k < n;k++)
scanf("%d",&slice[i][j][k]);
for(int i = 0;i < l;i++)
for(int j = 0; j < m;j++)
for(int k = 0;k < n;k++)
if(slice[i][j][k]) {
slice[i][j][k] = false;
int temp = BFS(i,j,k);
if(temp >= t) total += temp;
}
printf("%d",total);
}
A1020
不需要建树,纯数学,直接从中序后序转层序
#include
#include
using namespace std;
int a[40],b[40],c[40];
struct node{
node(int i,int j,int k,int p){postleft = i,postright = j,inleft = k,inright = p;}
int postleft,postright;
int inleft,inright;
};
int main(){
int n;scanf("%d",&n);
for(int i = 0; i < n;i++) scanf("%d",&a[i]);//postorder
for(int i = 0; i < n;i++) scanf("%d",&b[i]);
queue q;
node temp(0,n - 1,0,n - 1);
if(n > 0)q.push(temp);bool jud = false;//jud用来判断现在要不要输出空格
while(!q.empty()){
temp = q.front();q.pop();//每次产生左右子树并且把相应的编号给压进去
if(jud)printf(" %d",a[temp.postright]);
else{printf("%d",a[temp.postright]);jud = true;}
int pos = temp.inleft,postleft,postright,inleft,inright;//post in
while(b[pos] != a[temp.postright]) pos ++;
inleft = temp.inleft,inright = pos - 1;//先计算左子树
postleft = temp.postleft; postright = temp.postleft + (pos - temp.inleft) - 1;
if(postright >= postleft){
node in(postleft,postright,inleft,inright);
q.push(in);
}
inleft = pos + 1,inright = temp.inright;
postleft = temp.postleft + (pos - temp.inleft),postright = temp.postright - 1;
if(postright >= postleft){
node in(postleft,postright,inleft,inright);
q.push(in);
}
}
}
又敲了一遍要建树的
#include//使用建树得到
#include
using namespace std;
const int maxn = 50;
int in[maxn],post[maxn];
struct node{
node(int d){data = d;}
int data;
node* lchild,*rchild;
};
node* CreateTree(int inleft,int inright,int postleft,int postright){
if(inleft > inright) return NULL;
node* root = new node(post[postright]);
int pos = inleft;
while(in[pos] != post[postright]) pos++;
root -> lchild = CreateTree(inleft,pos - 1,postleft,postleft + pos - inleft - 1);
root -> rchild = CreateTree(pos + 1,inright,postleft + pos - inleft,postright - 1);
return root;
}
void BFS(node* root){
queue q;bool jud = true;
if(root!=NULL)q.push(root);
while(!q.empty()){
root = q.front();q.pop();
if(jud){printf("%d",root->data);jud = false;}
else printf(" %d",root->data);
if(root -> lchild != NULL) q.push(root -> lchild);
if(root -> rchild != NULL) q.push(root -> rchild);
}
}
int main(){
int n;scanf("%d",&n);
for(int i =0;i < n;i++) scanf("%d",&post[i]);
for(int i =0;i < n;i++) scanf("%d",&in[i]);
BFS(CreateTree(0,n-1,0,n-1));
}