Poj 2965 the pilots brothers 'refrigerator BFS + bitwise operation

Source: Internet
Author: User

The refrigerator has 16 switches, which are arranged in a 4*4 matrix. Each time you change the status of a switch, all the switches in the same column with it must also change the status. Find the minimum number of adjustments required to open the refrigerator.
Problem: the DFS should also work.

#include <queue>#include <iostream>using namespace std;queue<int> que;int step[1<<16];int point[1<<16];int cell[] = { 4383, 8751, 17487, 34959, 4593, 8946, 17652, 35064, 7953, 12066, 20292, 36744, 61713, 61986, 62532, 63624 };int main(){char ch; int i, pos = 0;for ( i = 0; i < 16; i++ ){cin >> ch;if ( ch == '+' )pos += ( 1 << i );}memset(step,-1,sizeof(step));memset(point,-1,sizeof(point));que.push ( pos );step[pos] = 0;int current, next;while ( !que.empty () ){current = que.front ();que.pop();for ( i = 0; i < 16; i++ ){next = current ^ cell[i];if ( step[next] == -1 ){point[next] = i;step[next] = step[current] + 1;que.push ( next );}if ( next == 0 ){cout << step[current] + 1 << endl;while ( next != pos ){cout << point[next] / 4 + 1 << ' ' << point[next] % 4 + 1 << endl;next = next ^ cell[point[next]];}return 0;}}}return 0;}

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.