- Prepare
- Algorithms
- Implementation
- Cavity Map
- Discussions

# Cavity Map

# Cavity Map

+ 21 comments no need to read the input as string . read it as int using "scanf("%1d",your array[i)[j]);" ...read only a single digit at a time

+ 5 comments Python Implementation:

Just concentrate on (n-2)X(n-2) matrix as we need to ignore the borders

n = int(input().strip()) grid = [] grid_i = 0 for grid_i in range(n): grid_t = list(str(input().strip())) grid.append(grid_t) for i in range(1,(n-2)+1): for j in range(1,(n-2)+1): if grid[i][j]>max(grid[i-1][j],grid[i+1][j],grid[i][j-1],grid[i][j+1]): grid[i][j]='X' for i in range(n): print (''.join(grid[i]))

+ 13 comments Instead of using two arrays, compare the center matrix (formed excluding boundary values) while printing the matrix..! ;)

`int n,i,j; scanf("%d",&n); int a[n][n]; for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%1d",&a[i][j]); } } for(i=0;i<n;i++) { for(j=0;j<n;j++) { if( (i>=1)&&(i<n-1) && (j>=1)&&(j<n-1) ) { if( (a[i][j] > a[i-1][j]) && (a[i][j] > a[i][j+1]) && (a[i][j] > a[i+1][j]) && (a[i][j] > a[i][j-1]) ) printf("X"); else printf("%d",a[i][j]); } else printf("%d",a[i][j]); } printf("\n"); }`

+ 1 comment Why does Test Case #2 have spaces in the input grid? Although submitting the code that works for the Test Case #1 (one without the spaces) works fine for everything else, and gives the full 30 points,

I kind-of wasted my time because my code was reading the spaces in the grid rather than the number itself.

+ 2 comments the conditions are:

1) is right or left of the current value a value that is greater or equals

2) is above or below of the current value a value that is greater or equals

3) is it in the first or last row or is it the first or last number

if so it cant be a cavity

I just created an inverse of a 2 dimensional array and checked those conditions for both matrices. Even its O(N*N) it doesnt needs a 1/10 of a second

Sort 739 Discussions, By:

Please Login in order to post a comment