Original title Address
Backtracking + Search
Enumeration of each location can be placed on the mine, when the first position of the enumeration is completed, the i-1 position is determined, at this time, check whether the i-1 location is satisfied with the requirements, that is, the left and right interval of 1 of the number of mines is equal to the declaration number, if the conditions, then continue to search, if not meet the conditions, Discard this search branch.
Once the search is complete, place all the possible solutions by the location "with" and find those that must be mines or must be empty.
The final output can be.
Code:
1#include <iostream>2#include <cstring>3 4 using namespacestd;5 6 voidMergeint*t,int*r,intN) {7 for(inti =0; I < n; i++) {8 if(T[i] = =-1|| T[i] = =R[i])9T[i] =R[i];Ten Else OneT[i] =-2; A } - } - the voidFindint*a,int*r,int*t,intPintN) { - if(P >=N) { - if(n = =1&& A[p-1] = = R[p-1]) -|| (N >1) && A[p-1] = = R[p-1] + r[p-2])) + Merge (t, R, N); - } + Else if(p = =0) { A for(inti =0; I <2; i++) { atR[P] =i; -Find (A, R, T, p +1, n); - } - } - Else if(p = =1) { - for(inti =0; I <2; i++) { inR[P] =i; - if(A[p-1] = = R[p-1] +R[p]) toFind (A, R, T, p +1, n); + } - } the Else { * for(inti =0; I <2; i++) { $R[P] =i;Panax Notoginseng if(A[p-1] = = R[p] + r[p-1] + r[p-2]) -Find (A, R, T, p +1, n); the } + } A } the + intMain () { - intN; $ $CIN >>N; - while(n--) { - intN; theCIN >>N; - int*a =New int[N];Wuyi int*r =New int[N]; the int*t =New int[N]; - Wu for(inti =0; i < N; i++) -CIN >>A[i]; AboutMemset (R,0Nsizeof(int)); $memset (T,-1Nsizeof(int)); - -Find (A, R, T,0, N); - A intMine =0; + intNot_mine =0; the - for(inti =0; i < N; i++) { $Mine + = (T[i] = =1?1:0); theNot_mine + = (t[i] = =0?1:0); the } the thecout <<mine; - for(inti =0; i < N; i++) in if(T[i] = =1) thecout <<" "<< i +1; thecout <<Endl; About thecout <<Not_mine; the for(inti =0; i < N; i++) the if(T[i] = =0) +cout <<" "<< i +1; -cout <<Endl; the Bayi delete T; the Delete R; the delete A; - } - the return 0; the}
hihocoder#1114 small hi small ho's big battle: Minesweeper • One