Analysis:
When N is an odd number, the parity can be reached. If n is an even number, the sum of the reverse order number sum and the distance of the target space row with the leading space must be the same as the reverse order number of the end state.
View code
# Include <iostream> # Include <Algorithm> # Include < String > Using Namespace STD; Const Int N = 300 + 10 ; Int S [N * n], G [N * N]; # Define _ CP (A, B) (a) <= (B )) Int _ TMP [N * N]; Int Inv ( Int N, Int * A ){ Int L = n> 1 , R = N- L, I, J; Int Ret = (r> 1 ? (INV (L, A) + inv (R, A + l )): 0 ); For (I = J = 0 ; I <= L; _ TMP [I + J] = A [I], I ++ ) For (Ret + = J; j <R & (I = L |! _ CP (A [I], a [L + J]); _ TMP [I + J] = A [L + J], J ++ ); Memcpy (A, _ TMP, Sizeof ( Int )* N ); Return RET ;} Int Main (){ Int N; While (Scanf ( " % D " , & N) = 1 && N ){ Int Num = 0 ; Bool Flag = True ; For ( Int I = 0 ; I <n * n; I ++ ) {Scanf ( " % D " ,& G [I]); If (Flag & G [I]! = 0 ) Num ++ ; If (G [I] = 0 ) Flag = False ;} Int Temp = inv (N * N, g); temp -= Num; // The number of reverse orders of 0 is not considered. If (! (N & 1 )) // If n is an even number, add the sum of the distance between the target space row and the DIS with the leading space. Temp + = (n- 1 -(Num/ N )); If (Temp & 1 ) Puts ( " No " ); Else Puts ( " Yes " );} Return 0 ;}