您的位置:首页 > 其它

ZOJ-#3465 The Hive(字符串模拟)

2014-09-16 20:35 323 查看
题目大意:给出蜂箱的布局,现在蜜蜂开始按照不同列,从下向上的顺序开始,当上下相邻的两个相同则合并为一个糖果,每一列满了之后,将不会发生任何变化,问有多个糖果产生,以及每一次测试后的蜂箱的布局。

解题思路:题目描述的不是很清楚,这是一个体力的模拟,直接模拟就可以搞定,但需要很多体力去做,细心一点就可以了。这里就不赘述了,详见code。

题目来源:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3465

code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int MAXN = 15;
int n,ans;
char p,q;
int a,b,c,d,e,f,g,h,i;
char A[MAXN],B[MAXN],C[MAXN],D[MAXN],E[MAXN],F[MAXN],G[MAXN],H[MAXN],I[MAXN];
char hive[40][30];
char str[40][30]={
"         _",
"       _/ \\_",
"     _/ \\_/ \\_",
"   _/ \\_/ \\_/ \\_",
" _/ \\_/ \\_/ \\_/ \\_",
"/ \\_/ \\_/ \\_/ \\_/ \\",
"\\_/ \\_/ \\_/ \\_/ \\_/",
"/ \\_/ \\_/ \\_/ \\_/ \\",
"\\_/ \\_/ \\_/ \\_/ \\_/",
"/ \\_/ \\_/ \\_/ \\_/ \\",
"\\_/ \\_/ \\_/ \\_/ \\_/",
"/ \\_/ \\_/ \\_/ \\_/ \\",
"\\_/ \\_/ \\_/ \\_/ \\_/",
"/ \\_/ \\_/ \\_/ \\_/ \\",
"\\_/ \\_/ \\_/ \\_/ \\_/",
"/ \\_/ \\_/ \\_/ \\_/ \\",
"\\_/ \\_/ \\_/ \\_/ \\_/",
"/ \\_/ \\_/ \\_/ \\_/ \\",
"\\_/ \\_/ \\_/ \\_/ \\_/",
"  \\_/ \\_/ \\_/ \\_/",
"    \\_/ \\_/ \\_/",
"      \\_/ \\_/",
"        \\_/"
};

void init(){
ans=0;
a=b=c=d=e=f=g=h=i=0;
memcpy(hive,str,sizeof(str)); //记得初始化,这里WA了好多次
for(int j=0;j<10;j++)
memset(A+j,0,sizeof(A+j));
}

void prin(){ //更改对应位置的值
if(a!=0) for(int j=a;j>0;j--) hive[19-2*j][1]=A[j];
if(b!=0) for(int j=b;j>0;j--) hive[20-2*j][3]=B[j];
if(c!=0) for(int j=c;j>0;j--) hive[21-2*j][5]=C[j];
if(d!=0) for(int j=d;j>0;j--) hive[22-2*j][7]=D[j];
if(e!=0) for(int j=e;j>0;j--) hive[23-2*j][9]=E[j];
if(f!=0) for(int j=f;j>0;j--) hive[22-2*j][11]=F[j];
if(g!=0) for(int j=g;j>0;j--) hive[21-2*j][13]=G[j];
if(h!=0) for(int j=h;j>0;j--) hive[20-2*j][15]=H[j];
if(i!=0) for(int j=i;j>0;j--) hive[19-2*j][17]=I[j];
for(int j=0;j<23;j++){
printf("%s\n",hive[j]);
}
}

int main(){
//freopen("input.txt","r",stdin);
while(~scanf("%d",&n)){
getchar();
init();
for(int j=0;j<n;j++){
scanf("%c%c",&p,&q);
getchar();
if(p=='A' && a<7){ //对应列的计数
A[++a]=q;
if(A[a]==A[a-1] && a!=1){ans++;a-=2;continue;}
}
else if(p=='B' && b<8){
B[++b]=q;
if(B[b]==B[b-1] && b!=1){ans++;b-=2;continue;}
}
else if(p=='C' && c<9){
C[++c]=q;
if(C[c]==C[c-1] && c!=1){ans++;c-=2;continue;}
}
else if(p=='D' && d<10){
D[++d]=q;
if(D[d]==D[d-1] && d!=1){ans++;d-=2;continue;}
}
else if(p=='E' && e<11){
E[++e]=q;
if(E[e]==E[e-1] && e!=1){ans++;e-=2;continue;}
}
else if(p=='F' && f<10){
F[++f]=q;
if(F[f]==F[f-1] && f!=1){ans++;f-=2;continue;}
}
else if(p=='G' && g<9){
G[++g]=q;
if(G[g]==G[g-1] && g!=1){ans++;g-=2;continue;}
}
else if(p=='H' && h<8){
H[++h]=q;
if(H[h]==H[h-1] && h!=1){ans++;h-=2;continue;}
}
else if(p=='I' && i<7){
I[++i]=q;
if(I[i]==I[i-1] && b!=1){ans++;i-=2;continue;}
}
}
printf("The number of candy is %d.\n",ans);
prin();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: