program main implicit none integer::N,i,j,tmp,winnum,num,minnum character::c character*6::S(6) integer*8,allocatable::games(:,:) integer*8,parameter::win=2,lose=0,self=9, none=1 type game integer::a,b end type game type(game)::yet(10) yet(:)%a=0 yet(:)%b=0 read *,N allocate(games(1:N,1:N)) read *,S(1:N)(1:N) do i=1,N games(i,i) = self do j=i+1,N c = S(i)(j:j) if(c.eq.'o') then games(i,j) = win games(j,i) = lose else if(c.eq.'x') then games(i,j) = lose games(j,i) = win else games(i,j) = none games(j,i) = none end if end do end do do j=2, N if(games(1,j).eq.none) then games(1,j) = win games(j,1) = lose end if end do winnum = COUNT(games(1,:).eq.win) do i=2, N tmp = COUNT(games(i,:).eq.win) if(winnum.gt.tmp) then do j=3, N if(games(i,j).eq.none) then games(i,j) = win games(j,i) = lose end if end do end if end do num = 0 do i=1, N do j=i+1, N if(games(i,j).eq.none) then num = num + 1 yet(num)%a = i yet(num)%b = j end if end do end do minnum = N do i=1, 2**num do j=0, num-1 if(btest(i, j).eqv..true.) then games(yet(j)%a,yet(j)%b) = win games(yet(j)%b,yet(j)%a) = lose end if end do minnum = MIN(minnum, get_grade()) end do print '(i0)', minnum contains function get_grade() result(w) integer::i,j,w integer::totalwin(num) do i=1,N totalwin(i) = COUNT(games(i,:).eq.win) end do w = COUNT(totalwin(2:N).gt.totalwin(1)) + 1 end function get_grade end program main