国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

POJ 2965 The Pilots Brothers' refrigerator (枚舉)

2019-11-08 03:11:15
字體:
來源:轉載
供稿:網(wǎng)友

Description

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

61 11 31 44 14 34 4

題意

把題目中給出的狀態(tài)圖,全部翻轉成 - 的狀態(tài)

每次翻轉一個,它所在的行和列都要進行翻轉

問最小翻轉次數(shù),同時輸出翻轉路徑。

思路

和 POJ 1753 一樣,只不過在dfs的時候用一個棧或者雙端隊列存儲當前的路徑,找到答案之后輸出。

AC 代碼

#include <iostream>#include<stdio.h>#include<math.h>#include<string.h>#include<algorithm>#include<stdlib.h>#include<queue>using namespace std;bool bits[16];bool flag=false;deque<int>sk;void rese(int n) //翻轉{ int x=n/4,y=n%4; for(int i=0; i<4; i++) bits[x*4+i]=!bits[x*4+i]; for(int i=0; i<4; i++) if(i!=x) bits[i*4+y]=!bits[i*4+y];}bool jud() //判斷是否一致{ bool ini=bits[0]; if(!ini)return false; for(int i=1; i<16; i++) if(ini!=bits[i]) return false; return true;}void solve(int maxx,int now,int step){ if(maxx==step) //翻轉完最后一枚棋子 { if(jud()) //滿足狀態(tài) { flag=true; printf("%d/n",maxx); for(int i=0; i<maxx; i++) { printf("%d %d/n",sk.front()/4+1,sk.front()%4+1); sk.pop_front(); } } return; } for(int i=now; i<16; i++) //從上次翻轉位置繼續(xù) { rese(i); //翻轉i sk.push_back(i); solve(maxx,i+1,step+1); if(flag)return; //找到答案,返回 sk.pop_back(); rese(i); //恢復原來狀態(tài) }}int main(){ char str[4][4]; for(int i=0; i<4; i++) { cin>>str[i]; for(int j=0; j<4; j++) bits[i*4+j]=(str[i][j]=='-')?1:0; } for(int i=0; i<=16; i++) //i 為要翻轉的棋子個數(shù) solve(i,0,0); return 0;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 郧西县| 彭阳县| 柘城县| 成武县| 商河县| 句容市| 芜湖市| 阳曲县| 鄂尔多斯市| 云浮市| 九龙城区| 肥乡县| 三门县| 云林县| 赤城县| 新宾| 定边县| 花莲县| 南郑县| 云南省| 河津市| 囊谦县| 阳山县| 阿拉善右旗| 盐津县| 乌拉特前旗| 峨边| 鄱阳县| 汉寿县| 任丘市| 海宁市| 平乐县| 阳泉市| 新泰市| 增城市| 天门市| 界首市| 斗六市| 七台河市| 班戈县| 黄浦区|