我先求出交叉的gird,然后再一行一行求得。感觉还可以吧。思路比较清晰,开始想的是数是第几行然后从每一行的前后开始控制,好麻烦的感觉,我就先求出来了框架再做就好做多啦!后来PE,突然发现我特殊处理n=1的时候一下子输出了2个 ,改掉以后就好了。
#include <stdio.h> #include <math.h> #include <string.h> #define bug puts("bug!bug!bug!!!!!"); char gird[100][100]; int main(){ int n,i,j,cnt,g; int isfirst=1; char gird[100][100],in,out,oout; while(~scanf("%d %c %c",&n,&in,&out)){ if(isfirst==0) printf(" "); if(n==1){ printf("%c ",in); continue; } for(i=0;i<n;++i) for(j=0;j<n;++j) gird[i][j]=' '; if( (n+1)/2%2==0 ) oout=out; else oout=in; for(i=0;i<n;++i) gird[i][0]=oout; for(i=1,j=1;i<n;++i,++j){ if(gird[i-1][j-1]==out) gird[i][j]=in; else gird[i][j]=out; } //bug; for(i=n-2,j=1;i>=0;--i,++j){ if(gird[i+1][j-1]==out) gird[i][j]=in; else gird[i][j]=out; }/* for(i=0;i<n;++i){ for(j=0;j<n;++j){ printf("%c",gird[i][j]); } printf(" "); }*/ for(i=0;i<n;++i) gird[n-1][i]=gird[0][i]=oout; for(i=1;i<n-1;++i){ cnt=0;g=0; for(j=1;j<n;++j){ if(gird[i][j]==out||gird[i][j]==in) g++; if(i==j&&i==(n-1)/2 ) g++; if(cnt==1){ gird[i][j]=gird[i][j-1]; }else if(cnt==2||cnt==0){ if(gird[i][j-1]==out) gird[i][j]=in; else gird[i][j]=out; } cnt=g; } } gird[0][0]=gird[n-1][n-1]=gird[0][n-1]=gird[n-1][0]=' '; for(i=0;i<n;++i){ for(j=0;j<n;++j){ printf("%c",gird[i][j]); } printf(" "); } isfirst=0; } return 0; }