POJ 2965 The Pilots Brothers' refrigerator
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 24130 | Accepted: 9327 | 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
6 1 1 1 3 1 4 4 1 4 3 4 4
Source
//从上到下,从左到右,每个把手翻转应该异或的值 void init() { for(int i = 0; i < 4; ++i){ for(int j = 0; j < 4; ++j){ int tmp = 0; tmp ^= 1<<(i*4+j); for(int k = 0; k < 4; ++k) tmp ^= 1<<(i*4+k); for(int k = 0; k < 4; ++k) tmp ^= 1<<(k*4+j); printf("%d ", tmp); } printf("\n"); } }
预处理得到change[]。
#include <cstdio> #include <vector> using namespace std; const int INF = 0x7fffffff; char s[20]; int change[] = {4383,8751,17487,34959,4593,8946,17652,35064,7953,12066,20292,36744,61713,61986,62532,63624}; int pos[16] = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768}; void solve() { int init_state = 0; for(int i = 0; i < 16; ++i){ if(s[i] == '+') init_state += pos[i]; } vector<pair<int, int> > resv, v; int res = INF; for(int i = 0; i < 65536; ++i){ int state = init_state; for(int j = 0; j < 16; ++j){ if(i&pos[j]){ state ^= change[j]; v.push_back(make_pair(j/4+1, j%4+1)); } } if(state == 0 && (int)v.size() < res){ res = v.size(); resv = v; } v.clear(); } printf("%d\n", res); for(int i = 0; i < res; ++i) printf("%d %d\n", resv[i].first, resv[i].second); } int main() { for(int i = 0; i < 4; ++i) scanf("%s", &s[i*4]); solve(); return 0; }
由于本题的特殊性,提供一个更高效的解法,参考"高效解法,供路人借鉴~"。
#include <cstdio> #include <cstring> char s[4][5]; bool mark[4][4]; void solve() { memset(mark, 0, sizeof(mark)); for(int i = 0; i < 4; ++i){ for(int j = 0; j < 4; ++j){ if(s[i][j] == '+'){ mark[i][j] = !mark[i][j]; for(int k = 0; k < 4; ++k){ mark[i][k] = !mark[i][k]; mark[k][j] = !mark[k][j]; } } } } int cnt = 0; int a[16], b[16]; for(int i = 0; i < 4; ++i){ for(int j = 0; j < 4; ++j){ if(mark[i][j]){ a[cnt] = i+1; b[cnt++] = j+1; } } } printf("%d\n", cnt); for(int i = 0; i < cnt; ++i) printf("%d %d\n", a[i], b[i]); } int main() { for(int i = 0; i < 4; ++i) scanf("%s", s[i]); solve(); return 0; }