【POJ】2965 - The Pilots Brothers' refrigerator(dfs & 双端队列 & 思维)

来源:互联网 发布:spark submit python 编辑:程序博客网 时间:2024/06/06 05:44

点击打开题目

The Pilots Brothers' refrigerator
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 23851 Accepted: 9185 Special Judge

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

Source

Northeastern Europe 2004, Western Subregion



这道题和翻转棋的差不多,但是这道题复杂多,要考虑多种情况,输出过程。

我这里用双端队列存过程。


代码如下:

#include <cstdio>#include <cmath>#include <queue>#include <stack>#include <cstring>#include <algorithm>using namespace std;struct node{int x,y;}pr;int mapp[8][8];deque<node> q[88];int ans[88];int ant = 0;bool flag = false;bool check(){for (int i = 1 ; i <= 4 ; i++){for (int j = 1 ; j <= 4 ; j++)if (mapp[i][j] == 0)return false;}return true;}void change(int x,int y){for (int i = 1 ; i <= 4 ; i++){mapp[x][i] = !mapp[x][i];mapp[i][y] = !mapp[i][y];}mapp[x][y] = !mapp[x][y];}void dfs(int x,int y){if (flag)return;if (check()){flag = true;return;}for (int i = y + 1 ; i <= 4 ; i++){change(x,i);ans[ant]++;pr.x = x;pr.y = i;q[ant].push_back(pr);dfs(x,i);if (flag)return;change(x,i);ans[ant]--;q[ant].pop_back();}for (int i = x + 1 ; i <= 4 ; i++){for (int j = 1 ; j <= 4 ; j++){change(i,j);ans[ant]++;pr.x = i;pr.y = j;q[ant].push_back(pr);dfs(i,j);if (flag)return;change(i,j);ans[ant]--;q[ant].pop_back();}}}int main(){for (int i = 1 ; i <= 4 ; i++){for (int j = 1 ; j <= 4 ; j++){char t;scanf ("%c",&t);if (t == '+')mapp[i][j] = 0;elsemapp[i][j] = 1;}getchar();}for (int i = 1 ; i <= 4 ; i++){for (int j = 1 ; j <= 4 ; j++){ans[ant] = 1;flag = false;pr.x = i;pr.y = j;q[ant].push_back(pr);change(i,j);dfs(i,j);change(i,j);if (flag)ant++;else{while (!q[ant].empty())q[ant].pop_back();}}}int pos = max_element(ans,ans+ant) - ans;printf ("%d\n",ans[pos]);while (!q[pos].empty()){pr = q[pos].front();printf ("%d %d\n",pr.x,pr.y);q[pos].pop_front();}return 0;}


0 0
原创粉丝点击