-
Notifications
You must be signed in to change notification settings - Fork 6
/
GridFill(Codechef_Starter).cpp
46 lines (40 loc) · 1.29 KB
/
GridFill(Codechef_Starter).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
You are given a N × N grid. You need to fill each cell of the grid with 1 or −1. You need to fill the grid in a way such that the following conditions are met :-
For every column - (Sum of values present in the column) x (Product of values present in the column) < 0
For every row - (Sum of values present in the row) x (Product of values present in the row) < 0
It is guaranteed that there exists at least one way to fill the grid under given constraints such that both the conditions are satisifed. If there exists multiple ways to fill the grid by satisfying both the conditions, you can print any
*/
#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
if(n&1){
vector<vector<int>>arr(n,vector<int>(n,1));
int s=0;
for(int i=0;i<n;i++){
arr[i][s] = -1;
s++;
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<arr[i][j]<<" ";
}
cout<<"\n";
}
}
else{
vector<vector<int>>arr(n,vector<int>(n,-1));
for(int i = 0;i<n; i++){
for(int j=0;j<n;j++){
cout<<arr[i][j]<<" ";
}
cout<<"\n";
}
}
}
return 0;
}