program main implicit none integer::N,depth=0 integer::K(12) logical::flag read *,N read *,K(1:N) flag = win(K(1:N), depth) if(flag.eqv..false.) print '("-1")' contains logical recursive function win(K, depth) result(w) integer::K(:) integer,allocatable::L(:) integer::a,b,c,s,ka,kb,kc, depth logical::mask(12) s = SIZE(K) if(s.le.2) then w = .false. return else allocate(L(s-3)) mask(1:s) = .true. do a=1,s-2 ka = K(a) do b=a+1,s-1 kb = K(b) if(kb.eq.ka) cycle do c=b+1,s kc = K(c) !print '(i0,":",3(i0,1x),":",3(i0,1x))',depth,a,b,c, ka,kb,kc if(.not.is_kadomatsu(ka,kb,kc)) cycle mask(a)=.false. mask(b)=.false. mask(c)=.false. L = PACK(K,mask(1:s)) if(win(L, depth+1).eqv..false.) then w = .true. if(depth.eq.0) then print '(3(i0,1x))', a-1,b-1,c-1 end if return end if mask(c)=.true. end do mask(b)=.true. end do mask(a)=.true. end do w = .false. end if end function win logical function is_kadomatsu(a,b,c) result(k) integer::a,b,c if(a.eq.b.or.b.eq.c.or.a.eq.b.or. & (a.gt.b.and.b.gt.c) .or. & (a.lt.b.and.b.lt.c)) then k = .false. else k = .true. end if end function is_kadomatsu subroutine aprinter(array) integer*8::array(:) character*32::cformat='(i0, (1x,i0))' write(cformat(5:9),'(i0)'),size(array) write(*,cformat) array end subroutine aprinter end program main