日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

The Water Bowls POJ - 3185(开关问题+暴力)

發布時間:2023/12/4 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 The Water Bowls POJ - 3185(开关问题+暴力) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題意:

給20個水碗。朝上為‘0’或朝下為‘1’,每次操作使三個碗翻轉,問使所有20個水碗都朝上,至少翻多少次?

題目:

The cows have a line of 20 water bowls from which they drink. The bowls can be either right-side-up (properly oriented to serve refreshing cool water) or upside-down (a position which holds no water). They want all 20 water bowls to be right-side-up and thus use their wide snouts to flip bowls.

Their snouts, though, are so wide that they flip not only one bowl but also the bowls on either side of that bowl (a total of three or – in the case of either end bowl – two bowls).

Given the initial state of the bowls (1=undrinkable, 0=drinkable – it even looks like a bowl), what is the minimum number of bowl flips necessary to turn all the bowls right-side-up?

Input

Line 1: A single line with 20 space-separated integers

Output

Line 1: The minimum number of bowl flips necessary to flip all the bowls right-side-up (i.e., to 0). For the inputs given, it will always be possible to find some combination of flips that will manipulate the bowls to 20 0’s.

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

Hint

Explanation of the sample:

Flip bowls 4, 9, and 11 to make them all drinkable:
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 [after flipping bowl 4]
0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 [after flipping bowl 9]
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [after flipping bowl 11]

分析:

這個題直接暴力過得,直接正反遍歷,找最小值,還有一種構造增廣矩陣類似于根據開關的關系構造有向圖的鄰接矩陣;構造增廣矩陣,高斯消元,枚舉自由元(二進制枚舉狀態),尋找最小值的方法,因為時間關系就沒有去鉆研,在這提供下思路。

AC代碼:

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int a[30],b[30]; int ans,num; int main(){for(int i=1;i<=20;i++){scanf("%d",&a[i]);b[i]=a[i];}num=ans=0;for(int i=1;i<=20;i++){if(a[i]==1){ans++;a[i+1]=!a[i+1];a[i+2]=!a[i+2];}}for(int i=20;i>=1;i--){if(b[i]==1){num++;b[i-1]=!b[i-1];b[i-2]=!b[i-2];}}ans=min(ans,num);printf("%d\n",ans);return 0; }

總結

以上是生活随笔為你收集整理的The Water Bowls POJ - 3185(开关问题+暴力)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。