日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

【POJ - 2965】The Pilots Brothers' refrigerator(暴力枚举,思维)

發(fā)布時間:2023/12/10 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ - 2965】The Pilots Brothers' refrigerator(暴力枚举,思维) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題干:

The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.

There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location?[i, j]?(1 ≤ i, j ≤ 4). However, this also changes states of all handles in row?i?and all handles in column?j.

The task is to determine the minimum number of handle switching necessary to open the refrigerator.

Input

The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “?” means “open”. At least one of the handles is initially closed.

Output

The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.

Sample Input

-+-- ---- ---- -+--

Sample Output

6 1 1 1 3 1 4 4 1 4 3 4 4

題目大意:

一個冰箱上有4*4共16個開關(guān),改變?nèi)我庖粋€開關(guān)的狀態(tài)(即開變成關(guān),關(guān)變成開)時,此開關(guān)的同一行、同一列所有的開關(guān)也會同時改變狀態(tài)。要想打開冰箱,要所有開關(guān)全部打開才行。‘+’代表關(guān)閉,‘-’代表打開。

解題報告:

直接2^16暴力枚舉,注意狀態(tài)的更新,不要全部更新一遍,而是只記錄行和列的更新即可。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; const int up = 1<<16; char s[5][5]; int R[5],C[5],ok[5][5],ansk=1e8,ans; bool tt[5][5]; int main() { // printf("%lld\n",(1<<15)+(1<<14)+(1<<12)+(1<<0)+(1<<2)+(1<<3));for(int i = 0; i<=3; i++) {scanf("%s",s[i]);}for(int i = 0; i<=3; i++) {for(int j = 0; j<=3; j++) {if(s[i][j] == '-') ok[i][j]=1;else ok[i][j]=0;}}for(int bit = 0; bit<up; bit++) { // if(bit == 53261) { // printf("ok\n"); // }int k=0,flag = 1,zs=0;for(int i = 0; i<=3; i++) R[i]=C[i]=0;memset(tt,0,sizeof tt);for(int x=bit;x;x>>=1,k++) {if(x%2 == 1) {zs++;int row = k/4,col = k%4;R[row]++,C[col]++;tt[row][col]=1;}}for(int i = 0; i<=3; i++) {for(int j = 0; j<=3; j++) {if((R[i]+C[j]+ok[i][j]+tt[i][j])%2 == 0) {flag = 0;break;}}if(flag == 0) break;}if(flag == 1) {if(zs < ansk) {ansk=zs;ans=bit;}}}printf("%d\n",ansk);int k=0;for(int x = ans;x;x>>=1) {if(x%2 == 1) {int row = k/4,col=k%4; printf("%d %d\n",row+1,col+1);}k++;}return 0 ; }

?

總結(jié)

以上是生活随笔為你收集整理的【POJ - 2965】The Pilots Brothers' refrigerator(暴力枚举,思维)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。