1767 - Funny Areas

Discussion around the problems of the COJ.
Forum rules
Remember that posting AC code is not allowed here. If you are going to ask a question or to post a solution, describe your algorithm instead. Posting AC code will be penalized.
ymondelo20
Posts: 1968
Joined: 9 years ago
Location: Universidad de las Ciencias Informáticas
Gender:
Contact:

1767 - Funny Areas

"Every problem has a simple, fast and wrong solution" OJ's Main Law.

Phantom
Posts: 58
Joined: 8 years ago
Location: Cuba
Gender:

Re: 1767 - Funny Areas

I got only WA in this problem, and I proved it even with negatives values in the center. I cannot understand…. Who is wrong, the COJ or me?? Can anybody help me?
This is my code in C++(is not AC):

Code: Select all

``````#include <cstdlib>
#include <iostream>

using namespace std;
int max(int, int);
int min(int, int);

int main()
{
int dimX, dimY, cases;
cin>>dimX>>dimY;
int matrix[dimX][dimY];
for(int i=0; i<dimX; i++)
for(int j=0; j<dimY; j++)cin>>matrix[i][j];
cin>>cases;
int info[3], suma;
for(int k=0; k<cases; k++)
{
cin>>info[0]>>info[1]>>info[2];
suma = 0;
int lim[4] = {max(0,info[0]-info[2]), min(dimX-1,info[0]+info[2]),
max(0,info[1]-info[2]), min(dimY-1,info[1]+info[2])};
for(int i=lim[0]; i<=lim[1]; i++)
for(int j=lim[2]; j<=lim[3]; j++)
if(abs(info[0]-i) + abs(info[1]-j) <= info[2])
suma += matrix[i][j];
cout<<suma<<'\n';
}
return 0;
}

int max(int a, int b){return (a > b)? a : b;}
int min(int a, int b){return (a < b)? a : b;}
``````
I prevent in “lim” the out of range cases. Thanks to “lim”, I only see the cases that are in range of the radius and in range of the matrix.
After, I go over the square that describe “lim”, and if the value are in the range of the radius, I sum the value in this position (suma += matrix[j] You see?? Very simple.