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
9 Upvotes

28 comments sorted by

View all comments

1

u/ThatRedFurby Jul 09 '12

My first C / C++:

#include <stdio.h>

#define HEIGHT 11
#define 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}};

int main()
{
    int t=HEIGHT, b=0, l=LENGTH, r=0;
    for(int row=0, rowB=HEIGHT-1; row<HEIGHT; row++, rowB--){
        for(int col=0, colR=LENGTH-1; col<LENGTH;col++, colR--){
            if(input[row][col]!=0){
                if(row<t) t=row;
                if(col<l) l=col;
            }
            if(input[rowB][colR]!=0){
                if(rowB>b) b=rowB;
                if(colR>r) r=colR;
            }
        }
    }
    //Print
    for(int row=t; row<=b; row++){
        for(int col=l; col<=r; col++){
            printf("%i ", input[row][col]);
        } printf("\n");
    }
    return 0;
}

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();
	}
}