Poj 2965 the pilots brothers 'refrigerator (enumeration)

Source: Internet
Author: User

Question link: http://poj.org/problemstatus? Problem_id = 2965 & START = 120 & orderby = Time & language =-1

This question is also relatively simple. First of all, we need to clarify that there is no requirement for the order of each point, that is, one point is either flipped or not flipped, And it is irrelevant to the order, understand this.

The question is simple. There are 2 ^ 16 cases in total. If you use a binary bit to enumerate it, it will be OK. If you use binary 1, it indicates that 0 is flipped, but not flipped;

Second, there are some technical optimizations.

First: count the number of binary 1, this with X & X-1 how many times, it should be the fastest speed

Second: Convert the char type to the bool type to facilitate processing.

Third: Use bitwise operations to calculate whether the bit is 1, so the speed is faster.

Fourth: using G ++, the GNU Compiler may be better at bitwise operation optimization.

#include <iostream>#include <string.h>#include <stdio.h>using namespace std;#define maxn 1<<16char rec[4][4];bool map[4][4];bool temp[4][4];int init(){    int i,j;    for(i=0;i<4;i++)    for(j=0;j<4;j++)    if(rec[i][j]=='+')    map[i][j]=true;    else    map[i][j]=false;    return 0;}bool is_ok(){    bool flag=false;    int i,j;    for(i=0;i<4;i++)    for(j=0;j<4;j++)    {       flag=temp[i][j]||flag;    }    return flag==false;}int num_of_two(int a){    int num=0;    while(a)    {        a=a&(a-1);        num++;    }    return num;}int change(int i,int j){    int a;    for(a=0;a<4;a++)    temp[i][a]=!temp[i][a],temp[a][j]=!temp[a][j];    temp[i][j]=!temp[i][j];//three    return 0;}bool find_ans(int k){    int i;    for(i=0;i<16;i++)    {        if(k&(1<<i))        change(i%4,i/4);    }    return is_ok();}int out_put(int k){    for(int i=0;i<16;i++)    {        if(k&(1<<i))        printf("%d %d\n",i%4+1,i/4+1);    }    return 0;}int main(){    int i,j,k;    int ans=0,num_two,n;    while(scanf("%s",rec[0])!=EOF)    {        num_two=10000;        ans=0;        scanf("%s%s%s",rec[1],rec[2],rec[3]);        init();        for(i=0;i<maxn;i++)        {            n=num_of_two(i);            if(n>=num_two)            continue;            memcpy(temp,map,sizeof(map));            if(find_ans(i))            ans=i,num_two=n;        }        printf("%d\n",num_two);        out_put(ans);    }    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.