11.py 2.41 KB
#!/usr/bin/python

import sys;
import math;

def constructMatrix(height,width,data):
	matrix = [[0 for x in range(0, width)] for x in range(0, height)];
	for i in range (0,height):
		k = 0;
		dataArray = data[i].split();
		for j in range (0,width):
			matrix[i][j] = dataArray[k];
			k += 1;

	return matrix;

def doAllOperations(heigth,width,matrix,x,y):
	higherResult = 0;
	#Horizontal forward
	if(x <= (width-4)):
		newResult = int(matrix[x][y])*int(matrix[x+1][y])*int(matrix[x+2][y])*int(matrix[x+3][y]);
		if(newResult > higherResult):
			higherResult = newResult;
		#Horizontal backward
	if(x >= 3):
		newResult = int(matrix[x][y])*int(matrix[x-1][y])*int(matrix[x-2][y])*int(matrix[x-3][y]);
		if(newResult > higherResult):
			higherResult = newResult;
		#Vertical upward
	if(y <= 3):
		newResult = int(matrix[x][y])*int(matrix[x][y-1])*int(matrix[x][y-2])*int(matrix[x][y-3]);
		if(newResult > higherResult):
			higherResult = newResult;
		#Vertical downward
	if(y <= (height-4)):
		newResult = int(matrix[x][y])*int(matrix[x][y+1])*int(matrix[x][y+2])*int(matrix[x][y+3]);
		if(newResult > higherResult):
			higherResult = newResult;
		#Diagonal NE
	if((x <= (width-4)) and (y <= 3)):
		newResult = int(matrix[x][y])*int(matrix[x+1][y-1])*int(matrix[x+2][y-2])*int(matrix[x+3][y-3]);
		if(newResult > higherResult):
			higherResult = newResult;
		#Diagonal NW
	if((x >= 3) and (y <= 3)):
		newResult = int(matrix[x][y])*int(matrix[x-1][y-1])*int(matrix[x-2][y-2])*int(matrix[x-3][y-3]);
		if(newResult > higherResult):
			higherResult = newResult;
		#Diagonal SW
	if((x >= 3) and (y <= (height-4))):
		newResult = int(matrix[x][y])*int(matrix[x-1][y+1])*int(matrix[x-2][y+2])*int(matrix[x-3][y+3]);
		if(newResult > higherResult):
			higherResult = newResult;
		#Diagonal SE
	if((x <= (width-4))and (y <= (height-4))):
		newResult = int(matrix[x][y])*int(matrix[x+1][y+1])*int(matrix[x+2][y+2])*int(matrix[x+3][y+3]);
		if(newResult > higherResult):
			higherResult = newResult;

	return higherResult;


def getHigherOperation(height,width,matrix):
	higherResult = 0;
	for i in range (0,height):
		for j in range (0,width):
			newResult = doAllOperations(height,width,matrix,j,i);
			if(newResult > higherResult):
				higherResult = newResult;
	return higherResult;

data = sys.stdin.readlines();
height = int(sys.argv[2]);
width = int(sys.argv[1]);
matrix = constructMatrix(height,width,data);
print "Result is: " + str(getHigherOperation(height,width,matrix));