poj2965--The Pilots Brothers' refrigerator
2014-05-15 21:16
344 查看
The Pilots Brothers' refrigerator
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
Source
Northeastern Europe 2004, Western Subregion
和翻转棋差不多的做法,只是改为了翻转一行和一列
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 17642 | Accepted: 6685 | Special Judge |
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
Northeastern Europe 2004, Western Subregion
和翻转棋差不多的做法,只是改为了翻转一行和一列
#include <stdio.h> #include <string.h> struct node{ int a[16] ; int t , n ; int s[16] ; int top ; }p[1000000]; int main() { int i , j , a[16] , min_t = 999999 , to ; char str[4][4] ; for(i = 0 ; i < 4 ; i++) scanf("%s", str[i]); for(i = 0 ; i < 4 ; i++) for(j = 0 ; j < 4 ; j++) if(str[i][j] == '+') a[4*i+j] = 1 ; else a[4*i+j] = 0 ; int low = 0 , top = 1 ; for(i = 0 ; i < 16 ; i++) p[0].a[i] = a[i] ; p[0].t = 0 ; ; p[0].n = -1 ; p[0].top = 0 ; while(low < top) { node k = p[low++] ; int num = 0 ; for(i = 0 ; i < 16 ; i++) num += k.a[i] ; if(num == 0 && k.t < min_t) { min_t = k.t ; to = low-1 ; } if(k.n == 15) continue ; if(k.t >= min_t) continue ; p[top] = k ; p[top].n++ ; top++ ; p[top] = k ; p[top].n++ ; p[top].t++ ; p[top].s[ p[top].top++ ] = p[top].n ; i = p[top].n / 4 ; j = p[top].n % 4 ; int s ; for(s = 0 ; s < 4 ; s++) { p[top].a[4*i+s] = 1 - p[top].a[4*i+s] ; p[top].a[4*s+j] = 1 - p[top].a[4*s+j] ; } p[top].a[4*i+j] = 1 - p[top].a[4*i+j] ; top++ ; } printf("%d\n", p[to].top); int q ; for(q = 0 ; q < p[to].top ; q++) { i = p[to].s[q] / 4 ; j = p[to].s[q] % 4 ; i++; j++ ; printf("%d %d\n", i , j ); } return 0; }
相关文章推荐
- poj2965 The Pilots Brothers' refrigerator
- poj2965 The Pilots Brothers' refrigerator(位运算)
- POJ2965-The Pilots Brothers' refrigerator
- 【枚举】POJ1753 Flip Game&POJ2965 The Pilots Brothers' refrigerator
- [POJ2965]The Pilots Brothers' refrigerator【枚举】
- poj2965 The Pilots Brothers' refrigerator
- poj2965-poj2965-The Pilots Brothers' refrigerator
- poj2965 The Pilots Brothers\' refrigerator
- POJ2965 The Pilots Brothers' refrigerator(枚举)
- POJ2965 The Pilots Brothers' refrigerator 枚舉題目
- POJ2965----The Pilots Brothers' refrigerator
- POJ1753 Flip Game & POJ2965 The Pilots Brothers' refrigerator (DFS)
- 暴力枚举-POJ2965 The Pilots Brothers' refrigerator
- poj2965 The Pilots Brothers' refrigerator
- POJ2965-The Pilots Brothers' refrigerator
- DFS.枚舉::poj1753 flip game && poj2965 the Pilots Brothers'refrigerator
- poj2965 The Pilots Brothers' refrigerator(直接计算或枚举Enum+dfs)
- poj2965-The Pilots Brothers' refrigerator
- 枚举 poj2965 The Pilots Brothers' refrigerator
- POJ2965-The Pilots Brothers' refrigerator