-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09b.m
executable file
·45 lines (44 loc) · 845 Bytes
/
09b.m
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
#!/usr/bin/octave
function retval=recfun(i,j,area)
global elem;
retval=area;
if (elem(i,j)!=9)
elem(i,j)=9;
retval+=1;
if (i<100)
retval+=recfun(i+1,j,area);
endif
if (j<100)
retval+=recfun(i,j+1,area);
endif
if (j>1)
retval+=recfun(i,j-1,area);
endif
if (i>1)
retval+=recfun(i-1,j,area);
endif
endif
endfunction
fid=fopen('09.txt', 'r');
data=textscan(fid, '%s');
fclose(fid);
elem=zeros(100,100);
for i=1:length(data{1})
for j=1:length(data{1}{i})
elem(i,j)=str2num(data{1}{i}(j));
endfor
endfor
k=1;
area=0;
global elem;
for i=1:100
for j=1:100
if (elem(i,j)!=9)
area(k)=recfun(i,j,0);
k+=1;
endif
endfor
endfor
area=sort(area,"descend");
answer=area(1)*area(2)*area(3);
printf('The product of the largest three areas is %d\n', answer);