Codeforces Round #369 (Div. 2) B. Chris and Magic Square
// This is all about 311 lines code
// I am request you to please see other code if you are incapable to solve it
Accepted Code Link: B. Chris and Magic Square
//#include<stdio.h>
//#include<iostream>
//#include<algorithm>
//#include<vector>
//#include<cstring>
//#include<cmath>
//#include<map>
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define high 505
typedef long long LL;
LL ar[high][high];
LL d=0 , diagsum1=0, diagsum2=0;
bool gotnm=false;
int n , row=0, col=0;
void checkWhole()
{
LL rowsum=0, colsum=0, tmprow=0, tmpcol=0 , d1=0, d2=0 , ans1=0, ans2=0;
int i,j;
bool decision=false;
for(i=0; i<n; i++)
{
rowsum=0;
colsum=0;
for(j=0; j<n; j++)
{
rowsum += ar[i][j];
colsum += ar[j][i];
}
if(tmpcol==0 and tmprow==0)
{
tmprow = rowsum;
tmpcol = colsum;
//cout << rowsum << " " << tmprow << "\n" << colsum << " " << tmpcol << "; ";
}
else
{
d1 = rowsum > tmprow ? rowsum - tmprow : tmprow - rowsum;
d2 = colsum > tmpcol ? colsum - tmpcol : tmpcol - colsum;
if(d1 > 0)
{
if(rowsum > tmprow) tmprow += d1;
else rowsum += d1;
ans1 = d1;
}
if(d2 > 0)
{
if(colsum > tmpcol) tmpcol += d2;
else colsum += d2;
ans2 = d2;
}
//cout << rowsum << " " << tmprow << "\n" << colsum << " " << tmpcol << "; ";
// if(tmprow != rowsum or tmpcol != colsum)
// {
// decision=true;
// break;
// }
tmprow = rowsum;
tmpcol = colsum;
}
}
//if(decision) outn("-1");
//cout << ans1 << " " << ans2 << "\n";
ar[row][col] = ans2;
tmprow=0;
tmpcol=0;
for(i=0; i<n; i++)
{
rowsum=0;
colsum=0;
for(j=0; j<n; j++)
{
rowsum+=ar[i][j];
colsum+=ar[j][i];
}
if(tmprow == 0 and tmpcol==0)
{
tmprow = rowsum;
tmpcol = colsum;
}
else
{
if(tmprow != rowsum or tmpcol != colsum)
{
decision=true;
break;
}
else
{
tmprow = rowsum;
tmpcol = colsum;
}
}
}
//cout << "tmprow = " << tmprow << " tmpcol = " << tmpcol;
//cout << "des = " << decision;
//cout << "diag1 = " << diagsum1 << " " << "diag2 = " << diagsum2;
if(decision) cout << "-1\n";
else
{
if(d == 0)
{
//cout << "d==0";
diagsum1+=ar[row][col];
diagsum2+=ar[row][col];
}
if((tmprow == diagsum1 and tmpcol==diagsum2) or (rowsum==diagsum1 and colsum==diagsum2))
{
if(ar[row][col] == 0) cout << "-1\n";
else cout << ar[row][col] << "\n";
// outn(ar[row][col]);
}
else cout << "-1\n";
}
}
void checkRowColumn()
{
LL rowsum=0 , colsum=0 , tmprow=0, tmpcol=0;
int i,j;
bool decision = false;
for(i=0; i<n; i++)
{
rowsum=0;
colsum=0;
for(j=0; j<n; j++)
{
rowsum+=ar[i][j];
colsum+=ar[j][i];
}
if(tmprow==0 and tmpcol==0)
{
tmprow = rowsum;
tmpcol = colsum;
}
else
{
if(tmprow != rowsum or tmpcol != colsum)
{
decision=true;
break;
}
else
{
tmprow = rowsum;
tmpcol = colsum;
}
}
}
//cout << rowsum << " " << colsum << "\n";
if(decision) outn("-1");
else
{
if((tmprow == diagsum1 and tmpcol==diagsum2) or (rowsum==diagsum1 and colsum==diagsum2))
{
if(ar[row][col] == 0) cout << "-1\n";
else cout << ar[row][col] << "\n";
}
else cout << "-1\n";
}
}
void other()
{
//outn("it is a other function");
if(gotnm)
{
checkRowColumn();
}
else
{
//outn("gotnm false");
checkWhole();
}
}
int main()
{
fast;
while(cin >> n)
{
int i,j;
if(n == 1)
{
cin >> ar[0][0];
cout << "1\n";
continue;
}
gotnm=false;
d=-1;
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
cin >> ar[i][j];
if(ar[i][j] == 0)
{
row=i;
col=j;
}
}
}
// diag 1
//outn("diag 1");
diagsum1 = 0;
bool diag1zero=false, diag2zero=false;
for(i=0; i<n; i++)
{
//cout << ar[i][i] << " ";
if(ar[i][i] == 0) diag1zero=true;
diagsum1+=ar[i][i];
}
// diag 2
//outn("diag 2");
diagsum2=0;
for(i=0; i<n; i++)
{
//cout << ar[i][j] << " ";
if(ar[i][n - i - 1] == 0) diag2zero = true;
diagsum2+=ar[i][n - i - 1];
}
//cout << diag1zero << " " << diag2zero << " " << d << "\n";
//cout << diagsum1 << " " << diagsum2 << " ";
if(diag1zero==true or diag2zero==true)
{
d = diagsum1 > diagsum2 ? diagsum1 - diagsum2 : diagsum2 - diagsum1;
if(diagsum1 > diagsum2) diagsum2 += d;
else diagsum1 += d;
}
//cout << "d = " << d;
//cout << "diagsum1 = " << diagsum1 << " diagsum2 = " << diagsum2 << " ";
//cout << diag1zero << " " << diag2zero << "\n";
if(diagsum1 == diagsum2)
{
//other();
if(diag1zero==true or diag2zero == true)
{
//cout << diag1zero << " " << diag2zero << " " << d << "\n";
//outn(d);
if(d == 0)
{
checkWhole();
continue;
}
ar[row][col] = d;
gotnm=true;
}
other();
}
else cout << "-1\n";
}
return 0;
}
/*
4
1 1 1 1
1 1 0 1
1 1 1 1
1 1 1 1
*/
// I am request you to please see other code if you are incapable to solve it
Accepted Code Link: B. Chris and Magic Square
//#include<stdio.h>
//#include<iostream>
//#include<algorithm>
//#include<vector>
//#include<cstring>
//#include<cmath>
//#include<map>
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define high 505
typedef long long LL;
LL ar[high][high];
LL d=0 , diagsum1=0, diagsum2=0;
bool gotnm=false;
int n , row=0, col=0;
void checkWhole()
{
LL rowsum=0, colsum=0, tmprow=0, tmpcol=0 , d1=0, d2=0 , ans1=0, ans2=0;
int i,j;
bool decision=false;
for(i=0; i<n; i++)
{
rowsum=0;
colsum=0;
for(j=0; j<n; j++)
{
rowsum += ar[i][j];
colsum += ar[j][i];
}
if(tmpcol==0 and tmprow==0)
{
tmprow = rowsum;
tmpcol = colsum;
//cout << rowsum << " " << tmprow << "\n" << colsum << " " << tmpcol << "; ";
}
else
{
d1 = rowsum > tmprow ? rowsum - tmprow : tmprow - rowsum;
d2 = colsum > tmpcol ? colsum - tmpcol : tmpcol - colsum;
if(d1 > 0)
{
if(rowsum > tmprow) tmprow += d1;
else rowsum += d1;
ans1 = d1;
}
if(d2 > 0)
{
if(colsum > tmpcol) tmpcol += d2;
else colsum += d2;
ans2 = d2;
}
//cout << rowsum << " " << tmprow << "\n" << colsum << " " << tmpcol << "; ";
// if(tmprow != rowsum or tmpcol != colsum)
// {
// decision=true;
// break;
// }
tmprow = rowsum;
tmpcol = colsum;
}
}
//if(decision) outn("-1");
//cout << ans1 << " " << ans2 << "\n";
ar[row][col] = ans2;
tmprow=0;
tmpcol=0;
for(i=0; i<n; i++)
{
rowsum=0;
colsum=0;
for(j=0; j<n; j++)
{
rowsum+=ar[i][j];
colsum+=ar[j][i];
}
if(tmprow == 0 and tmpcol==0)
{
tmprow = rowsum;
tmpcol = colsum;
}
else
{
if(tmprow != rowsum or tmpcol != colsum)
{
decision=true;
break;
}
else
{
tmprow = rowsum;
tmpcol = colsum;
}
}
}
//cout << "tmprow = " << tmprow << " tmpcol = " << tmpcol;
//cout << "des = " << decision;
//cout << "diag1 = " << diagsum1 << " " << "diag2 = " << diagsum2;
if(decision) cout << "-1\n";
else
{
if(d == 0)
{
//cout << "d==0";
diagsum1+=ar[row][col];
diagsum2+=ar[row][col];
}
if((tmprow == diagsum1 and tmpcol==diagsum2) or (rowsum==diagsum1 and colsum==diagsum2))
{
if(ar[row][col] == 0) cout << "-1\n";
else cout << ar[row][col] << "\n";
// outn(ar[row][col]);
}
else cout << "-1\n";
}
}
void checkRowColumn()
{
LL rowsum=0 , colsum=0 , tmprow=0, tmpcol=0;
int i,j;
bool decision = false;
for(i=0; i<n; i++)
{
rowsum=0;
colsum=0;
for(j=0; j<n; j++)
{
rowsum+=ar[i][j];
colsum+=ar[j][i];
}
if(tmprow==0 and tmpcol==0)
{
tmprow = rowsum;
tmpcol = colsum;
}
else
{
if(tmprow != rowsum or tmpcol != colsum)
{
decision=true;
break;
}
else
{
tmprow = rowsum;
tmpcol = colsum;
}
}
}
//cout << rowsum << " " << colsum << "\n";
if(decision) outn("-1");
else
{
if((tmprow == diagsum1 and tmpcol==diagsum2) or (rowsum==diagsum1 and colsum==diagsum2))
{
if(ar[row][col] == 0) cout << "-1\n";
else cout << ar[row][col] << "\n";
}
else cout << "-1\n";
}
}
void other()
{
//outn("it is a other function");
if(gotnm)
{
checkRowColumn();
}
else
{
//outn("gotnm false");
checkWhole();
}
}
int main()
{
fast;
while(cin >> n)
{
int i,j;
if(n == 1)
{
cin >> ar[0][0];
cout << "1\n";
continue;
}
gotnm=false;
d=-1;
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
cin >> ar[i][j];
if(ar[i][j] == 0)
{
row=i;
col=j;
}
}
}
// diag 1
//outn("diag 1");
diagsum1 = 0;
bool diag1zero=false, diag2zero=false;
for(i=0; i<n; i++)
{
//cout << ar[i][i] << " ";
if(ar[i][i] == 0) diag1zero=true;
diagsum1+=ar[i][i];
}
// diag 2
//outn("diag 2");
diagsum2=0;
for(i=0; i<n; i++)
{
//cout << ar[i][j] << " ";
if(ar[i][n - i - 1] == 0) diag2zero = true;
diagsum2+=ar[i][n - i - 1];
}
//cout << diag1zero << " " << diag2zero << " " << d << "\n";
//cout << diagsum1 << " " << diagsum2 << " ";
if(diag1zero==true or diag2zero==true)
{
d = diagsum1 > diagsum2 ? diagsum1 - diagsum2 : diagsum2 - diagsum1;
if(diagsum1 > diagsum2) diagsum2 += d;
else diagsum1 += d;
}
//cout << "d = " << d;
//cout << "diagsum1 = " << diagsum1 << " diagsum2 = " << diagsum2 << " ";
//cout << diag1zero << " " << diag2zero << "\n";
if(diagsum1 == diagsum2)
{
//other();
if(diag1zero==true or diag2zero == true)
{
//cout << diag1zero << " " << diag2zero << " " << d << "\n";
//outn(d);
if(d == 0)
{
checkWhole();
continue;
}
ar[row][col] = d;
gotnm=true;
}
other();
}
else cout << "-1\n";
}
return 0;
}
/*
4
1 1 1 1
1 1 0 1
1 1 1 1
1 1 1 1
*/
Comments
Post a Comment