poj-3185-switch problem

Source: Internet
Author: User

Describe

Cattle line 20 They drank the water bowl. The bowl can be so (facing right for the cool water) or upside down (one position without water). They want all 20 water bowls so, so turn the bowl with a wide nose.

The mouth is too wide, they not only flip a bowl of bowls on both sides of the bowl (a total of three or three--in case of both ends of the bowl--two bowls).

Given the initial state of the bowl (1 = not drinkable, 0 = drinkable – it even looks like a bowl), what is the minimum number of bowls that are necessary to flip all the bowls then?

Input

Line Line 1: A line of 20 space-delimited integers

Output

Line 1: The minimum number of bowls needed to turn over the bowl so (ie. 0). For a given input, it is always possible to find some combination of flip action bowls 20 0.

Sample input

0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0

Sample output

3

Tips

Explanation of the sample:

Flip Bowls 4, 9 years old and 11 years old when they are drunk:

0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 (initial state)

0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 (Rear turn bowl 4]

0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 after the turn of the bowl [9]

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 after the turn of the bowl [11]

Ideas:
Starting from the left and right sides, find the position of 1, let the latter two take the reverse; it is impossible to turn the last position or the first position to the last throw to 1; So when this happens, give the counter a large value and terminate it.
The minimum value of the left and right two counters is finally taken.

#include <cmath> #include <iostream>using namespace std; int main () { int cnt1=0,cnt2=0; int a[20],b[20]; for (int i=0;i<20;i++) { cin>>a[i]; B[i]=a[i]; } for (int i=0;i<20;i++) { if (a[i]==1) { if (i==19) {cnt1=20; break; } cnt1++; A[I+1]=!A[I+1]; A[I+2]=!A[I+2]; } } for (int i=19;i>=0;i--) { if (b[i]==1) { if (i==0) { cnt2=20; break; } cnt2++; B[I-1]=!B[I-1]; B[i-2]=!b[i-2]; } } Cout<<min (cnt1,cnt2) <<endl; return 0;}

  

poj-3185-switch problem

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.