fclose('all');
clear all;

matfiles = dir(fullfile(cd,'*.ct'));
[tmp ind] = sort([matfiles.bytes]);
matfiles= matfiles(ind);
%matlabpool(3);
for lm =1:1000

%Input    
str = [cd,'/',matfiles(lm).name];
fid = fopen(str);
inFile = textscan(fid,'%s');

%Input Reshaping
idx = find(strcmp(inFile{1,1}(1:min(100,length(inFile{1,1}))), '1'));
pc = 1;
while (~(strcmp(inFile{1,1}(idx(pc)+6), '2')) || ~(strcmp(inFile{1,1}(idx(pc)+5), '1')));
    pc = pc + 1;
end
idx1 = idx(pc);
in = reshape(inFile{1,1}(idx1:length(inFile{1,1})),6,(length(inFile{1,1})-idx1+1)/6);
len = size(in,2);
bStar = cell(len+1);
p = cell(len+1);

% Dynamic Programming Part 
for k = 1:len+1
    for o = 1:len+1
        bStar{k,o} = [0,0,0];
        p{k,o} = [0,0,0];
    end
end

for l = 2:len-1
    for i = 1:len-l
        j = i + l;
        bStar{i,j} = computeBStar(i, j, p, bStar,in,len);
        p{i,j} = computeP(i, j, p, bStar);
    end
end

%Output : a list of points which make the polygon
str2 = [cd,'/resultGU3/',matfiles(lm).name,'.txt'];
fileID= fopen(str2,'w');
fprintf(fileID,'%d %d %d\r\n',p{1,len}');
fclose(fileID);
end
%matlabpool close;