GRID01-Editorials

PROBLEM LINK

Practice http://www.codechef.com/INLO2015/problems/GRID01

Contest http://www.codechef.com/INLO2015

Editorialist: Arihant singhi http://www.codechef.com/users/arihant95

DIFFICULTY:

Simple

PREREQUISITES:

IQ based Maths

PROBLEM

Find the Number of sqaures around each block.

EXPLANATION

The question ask us to calculate the no. of squares around each block..

Following are cases in general :

  1. If block is at corner .

There will be 3 sqaures around each corner.

  1. If block is at edge

There will be 5 sqaures around each corner.

General Formula for no. of sqaures around each squares in a grid .

43 + (n-2)(m-2)*8 + (n-2)*10 + (m-2)*10

for grid [n*m]
where n,m >= 2

pseudocode

Start
input test cases
input grid n*m

if n=m=1
print 0;

else if n=1

s= (m-2)*2+2

else if m=1

s= (n-2)*2+2

else

s=43 + (n-2)(m-2)*8 + (n-2)*10 + (m-2)*10;

print s;

End