POJ 2965 The Pilots Brothers' refrigerator
来源:互联网 发布:mac 触摸板 手势 编辑:程序博客网 时间:2024/06/15 22:08
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 rowi 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
题意:'+'代表关,'-'代表开,改变一次(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 WINtypedef __int64 LL;#define iform "%I64d"#define oform "%I64d\n"#elsetypedef 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^16const 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;}
- poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator
- poj 2965The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator 枚举
- POJ-2965-The Pilots Brothers' refrigerator
- poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator
- poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator
- POJ-2965-The Pilots Brothers' refrigerator
- poj 2965 The Pilots Brothers' refrigerator
- POJ 2965:The Pilots Brothers' refrigerator
- Poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator(枚举)
- poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator 枚举
- POJ-2965-The Pilots Brothers' refrigerator
- 2015阿里巴巴数据分析师实习生招聘笔试题(完整照片版)
- springsecurity学习(一)
- 11G怎样编译BBED
- java web学习笔记(五)--tomcat的目录结构
- linux 启动脚本调用顺序(测试平台 AM335X-ARM A9)
- POJ 2965 The Pilots Brothers' refrigerator
- getchar()的陷阱
- Linux分段
- 吐槽
- 6.37③ 试直接利用栈的基本操作写出先序遍历的非递归 形式的算法
- 列表标签、表单标签—实验
- java 打jar包
- Linux GDT
- poj-3009-Curling 2.0-dfs