题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=4&page=show_problem&problem=233
说明:用书中的思路,一会的功夫写下来了,但是找bug找了好久,发现是数组开小了,但是汝佳的代码s[1024+10]就过了,我的代码s[1024+1024+10]开的数组大了一倍才过,不明白为什么。
#include<stdio.h> #include<string.h> int cnt; int vis[32][32]; const int maxn=1024+1024+10;//汝佳的代码maxn=1024+10,这里不明白 char s[maxn]; void draw(const char* s,int& p,int r,int c,int w){ char ch=s[p++]; if(ch=='p'){ draw(s,p,r, c+w/2,w/2); draw(s,p,r, c, w/2); draw(s,p,r+w/2,c, w/2); draw(s,p,r+w/2,c+w/2,w/2); }else if(ch=='f'){ for(int i=r;i<r+w;i++) for(int j=c;j<c+w;j++) if(vis[i][j]==0) {vis[i][j]=1;cnt++; } } } int main(){ int T; scanf("%d",&T); while(T--){ cnt=0; memset(vis,0,sizeof(vis)); for(int i=0;i<2;i++){ scanf("%s",s); int p=0; draw(s,p,0,0,32); } printf("There are %d black pixels.\n",cnt); } return 0; }