Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 17386 | Accepted: 6578 | 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
题意:'+'代表关,'-'代表开,改变一次(i,j)状态,则i行j列的其他开关状态都会改变,问给定初始状态,最少多少步才能让所以开关状态都为开。
思路:我的思路就只是BFS,只是因为扩展一个状态的时候忘了标记,然后TLE,WA,找了好久才找到错误。然后在网上看到了一个高效解法。
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <map>
#include <queue>
#include <set>
using namespace std;
//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#endif
#define SI(a) scanf("%d", &(a))
#define SDI(a, b) scanf("%d%d", &(a), &(b))
#define S64I(a) scanf(iform, &(a))
#define SS(a) scanf("%s", (a))
#define SDS(a, b) scanf("%s%s", (a), (b))
#define SC(a) scanf("%c", &(a))
#define PI(a) printf("%d\n", (a))
#define PS(a) puts(a)
#define P64I(a) printf(oform, (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define MSET(a, b) (memset((a), (b), sizeof(a)))
#define Mid(L, R) ((L) + ((R) - (L))/2)
#define Abs(a) ((a) >= 0 ? (a) : -(a))
#define REP(i, n) for(int (i)=0; (i) < (n); (i)++)
#define FOR(i, a, n) for(int (i)=(a); (i) <= (n); (i)++)
const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double MAX_DOUBLE = 10e17;
const int maxs = 70000 + 20;//2^16
const int FS = (1<<16) - 1;
struct Node {
int s, p, step, last;
Node(int _s=0, int _p=0, int _step=0, int _last=-1) {
s = _s;
p = _p;
step = _step;
last = _last;
}
};
Node Q[maxs];
int QFront, QRear;
int vis[maxs];
void print(int i) {
if(Q[i].last != -1) {
print(Q[i].last);
printf("%d %d\n", Q[i].p/4+1, Q[i].p%4+1);
}
}
int SW(int s, int x, int y) {
int ts = s;
for(int i=0; i<4; i++) {
int t = x * 4 + i;
s ^= (1<<t);
t = i * 4 + y;
s ^= (1<<t);
}
s ^= (1<<(x * 4 + y));
return s;
}
int bfs(int ss) {
QFront = QRear = 0;
Node s(ss, 0, 0, -1);
Q[QRear++] = s;
MSET(vis, 0);
int qqq = 0;
while(QFront < QRear) {
Node cur = Q[QFront++];
qqq ++;
vis[cur.s] = 1;
if(cur.s == FS) {
return QFront-1;
}
for(int i=0; i<16; i++) {
int x = i / 4;
int y = i % 4;
int ns = SW(cur.s, x, y);
if(!vis[ns]) {
Node nn(ns, i, cur.step+1, QFront-1);
vis[ns] = 1;
Q[QRear++] = nn;
}
}
}
return 0;
}
int main() {
int ss = 0;
for(int i=0; i<16; i++) {
int x = i /4;
int y = i % 4;
char t = getchar();
while(t!='+' && t!='-') t = getchar();
if(t == '-') ss |= (1<<i);
}
int a = bfs(ss);
int ans = Q[a].step;
printf("%d\n", ans);
print(a);
return 0;
}