【POJ - 2965】The Pilots Brothers' refrigerator(暴力枚举,思维)
題干:
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題目大意:
一個(gè)冰箱上有4*4共16個(gè)開關(guān),改變?nèi)我庖粋€(gè)開關(guān)的狀態(tài)(即開變成關(guān),關(guān)變成開)時(shí),此開關(guān)的同一行、同一列所有的開關(guān)也會(huì)同時(shí)改變狀態(tài)。要想打開冰箱,要所有開關(guān)全部打開才行?!?#43;’代表關(guān)閉,‘-’代表打開。
解題報(bào)告:
直接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(暴力枚举,思维)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【HDU - 1542】Atlantis
- 下一篇: 申请信用卡邮箱怎么填 邮箱填写这两点作用