Berland Flag
Time limit per test: 0.5 second(s)
Memory limit: 65536 kilobytes
Memory limit: 65536 kilobytes
input: standard
output: standard
output: standard
The Berland is in trouble again. After the recent elections All-Berland Great Duma has divided into two coalitions: Blue-eyed coalition and Red-eyed coalition. On the independence day the following question was raised: what will the national flag look like? Both coalitions agreed that the flag should be a square of N2 x N2 cells, each of them painted blue of red. To make the flag acceptable for both coalitions, the following requirements must be held:
- every row of the flag must contain exactly K blue cells
- every column of the flag must contain exactly K blue cells
- if we split the flag into squares of N x N cells (there will be N x N such squares), then every such square must contain exactly K blue cells. You are the chief programmers in Berland. So, making the flag model is your duty.
InputFirst line of the input contains two integer numbers N and K (1 ≤ N ≤ 20, 0 ≤ K ≤ N2).
OutputOutput description of the desired flag. Print N2 lines which consist of N2 characters'*'
and'.'
, where'*'
means blue cell and'.'
means red cell. Output"NO SOLUTION"
(without quotes), if there is no flag which satisfies both coalitions. If there are several solutions, output any of them.
Example(s)sample input
sample output
2 2
*..* .**. .**. *..*
sample input
sample output
3 1
*........ ...*..... ......*.. .*....... ....*.... .......*. ..*...... .....*... ........*
样例很重要,这道题有爱之处就在于,第二个样例就把一个很简单的规律展现出来了。
#include <cstdlib> #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> using namespace std; char mp[500][500]; int n, K; void build(int ii, int jj) { const int PX= (jj-1)*n; const int PY= (ii-1)*n; int x= ii, y= jj; int cnt= 0; for(int k=0;k<K;k++) { mp[PX+x][PY+y]= '*'; y++; if (y>n) y= 1; if (y==jj) { x++; if (x>n) x= 1; } } } int main() { while(~scanf("%d%d", &n, &K)) { for(int i=1;i<=n*n;i++) for(int j=1;j<=n*n;j++) mp[i][j]= '.'; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) build(i, j); for(int j= 1;j<=n*n;j++) { for(int i=1;i<=n*n;i++) printf("%c", mp[i][j]); printf("\n"); } } }