## 11470 - Square Sums

Moderator: Board moderators

slxst
New poster
Posts: 23
Joined: Mon Oct 16, 2006 2:18 am

### 11470 - Square Sums

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?

andmej
Experienced poster
Posts: 158
Joined: Sun Feb 04, 2007 7:45 pm
Location: Medellin, Colombia

### Re: 11470 - Square Sums

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]);
``````
Runtime errors in Pascal are reported as Wrong Answers by the online judge. Be careful.

Are you dreaming right now?
http://www.dreamviews.com

Mizanur Rahman(IUK)
New poster
Posts: 12
Joined: Wed Aug 18, 2010 12:07 pm

### Re: 11470 - Square Sums

mintae71
New poster
Posts: 18
Joined: Tue Jan 19, 2010 10:50 am

### 11470-Square Sums

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.``
Last edited by mintae71 on Thu Sep 09, 2010 11:05 am, edited 1 time in total.

Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Contact:

### Re: 11470-Square Sums

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

``````
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359

mintae71
New poster
Posts: 18
Joined: Tue Jan 19, 2010 10:50 am

### Re: 11470-Square Sums

sazzadcsedu, I changed my code and It works......

But why OJ says NO........

Thank you~

Code: Select all

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

graph_dp
New poster
Posts: 7
Joined: Fri Sep 14, 2012 8:33 am

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

why i am getting WA..........?????
thnkz again brianfry713 Got AC....many many thnkz
Last edited by graph_dp on Fri Jan 04, 2013 11:28 pm, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

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

Doesn't match the sample I/O, you shouldn't be printing len1.
Check input and AC output for thousands of problems on uDebug!

??????
New poster
Posts: 6
Joined: Thu Jun 13, 2013 5:40 am

### Re: 11470-Square Sums

Why i am getting WA????

Code: Select all

``````#include <stdio.h>
int main()
{
long long int ara[100][100],sqval[10000];
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;
}
``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 11470-Square Sums

Try using scanf("%lld", &n) on line 8.
Check input and AC output for thousands of problems on uDebug!