POJ 2965 the pilots ' refrigerator

Source: Internet
Author: User

Test instructions: There are 4x4 switches, each change the state of a switch, will simultaneously change the same row and the same row of the state of the switch, give the initial state, the minimum number of steps required to turn all the switches into open, and output scheme.

Solution: Enumeration + pruning. Direct violence enumeration unexpectedly t ... Feel not too scientific ... The complexity of 2^16*16 ... Had to add a pruning, record the best answers that have been enumerated, and then just enumerate to the best answers, if the steps that exceed the best answer are definitely not the best solution.

Code:

#include <stdio.h> #include <iostream> #include <algorithm> #include <string> #include < string.h> #include <math.h> #include <limits.h> #include <time.h> #include <stdlib.h># include<map> #include <queue> #include <set> #include <stack> #include <vector> #define LL    Long longusing namespace Std;int main () {iOS:: Sync_with_stdio (false);    Char maze[4][5];        while (Cin >> Maze[0]) {for (int i = 1; i < 4; i++) cin >> Maze[i];        int MAXN = 1 << 16;        int ans = 1000000, pos = 0;            for (int i = 0; i < MAXN; i++) {int res = 0;            int cnt[4][4] = {0};            int flag = 1;                    for (int j = 0; J <; J + +) {if (I & (1 << j)) {                    res++;                        if (res > ans)//pruning {flag = 0;               Break     } for (int k = 0; k < 4; k++) {cnt[j/4][k]++;                    Cnt[k][j% 4]++;                } cnt[j/4][j% 4]--; }} for (int j = 0; J < 4 && Flag; j + +) for (int k = 0; k < 4 && Flag                    k++) {int tmp = maze[j][k] = = ' + '? 1:0;                    TMP + = Cnt[j][k];                if (TMP & 1) flag = 0;                } if (Flag && (res < ans)) {ans = res;            pos = i;        }} if (ans! = 1000000) cout << ans << endl;        else cout << "0" << Endl; for (int i = 0; i < i++) {if (pos & (1 << i)) {cout <<            I/4 + 1 << ' << i% 4 + 1 << endl;       } }} return 0;} 

  

POJ 2965 the pilots ' refrigerator

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.