function W = solver1(B) % for connecting purposes ignore pins with only one occurrence % if sum of l1 distances to center is greater than sum of pins, ignore % pins = unique(B(B>0)); % ignPin = zeros(size(pins)); % for idx = 1:numel(pins) % k = find(B == pins(idx)); % [i,j] = ind2sub(size(B),k); % ci = mean(i); % cj = mean(j); % dc = sum(abs(ci-i) + abs(cj-j)); % pB = sum(B(k)); % ignPin(idx) = dc - pB; % end pinNbr = sort(unique(B(B>0)), 'descend'); [br, bc] = size(B); neighbIdx = [ -1 br 1 -br ]; % [ ^ > v < ] W = zeros(0,4); for n = pinNbr' pinIdx = find(B == n); for p = pinIdx' neighb = p + neighbIdx; for k = 1:4 N = neighb(k); if N > 0 && N <= br*bc && (B(N) == 0 || B(N) == n) p2 = N; neighbIdx2 = setdiff(neighbIdx, -neighbIdx(k)); % remove previous direction neighb2 = p2 + neighbIdx2; isGood = B(N) == n; for k2 = 1:3 N2 = neighb2(k2); if N2 > 0 && N2 <= br*bc && B(N2) == n [p2i, p2j] = ind2sub([br bc],p2); [N2i, N2j] = ind2sub([br bc],N2); W = [ W ; p2i p2j N2i N2j ]; isGood = true; end end if isGood [pi, pj] = ind2sub([br bc],p); [Ni, Nj] = ind2sub([br bc],N); W = [ W ; pi pj Ni Nj ]; end end end end end