232 - Crossword Answers(书上习题3-6)
做了差不多三个小时,不难,就是比较麻烦,一次AC
慢的原因:
1.思路开始不清晰,开始还想把横着的竖着的当成不同的思路来处理。。其实是一样的,把竖着的写出来后又改了横着的,i,j 差不多换换就行。
2.思路不清晰,不是 i==0||j==0 而只能是其中一个的,后来才改,空格才对了,而这时候如果还要加什么判断条件什么的就复杂死了,处理问题的时候想办法让它们和谐统一,尽量化成一种问题。
(处理问题之前把问题想清楚了再打,横竖分别思路归一,满足什么条件,如何判断)
3.其实下面代码比较冗长,有的地方是可以合并的。
1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 int main() 5 { 6 int m,n,w=1,l=0; 7 while(scanf("%d",&m)==1&&m!=0) 8 { 9 if(l++) printf("\n"); //又是这种输出格式 10 scanf("%d",&n); 11 getchar(); 12 char ch[m][n]; 13 for(int i=0;i<m;i++) 14 { 15 for(int j=0;j<n;j++) 16 { 17 scanf("%c",&ch[i][j]); 18 } 19 getchar(); 20 } 21 int k=1; 22 int num[m][n]; 23 for(int i=0;i<m;i++) 24 { 25 for(int j=0;j<n;j++) 26 { 27 if((i==0)||(j==0)) 28 { 29 if(ch[i][j]!='*')num[i][j]=k++; 30 else num[i][j]=0; 31 } 32 else if(ch[i-1][j]=='*'&&ch[i][j]!='*'||ch[i][j-1]=='*'&&ch[i][j]!='*') 33 num[i][j]=k++; 34 else num[i][j]=0; 35 } 36 37 } 38 printf("puzzle #%d:\n",w++); 39 printf("Across\n"); 40 for(int i=0;i<m;i++) 41 { 42 for(int j=0;j<n;j++) 43 { 44 if(j==0) 45 { 46 if(ch[i][j]!='*') 47 { 48 printf("%3d.",num[i][j]); 49 int cj=j; 50 while(cj<n&&ch[i][cj]!='*') 51 { 52 printf("%c",ch[i][cj++]); 53 } 54 printf("\n"); 55 } 56 } 57 else if(ch[i][j-1]=='*'&&ch[i][j]!='*') 58 { 59 printf("%3d.",num[i][j]); 60 int cj=j; 61 while(cj<n&&ch[i][cj]!='*') 62 { 63 printf("%c",ch[i][cj++]); 64 } 65 printf("\n"); 66 } 67 68 } 69 } 70 printf("Down\n"); 71 for(int i=0;i<m;i++) 72 { 73 for(int j=0;j<n;j++) 74 { 75 if(i==0) 76 { 77 if(ch[i][j]!='*') 78 { 79 80 printf("%3d.",num[i][j]); 81 int ci=i; 82 while(ci<m&&ch[ci][j]!='*') 83 { 84 printf("%c",ch[ci++][j]); 85 } 86 printf("\n"); 87 } 88 } 89 else if(ch[i-1][j]=='*'&&ch[i][j]!='*') 90 { 91 printf("%3d.",num[i][j]); 92 int ci=i; 93 while(ci<m&&ch[ci][j]!='*') 94 { 95 printf("%c",ch[ci++][j]); 96 } 97 printf("\n"); 98 } 99 } 100 } 101 } 102 return 0; 103 }

浙公网安备 33010602011771号