uva232(纵横字谜)。
book数组用来判断起始点。
PR了四次,迷之换行。。。
1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <vector> 5 #include <cmath> 6 #include <cstring> 7 8 using namespace std; 9 char Map[11][11]; 10 int book1[11][11]; 11 int book2[11][11]; 12 13 int judge(int r,int c) 14 { 15 if((r-1<0||c-1<0||Map[r][c-1]=='*'||Map[r-1][c]=='*')&&(Map[r][c]!='*')) 16 return 1; 17 return 0; 18 } 19 int borderC(int rr,int cc,int r,int c) 20 { 21 if(cc+1>=c||Map[rr][cc+1]=='*')return 1; 22 return 0; 23 } 24 int borderR(int rr,int cc,int r,int c) 25 { 26 if(rr+1>=r||Map[rr+1][cc]=='*')return 1; 27 return 0; 28 } 29 int main() 30 { 31 int r,c,i,j,k=0,p,q; 32 while(cin >> r && r) 33 { 34 cin >> c; 35 memset(book1,0,sizeof(book1)); 36 memset(book2,0,sizeof(book2)); 37 for(i=0;i<r;i++) 38 scanf("%s",Map[i]); 39 if(k)printf("\n");//唯一坑的地方!!! 40 printf("puzzle #%d:\n",++k); 41 int cnt = 0; 42 for(i=0;i<r;i++) 43 { 44 for(j=0;j<c;j++) 45 { 46 if(judge(i,j)) 47 { 48 cnt++; 49 book1[i][j]=cnt; 50 book2[i][j]=cnt; 51 } 52 } 53 } 54 printf("Across\n"); 55 for(i=0;i<r;i++) 56 { 57 for(j=0;j<c;j++) 58 { 59 if(book1[i][j]) 60 { 61 printf("%3d.",book1[i][j]); 62 for(q=j;borderC(i,q-1,r,c)==0;q++) 63 { 64 printf("%c",Map[i][q]); 65 if(book1[i][q]) 66 book1[i][q] = 0; 67 } 68 printf("\n"); 69 } 70 } 71 } 72 printf("Down\n"); 73 for(i=0;i<r;i++) 74 { 75 for(j=0;j<c;j++) 76 { 77 if(book2[i][j]) 78 { 79 printf("%3d.",book2[i][j]); 80 for(p=i;borderR(p-1,j,r,c)==0;p++) 81 { 82 printf("%c",Map[p][j]); 83 if(book2[p][j]) 84 book2[p][j] = 0; 85 } 86 printf("\n"); 87 } 88 } 89 } 90 } 91 return 0; 92 }