Google Code Jam 2013 – Qualification Round Problem 2

Alice and Bob have a lawn in front of their house, shaped like an N metre by M metre rectangle. Each year, they try to cut the lawn in some interesting pattern. They used to do their cutting with shears, which was very time-consuming; but now they have a new automatic lawnmower with multiple settings, and they want to try it out.

The new lawnmower has a height setting – you can set it to any height h between 1 and 100 millimetres, and it will cut all the grass higher than h it encounters to height h. You run it by entering the lawn at any part of the edge of the lawn; then the lawnmower goes in a straight line, perpendicular to the edge of the lawn it entered, cutting grass in a swath 1m wide, until it exits the lawn on the other side. The lawnmower’s height can be set only when it is not on the lawn.

Alice and Bob have a number of various patterns of grass that they could have on their lawn. For each of those, they want to know whether it’s possible to cut the grass into this pattern with their new lawnmower. Each pattern is described by specifying the height of the grass on each 1m x 1m square of the lawn.

The grass is initially 100mm high on the whole lawn.

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing two integers: N and M. Next follow N lines, with the ith line containing M integers ai,j each, the number ai,j describing the desired height of the grass in the jth square of the ith row.

Output

For each test case, output one line containing “Case #x: y”, where x is the case number (starting from 1) and y is either the word “YES” if it’s possible to get the x-th pattern using the lawnmower, or “NO”, if it’s impossible (quotes for clarity only).

Limits

1 ≤ T ≤ 100.

Small dataset

1 ≤ N, M ≤ 10.
1 ≤ ai,j ≤ 2.
Large dataset

1 ≤ N, M ≤ 100.
1 ≤ ai,j ≤ 100.

Sample Input

3
3 3
2 1 2
1 1 1
2 1 2
5 5
2 2 2 2 2
2 1 1 1 2
2 1 2 1 2
2 1 1 1 2
2 2 2 2 2
1 3
1 2 1

Sample Output

Case #1: YES
Case #2: NO
Case #3: YES

My Solution

The basic idea to solve this problem is that for any square you must be able to find a column or row that includes that square and that only has squares with value equal or smaller. If you can’t find it than the pattern is not possible.

#include <stdio.h>

char* processLawn(int lawn[], int n, int m){
int i,j;
int possible;
int x;
int count;
int value;

for(i=0;i<n;i++){
for(j=0;j<m;j++){
value = lawn[i][j];
possible = 0;
/*try column path*/
count=0;
for(x=0;x<n;x++){
if(lawn[x][j]<=value)
count++;
}
if(count==n)
possible = 1;

/*try row path*/
count=0;
for(x=0;x<m;x++){
if(lawn[i][x]<=value)
count++;
}
if(count==m)
possible = 1;

if(possible)
continue;
else
return "NO";

}
}

return "YES";
}

int main(){
int k,t;
int lawn;
int n,m;
int i,j;
char *result;

scanf("%d",&t);

for(k=0;k<t;k++){
scanf("%d %d",&n,&m);

for(i=0;i<n;i++)
for(j=0;j<m;j++){
scanf("%d",&lawn[i][j]);
}

result = processLawn(lawn,n,m);

if(k>0)
printf("n");
printf("Case #%d: %s",k+1,result);
}

return 0;
}