hihocoder#1114 small hi small ho's big battle: Minesweeper • One

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.