-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay18.cpp
66 lines (58 loc) · 1.98 KB
/
Day18.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
long long int MissingNo(vector<vector<int> >& matrix) {
int n = matrix.size();
long long int affected = -1;
long long int unaffected = -1;
long long int diago1 = 0;
long long int diago2 = 0;
bool d1aff = 0;
bool d2aff = 0;
for ( int i = 0; i<n; i++ ){
long long int row = 0;
long long int col = 0;
bool caff = 0;
bool raff = 0;
for ( int j = 0; j<n; j++ ){
col += matrix[j][i];
row += matrix[i][j];
if ( matrix[i][j] == 0) raff = 1;
if ( matrix[j][i] == 0 ) caff = 1;
}
if ( affected == -1 ){
if ( raff )
affected = row;
if ( caff )
affected = col;
}
if ( unaffected == -1 ){
if ( !raff )
unaffected = row;
if ( !caff )
unaffected = col;
}
if ( raff && affected != row )
return -1;
if ( caff && affected != col )
return -1;
if ( !raff && unaffected != row )
return -1;
if ( !caff && unaffected != col)
return -1;
diago1 += matrix[i][i];
diago2 += matrix[i][n-i-1];
if ( matrix[i][i] == 0)
d1aff = 1;
if ( matrix[i][n-1-i] == 0)
d2aff = 1;
}
if ( unaffected <= affected)
return -1;
if ( d1aff && affected != diago1 )
return -1;
if ( d2aff && affected != diago2 )
return -1;
if ( !d1aff && unaffected != diago1)
return -1;
if ( !d2aff && unaffected != diago2 )
return -1;
return unaffected-affected;
}