11.py
2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/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));