poj 1222
来源:互联网 发布:java 写html文件 编辑:程序博客网 时间:2024/05/21 08:11
http://poj.org/problem?id=1222
题意:5*6矩阵中有30个灯,操作一个灯,周围的上下左右四个灯会发生相应变化 即由灭变亮,由亮变灭,如何操
作使灯全灭?
EXTENDED LIGHTS OUT
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 7800 Accepted: 5075
Description
In an extended version of the game Lights Out, is a puzzle with 5 rows of 6 buttons each (the actual puzzle has 5 rows of 5 buttons each). Each button has a light. When a button is pressed, that button and each of its (up to four) neighbors above, below, right and left, has the state of its light reversed. (If on, the light is turned off; if off, the light is turned on.) Buttons in the corners change the state of 3 buttons; buttons on an edge change the state of 4 buttons and other buttons change the state of 5. For example, if the buttons marked X on the left below were to be pressed,the display would change to the image on the right.
The aim of the game is, starting from any initial set of lights on in the display, to press buttons to get the display to a state where all lights are off. When adjacent buttons are pressed, the action of one button can undo the effect of another. For instance, in the display below, pressing buttons marked X in the left display results in the right display.Note that the buttons in row 2 column 3 and row 2 column 5 both change the state of the button in row 2 column 4,so that, in the end, its state is unchanged.
Note:
1. It does not matter what order the buttons are pressed.
2. If a button is pressed a second time, it exactly cancels the effect of the first press, so no button ever need be pressed more than once.
3. As illustrated in the second diagram, all the lights in the first row may be turned off, by pressing the corresponding buttons in the second row. By repeating this process in each row, all the lights in the first
four rows may be turned out. Similarly, by pressing buttons in columns 2, 3 ?, all lights in the first 5 columns may be turned off.
Write a program to solve the puzzle.
The aim of the game is, starting from any initial set of lights on in the display, to press buttons to get the display to a state where all lights are off. When adjacent buttons are pressed, the action of one button can undo the effect of another. For instance, in the display below, pressing buttons marked X in the left display results in the right display.Note that the buttons in row 2 column 3 and row 2 column 5 both change the state of the button in row 2 column 4,so that, in the end, its state is unchanged.
Note:
1. It does not matter what order the buttons are pressed.
2. If a button is pressed a second time, it exactly cancels the effect of the first press, so no button ever need be pressed more than once.
3. As illustrated in the second diagram, all the lights in the first row may be turned off, by pressing the corresponding buttons in the second row. By repeating this process in each row, all the lights in the first
four rows may be turned out. Similarly, by pressing buttons in columns 2, 3 ?, all lights in the first 5 columns may be turned off.
Write a program to solve the puzzle.
Input
The first line of the input is a positive integer n which is the number of puzzles that follow. Each puzzle will be five lines, each of which has six 0 or 1 separated by one or more spaces. A 0 indicates that the light is off, while a 1 indicates that the light is on initially.
Output
For each puzzle, the output consists of a line with the string: "PUZZLE #m", where m is the index of the puzzle in the input file. Following that line, is a puzzle-like display (in the same format as the input) . In this case, 1's indicate buttons that must be pressed to solve the puzzle, while 0 indicate buttons, which are not pressed. There should be exactly one space between each 0 or 1 in the output puzzle-like display.
Sample Input
20 1 1 0 1 01 0 0 1 1 10 0 1 0 0 11 0 0 1 0 10 1 1 1 0 00 0 1 0 1 01 0 1 0 1 10 0 1 0 1 11 0 1 1 0 00 1 0 1 0 0
Sample Output
PUZZLE #11 0 1 0 0 11 1 0 1 0 10 0 1 0 1 11 0 0 1 0 00 1 0 0 0 0PUZZLE #21 0 0 1 1 11 1 0 0 0 00 0 0 1 0 01 1 0 1 0 11 0 1 1 0 1方法一;;
#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>using namespace std;const int dx[5]= {-1,0,0,0,1};const int dy[5]= {0,-1,0,1,0};int tile[10][10];int opt[10][10];int flip[10][10];int get(int x,int y){ int c=tile[x][y]; for(int d=0; d<5; d++) { int x2=x+dx[d],y2=dy[d]+y; if(0<=x2&&x2<5&&y2>=0&&y2<6) { c+=flip[x2][y2]; } } return c%2;}int calc (){ for(int i=1; i<5; i++) { for(int j=0; j<6; j++) { if(get(i-1,j)!=0) { flip[i][j]=1; } } } for(int j=0; j<6; j++) if(get(4,j)!=0) return -1; int res=0; for(int i=0; i<5; i++) { for(int j=0; j<6; j++) { res+=flip[i][j]; } } return res;}void solve(){ int res=-1; for(int i=0; i<1<<6; i++) { memset(flip,0,sizeof(flip)); for(int j=0; j<6; j++) { flip[0][j]=i>>j&1; } int num=calc(); if(num>=0&&(res<0||res>num)) { res=num; memcpy(opt,flip,sizeof(flip)); } } if(res<0) { printf("01\n"); } else for(int i=0; i<5; i++) { for(int j=0; j<6; j++) { printf("%d%c",opt[i][j],j+1==6?'\n':' '); } }}int main(){ int t,T=1; cin>>t; while(t--) { for(int i=0; i<5; i++) for(int j=0; j<6; j++) cin>>tile[i][j]; cout<<"PUZZLE #"<<T++<<endl; solve(); } return 0;}
#include <iostream> #include <string.h> #include <algorithm> #include <stdio.h> #include <math.h> using namespace std; const int N = 35; int gcd(int a,int b) { return b ? gcd(b,a%b):a; } int lcm(int a,int b) { return a / gcd(a,b) * b; } void Gauss(int a[][N],int n,int m,int &r,int &c) { r = c = 0; for(; r<n && c<m; r++,c++) { int maxi = r; for(int i=r+1; i<n; i++) if(abs(a[i][c]) > abs(a[maxi][c])) maxi = i; if(maxi != r) { for(int i=r; i<m+1; i++) swap(a[r][i],a[maxi][i]); } if(a[r][c] == 0) { r--; continue; } for(int i=r+1; i<n; i++) { if(a[i][c] != 0) { int x = abs(a[i][c]); int y = abs(a[r][c]); int LCM = lcm(x,y); int tx = LCM / x; int ty = LCM / y; if(a[i][c] * a[r][c] < 0) ty = -ty; for(int j=c; j<m+1; j++) a[i][j] = ((a[i][j] % 2 * tx % 2 - a[r][j] % 2 * ty % 2) % 2 + 2) % 2; } } } } int Rewind(int a[][N],int x[],int r,int c) { for(int i=r-1; i>=0; i--) { int t = a[i][c] % 2; for(int j=i+1; j<c; j++) { if(a[i][j] != 0) t -= a[i][j] % 2 * x[j] % 2; } x[i] = t / a[i][i] % 2; x[i] = (x[i] + 2) % 2; } return 0; } int a[N][N]; int x[N]; int main() { int cas = 1; int n,m,T; scanf("%d",&T); while(T--) { n = m = 30; memset(a,0,sizeof(a)); for(int i=0; i<5; i++) { for(int j=0; j<6; j++) { if(i >= 1) a[6*i+j][6*(i-1)+j] = 1; if(i <= 3) a[6*i+j][6*(i+1)+j] = 1; if(j >= 1) a[6*i+j][6*i+j-1] = 1; if(j <= 4) a[6*i+j][6*i+j+1] = 1; a[6*i+j][6*i+j] = 1; scanf("%d",&a[6*i+j][30]); } } int r,c; int cnt = 0; Gauss(a,n,m,r,c); Rewind(a,x,r,c); printf("PUZZLE #%d\n",cas++); for(int i=0; i<30; i++) { cnt++; if(cnt % 6) printf("%d ",x[i]); else printf("%d\n",x[i]); } } return 0; }
0 0
- poj 1222
- POJ 1222
- POJ 1222
- poj 1222
- 【POJ】1222
- 开关问题 POJ 3276 POJ 3279 POJ 1222
- zoj 1354 poj 1222
- POJ 1222 熄灯游戏
- poj 1222(高斯消元法求解。。。。)
- poj 1222 解题报告
- POJ 1222 高斯消元法
- POJ 1222 解题报告
- POJ 1222 熄灯问题
- poj 1222 (高斯消元)
- POJ 1222 高斯消元法
- POJ 1222 高斯消元
- poj 1222 高斯消元
- poj 1222 高斯消元详解
- C语言复习day2
- PAT(甲级)1048
- WPF 中图标路径问题
- jqery实现10X10的表格,双击消失
- 停课的沉淀反思(四)——任道重远
- poj 1222
- 自定义的RPC的Java实现
- Bios工程师手边事—battery
- leetcode 080 Remove Duplicates from Sorted Array II(难易度:Medium)
- 查找(二)二叉查找数BST
- PAT(甲级)1049
- android读入一个txt文件
- 读写文件的工具类IOUtils 和FileUtils
- PAT(甲级)1050