r/dailyprogrammer • • Jul 06 '12

[7/6/2012] Challenge #73 [intermediate]

Write a program that, given an ASCII binary matrix of 0's and 1's like this:

0000000000000000
0000000000000000
0000011001110000
0000001111010000
0000011001110000
0000011011100000
0000000000110000
0000101000010000
0000000000000000
0000000000000000
0000000000000000

Outputs the smallest cropped sub-matrix that still contains all 1's (that is, remove all borders of 0's):

01100111
00111101
01100111
01101110
00000011
10100001
10 Upvotes

28 comments sorted by

View all comments

Show parent comment

1

u/appleade280 Jul 09 '12

Quite similar but in D:

import std.stdio;

immutable HEIGHT = 11;
immutable LENGTH = 16;

int input[HEIGHT][LENGTH] =     [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                                [0,0,0,0,0,1,1,0,0,1,1,1,0,0,0,0],
                                [0,0,0,0,0,0,1,1,1,1,0,1,0,0,0,0],
                                [0,0,0,0,0,1,1,0,0,1,1,1,0,0,0,0],
                                [0,0,0,0,0,1,1,0,1,1,1,0,0,0,0,0],
                                [0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0],
                                [0,0,0,0,1,0,1,0,0,0,0,1,0,0,0,0],    
                                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]];

void main() {
	int hmin = LENGTH - 1, hmax = 0, vmin = HEIGHT - 1, vmax = 0;
	for(int i = 0; i < HEIGHT; i++) {
		for(int j = 0; j < LENGTH; j++) {
			if(input[i][j] == 1) {
				if(i < hmin) hmin = i;
				else if(i > hmax) hmax = i;
				if(j < vmin) vmin = j;
				else if(j > vmax) vmax = j;
			}
		}
	}

	for(int i = hmin; i <= hmax; i++) {
		for(int j = vmin; j <= vmax; j++)
			write(input[i][j]);
		writeln();
	}
}