From the matrix a crossword diagram can be drawn. In the diagram each square is represented by a box 4 x 6 characters. Black square and white squares (numbered and not numbered square) are represented as follows (where nnn is the number of the square):
++++++ ++++++ ++++++ ++++++ +nnn + + + ++++++ + + + + ++++++ ++++++ ++++++
The remaining characters of the box are spaces. If black squares are given at the edges, they should be removed from the diagram (see the example). Only use spaces as necessary filling characters. Don't use any unnecessary spaces at the end of the line.
Input
The input consists of several blocks of lines each representing a crossword. Each block starts with the line containing two integers m < 25 and n < 25 separated by one space. In each of the next m lines there are n numbers 0 or 1, separated by one space. The last block will be empty, m = n = 0.
Output
The output contains the corresponding crossword diagram for each except the last block. After each diagram there are two empty lines.
Sample Input
6 7 1 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 1 1 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 5 3 1 0 1 0 0 0 1 1 1 0 0 0 1 0 1 0 0
Sample Output
+++++++++++++++++++++ +001 + +002 +003 + + + + + + ++++++++++++++++++++++++++++++++++++ +004 + ++++++005 + +006 +007 + + + ++++++ + + + + ++++++++++++++++++++++++++++++++++++ +008 + +009 + + +010 + + + + + + + + + + +++++++++++++++++++++ +++++++++++ + ++++++011 + + + ++++++ + + ++++++++++++++++++++++++++++++++++++ +012 +013 + ++++++014 +015 + + + + + ++++++ + + + ++++++++++++++++++++++++++++++++++++ +016 + + + + + + + + + + + ++++++++++++++++++++++++++ ++++++ +001 + + + ++++++++++++++++ +002 + + + + + + + ++++++++++++++++ ++++++++++++++++ +003 +004 + + + + + + ++++++++++++++++ + + + + ++++++
Source: Central European 1996
先将输入矩阵处理为标识性的中间矩阵,再把中间矩阵转化为输出,感觉是很蠢的方法。。。
#include <iostream> #include <memory.h> #include <stdlib.h> using namespace std; int a[25][25]; int b[25][25]; char *c; void display(int m,int n){ for(int i = 0; i < m; i++) { for(int j = 0;j < n; j++) cout << b[i][j]<<" "; cout<<endl; } } void vmark(int i,int j,int m,int n){ b[i][j] = -2; //display(m,n); if(i>0 && b[i-1][j]!=-2 && a[i-1][j]<0) vmark(i-1,j,m,n); if(i<m-1 && b[i+1][j]!=-2 && a[i+1][j]<0) vmark(i+1,j,m,n); if(j>0 && b[i][j-1]!=-2 && a[i][j-1]<0) vmark(i,j-1,m,n); if(j<n-1 && b[i][j+1]!=-2 && a[i][j+1]<0) vmark(i,j+1,m,n); } int main(){ int m,n,i,j,q,s,f,ti,tj; int g = 0; while(cin>>m>>n) { if(m == 0 || n == 0) break; int d = (m*3+1)*(n*5+1); c = new char[d]; memset(c,' ',sizeof(char)*d); memset(a,0,sizeof(int)*625); memset(b,0,sizeof(int)*625); for(i = 0; i < m; i++) { for(j = 0;j < n; j++) { cin >> a[i][j]; if(a[i][j] == 1) a[i][j] = -1; } } q = 1;//数字 for(i = 0; i < m; i++) { for(j = 0; j < n; j++){ if(a[i][j] < 0) { if(i==0 || j==0 || j==n-1 || i==m-1){ if(b[i][j] != -2) vmark(i,j,m,n); } else if(b[i][j] != -2) b[i][j] = -1; } else{ if((i==0 || a[i-1][j]<0) && i+1!=m && a[i+1][j]==0){ b[i][j]=q;q++; } else if((j==0 || a[i][j-1]<0) && j+1!=n && a[i][j+1]==0){ b[i][j]=q;q++; } } } } for(i = 0; i < m; i++) { for(j = 0;j < n; j++) { if(b[i][j] == -2) continue; else if(b[i][j] == -1){ ti = i*3; tj = j*5; for(s=0; s<4; s++) { for(f=0; f<6; f++) { c[(ti+s)*(5*n+1)+tj+f] = '+'; } } } else if(b[i][j] >= 0){ ti = i*3; tj = j*5; for(f=0; f<6; f++) { c[(ti+0)*(5*n+1)+tj+f] = '+'; c[(ti+3)*(5*n+1)+tj+f] = '+'; } for(s=0; s<4; s++) { c[(ti+s)*(5*n+1)+tj+0] = '+'; c[(ti+s)*(5*n+1)+tj+5] = '+'; } if(b[i][j] > 0) { int k = b[i][j] / 10; c[(ti+1)*(5*n+1)+tj+3] = b[i][j] % 10 + '0'; c[(ti+1)*(5*n+1)+tj+2] = k % 10 + '0'; c[(ti+1)*(5*n+1)+tj+1] = k / 10 + '0'; } } } } for(i=0; i<m*3+1; i++){ for(j=n*5;j>=0;j--){ if(c[i*(5*n+1)+j]!=' ') break; } for(s=0;s<=j;s++){ cout<<c[i*(5*n+1)+s]; } cout<<endl; } cout<<endl;cout<<endl; } return 0; }
有个地方要注意,输出的每一行结尾到了空白处就要换行,不要用空格。。。