-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.cpp
93 lines (76 loc) · 1.79 KB
/
grid.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <bits/stdc++.h>
using namespace std;
void swap(char*a,char *b)
{
char temp = *a;
*a=*b;
*b=temp;
}
void bubbleSort(char*a,int n) // call by address
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i;j++)
{
if( a[j+1]<a[j] && (j+1)<n) // increasing order
swap(&a[j+1],&a[j]);
}
}
}
int main()
{
int t;
cin>>t;
while( t-- )
{
int n;
cin>>n;
char grid[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>grid[i][j];
}
}
for(int i=0;i<n;i++)
{
bubbleSort(grid[i],n);
}
bool row = true;
bool column = true;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
//cout<<grid[i][j]<<" ";
if( (grid[i][j+1] < grid[i][j]) && (j+1)<n) // comparing the elements rowwise
{
row = false;
cout<<"In the row"<<endl;
cout<<" Values \t"<<grid[i][j+1]<<" "<<grid[i][j]<<endl;
}
if( grid[i+1][j] < grid[i][j] && (i+1)<n) // comparing the elements columnwise
{
column = false;
cout<<"in the column"<<endl;
}
}
//cout<<endl;
}
// cout<<(row && column)<<endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<grid[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
if( row && column)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}