zoukankan      html  css  js  c++  java
  • (回归2.0)A

    Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules: 

    1. Choose any one of the 16 pieces. 
    2. Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).


    Consider the following position as an example: 

    bwbw 
    wwww 
    bbwb 
    bwwb 
    Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become: 

    bwbw 
    bwww 
    wwwb 
    wwwb 
    The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal. 

    Input

    The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.

    Output

    Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).

    Sample Input

    bwwb
    bbwb
    bwwb
    bwww

    Sample Output

    4

    更一道水题证明我开始训练

    由于本身的情况数量并不多

    直接dfs

    这题运用位运算能大大简化代码量(用map易超时)

    #include<cstdio>
    #include<queue>
    #include<cstring>
    #include<iostream>
    using namespace std;
    char s[10][10];
    bool vis[140000];
    queue<pair<int,int> > q;
    int ans=-1;
    void bfs()
    {
        while(q.size())
        {
            int tmp=q.front().first;
            int pos=q.front().second;
            int tmp1;
            q.pop();
            if(tmp==0||tmp==65535)
            {
                ans=pos;
                break;
            }
            for(int i=0;i<=15;i++)
            {
                tmp1=tmp;
                tmp1=(tmp1)^(1<<i);
                if(i%4!=0)//not left
                {
                    tmp1=tmp1^(1<<i-1);
                }
                if(i%4!=3)//not right
                {
                    tmp1=tmp1^(1<<i+1);
                }
                if(i>3)//not up
                {
                    tmp1=tmp1^(1<<i-4);
                }
                if(i<12)//not down
                {
                    tmp1=tmp1^(1<<i+4);
                }
                if(vis[tmp1]==0)
                {
                    q.push(make_pair(tmp1,pos+1));
                    vis[tmp1]=1;
                }
            }
        }
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        memset(vis,0,sizeof(vis));
        for(int i=1; i<=4; i++)
        {
            cin>>s[i]+1;
        }
        int cnt=0;
        int num=0;
        for(int i=1;i<=4;i++)
        {
            for(int j=1;j<=4;j++)
            {
                if(s[i][j]=='b')
                {
                    num=num|(1<<cnt);
                    //cout<<num<<" "<<cnt<<endl;
                }
                cnt++;
            }
        }
        vis[num]=1;
        q.push(make_pair(num,0));
        //cout<<num<<endl;
        bfs();
        if(ans==-1)
        {
            printf("Impossible
    ");
        }
        else
        {
            printf("%d
    ",ans);
        }
    }
    
  • 相关阅读:
    第二篇第十一章灭火救援设施
    第二篇第六章安全疏散
    第二篇第五章防火防烟分区于分隔
    第二篇第三章建筑分类与耐火等级
    applicationContext-solr.xml
    solrj 操作 solr 集群版
    centos solr 集群搭建
    org.apache.ibatis.binding.BindingException
    全文检索基础
    solrj 操作 solr 单机版
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852246.html
Copyright © 2011-2022 走看看