Page 1 of 1

### 11470 - Square Sums

Posted: Tue Sep 09, 2008 11:14 pm
This post is not about asking for help so much as sharing approaches:

I travel the given square in a spiral-way adding the integers I find. When I get to a visited cell I tried to turn right and continue. After visiting all cells I stop.

Every time I turn and I'm facing right I store the value of the sum and then reset it to 0.

To make the travel I frame the square with -1 and visited cells are marked with -1 as well. I move in the actual direction until I reach a -1. From RIGHT I turn DOWN, then LEFT then UP and RIGHT again.

Anyone did something different than the spiral traveling? If you did a spiral traveling, how did you do it?

### Re: 11470 - Square Sums

Posted: Wed Sep 10, 2008 5:46 am
I simultaneously added top and bottom rows of each ring and then left and right columns of the rings, excluding the numbers that belong both to the top and botom rows.

Here's the relevant part of my code:

Code: Select all

``````    printf("Case %d:", C++);
for (int i=0; i<(n/2); ++i){
int s = 0;
for (int j=i; j<=n-1-i; ++j){
s += g[i][j];
s += g[n-1-i][j];
}
for (int k=i+1; k<n-1-i; ++k){
s += g[k][i];
s += g[k][n-1-i];
}
printf(" %d", s);
}
if (n & 1) printf(" %d", g[n/2][n/2]);
``````

### Re: 11470 - Square Sums

Posted: Mon Aug 23, 2010 1:09 pm

### 11470-Square Sums

Posted: Sat Sep 04, 2010 9:28 am  I tried almost 20 or more test data and it was all right!

but Judge says it's wrong!!

please give me some critical test data.

and here is my code....

Code: Select all

``Thank you sazzadcsedu, I got AC.``

### Re: 11470-Square Sums

Posted: Sat Sep 04, 2010 8:30 pm
Try this-

Code: Select all

``````8
23 434 555 6666 656 666 67 12
654 44 55 7676 888 866 343 77
98 77 4454 544 88 99 343 5454
5454 54 55 55 66 77 55 22
979 787 77 33 555 666 888 666
87678 676 55 666 454 766 99 67786
76786 77876 87 777 55465 5465 887 343
786 22 655 55 77 6565 77 88
0

output:
Case 1: 263401 153408 8001 709

Case 1: 263401 153408 8001 709 33

``````

### Re: 11470-Square Sums

Posted: Thu Sep 09, 2010 10:56 am
sazzadcsedu, I changed my code and It works...... But why OJ says NO........       Thank you~  Code: Select all

``Thank you sazzadcsedu, I got AC.``

### getting WA at uva=11470;http://uva.onlinejudge.org/external/

Posted: Thu Jan 03, 2013 11:57 pm
why i am getting WA..........?????
thnkz again brianfry713 Got AC....many many thnkz

### Re: getting WA at uva=11470;http://uva.onlinejudge.org/exter

Posted: Fri Jan 04, 2013 9:51 pm
Doesn't match the sample I/O, you shouldn't be printing len1.

### Re: 11470-Square Sums

Posted: Thu Jun 13, 2013 7:01 am
Why i am getting WA???? Code: Select all

``````#include <stdio.h>
int main()
{
long long int ara,sqval;
long long int n,i,j,i1,j1,x,x1,y,y1,z,t,nsq,c,s1,s2,s3,s4,sum;
for(t=0;;)
{
scanf("%d",&n);if(n==0) break;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%lld",&ara[i][j]);
}
}
if(n%2==0) nsq=n/2;
else nsq=(n+1)/2;

for(c=0;c<nsq;c++)
{
for(y=c,s1=0,s2=0;y<n-c;y++) {s1=s1+ ara[c][y]; s2=s2+ ara[n-1-c][y];}
for(x=c,s3=0,s4=0;x<n-c;x++) { s3=s3+ara[x][c];s4=s4+ara[x][n-1-c]; }
if(c!=nsq-1) sum=s1+s2+s3+s4-ara[c][c]-ara[n-1-c][c]-ara[c][n-1-c]-ara[n-1-c][n-1-c];
else if(c==nsq-1 && (c+1) %2!=0) sum=s1;
else if(c==nsq-1 && (c+1) % 2==0 ) sum=s1+s2+s3+s4-ara[c][c]-ara[n-1-c][c]-ara[c][n-1-c]-ara[n-1-c][n-1-c];
sqval[c]=sum;
}
t++;
printf("Case %lld: ",t);
for(z=0;z<nsq;z++)
{
printf("%lld",sqval[z]);
if(z!=nsq-1) { printf(" "); }
}
printf("\n");
}
return 0;
}
``````

### Re: 11470-Square Sums

Posted: Thu Jun 13, 2013 11:33 pm
Try using scanf("%lld", &n) on line 8.