C ++ basic algorithm learning-lamp-out problem, algorithm lamp-out

Source: Internet
Author: User

C ++ basic algorithm learning-lamp-out problem, algorithm lamp-out

There is a matrix composed of buttons. Each row has 6 buttons, 5 in total.
-Each button has a light on it.
-When a button is pressed, the button and its surrounding position (top, bottom,
The lights on the left and right change the status.
26 lights out POJ1222
-If the light turns on, it will be extinguished.
-If the light is turned off, it will be lit.
• The button on the matrix corner changes the status of the three lamps.
• The button on the edge of the matrix changes the status of the four lights
• Other buttons change the status of 5 lamps
27 lights out POJ1222
When multiple buttons adjacent to a lamp are pressed, one operation will offset another operation.
Result
Given the initial state of each lamp in the matrix, find a push button scheme to make all
The problem of turning off all the lights is POJ1222
Input:
-The first line is a positive integer N, indicating the number of cases to be resolved
-Each case consists of five rows and each row contains six numbers.
-The numbers are separated by spaces, which can be 0 or 1.
-0 indicates that the initial state of the lamp is off.
-1 indicates that the light is in the initial state.
29 lights out
Output:
-Output a line for each case first,
Output string "PUZZLE # m", where m is the serial number of the case
-Output 5 rows according to the input format of the case.
• 1 indicates that the corresponding Button needs to be pressed
• 0 indicates that you do not need to press the corresponding button
• Each number is separated by a space.

The Code is as follows:

# Include <iostream> # include <string. h> using namespace std; char Oright [5]; char Light [5]; char Result [5]; void SetChar (char & t, int j, int s ); void OutPut (char * t); int GetChar (char t, int j); void Flip (char & s, int I); int main () {int switches; for (int I = 0; I <5; I ++) // one-bit storage. {For (int j = 0; j <6; j ++) {int s; cin> s; SetChar (Oright [I], j, s );}} for (int I = 0; I <64; I ++) {memcpy (Light, Oright, sizeof (Oright); switches = I; for (int j = 0; j <5; j ++) // For the I value, each row starts to test {Result [j] = switches; // The Result stores the switch pressing scheme. For (int k = 0; k <6; k ++) // test each column {if (GetChar (switches, k) {if (k> 0) flip (Light [j], k-1); // The left begins to change Flip (Light [j], k); if (k <5) Flip (Light [j], k + 1); // start change on the right} if (j <4) Light [j + 1] ^ = switches; switches = Light [j];} if (Light [4] = 0) {cout <"end! "<Endl; OutPut (Result); break ;}return 0;} void Flip (char & s, int I) {s = s ^ (1 <I);} int GetChar (char t, int j) {return (t> j) & 1 ;} void SetChar (char & t, int j, int s) {if (s = 1) t = t | (1 <j); else t = t &(~ (1 <j);} void OutPut (char * t) {for (int I = 0; I <5; I ++) {for (int j = 0; j <6; j ++) {cout <GetChar (t [I], j); cout <";}cout <endl ;}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.