• POJ1753(DFS,Notation,Bit operation,Queue)


    1753:Flip Game

    时间限制:

    1000ms

    内存限制:

    65536kB

    描述

    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.

    输入

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

    输出

    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).

    样例输入

    bwwb

    bbwb

    bwwb

    bwww

    样例输出

    4

     

    #include"iostream"
    #include"queue"
    using namespace std;
    const int MAX=0xffff;//即2^16,分别对应状态id从0到1111111111111111
    int for_reverse[4][4]=
    {0xc800,0xe400,0x7200,0x3100,
    0x8c80,0x4e40,0x2720,0x1310,
    0x08c8,0x04e4,0x0272,0x0131,
    0x008c,0x004e,0x0027,0x0013};
    int main()
    {
     char input;
     int id=0;
     for(int i=0;i<4;i++)
      for(int j=0;j<4;j++)
      {
       cin>>input;
       if(input=='w')
       {
        id<<=1;
       }
       else if(input=='b')
       {
        id<<=1;
        id++;
       }
      }
      bool done[MAX]={false};//记录状态id=i是否遍历过
      int step[MAX];//记录到达状态id=i需要的次数
      done[id]=true;//初始点标记为遍历过
      step[id]=0;//初始状态对应id次数
      queue<int> st;
      st.push(id);
       if(id==0||id==0xffff)
       {cout<<0<<endl;return 0;}   //如果初始状态已经满足要求直接输出
      while(!st.empty())//利用栈DFS遍历不同id值
      {
       int temp=st.front();
       st.pop();
       id=temp;
       //cout<<"id:"<<id<<endl;
       //cout<<"step[id]"<<step[id]<<endl;
       for(int i=0;i<4;i++)//遍历16种操作
        for(int j=0;j<4;j++)
        {
         temp=id;//注意了,每次都要重新赋值
         temp^=for_reverse[i][j];//利用异或符翻转
         if(temp==0||temp==0xffff)
         {
          cout<<step[id]+1<<endl;
          return 0;
         }
         else if(done[temp]==false)
         {
          step[temp]=step[id]+1;
          done[temp]=true;
          st.push(temp);
         }
        }//结束操作遍历
      }//结束状态值id遍历
      cout<<"Impossible"<<endl;
      return 0;
    }

  • 相关阅读:
    Borladn传奇终结巨人倒下了
    C#数据导出到excel
    ADO.NET Entity Framework学习笔记(4)ObjectQuery对象
    ADO.NET Entity Framework学习笔记(3)ObjectContext对象[转]
    在Vista,WIN2K8下IE7.0就不能正常访问asp.net development server解决办法
    获取SQL Server表字段的各种属性
    白话说.net事件和委托
    Ado.Net Entity : Object doesn’t display linked members (foreign keys)
    移动飞信WEB发送服务接口
    asp.net FormsAuthentication 认证
  • 原文地址:https://www.cnblogs.com/lzhitian/p/2140072.html
Copyright © 2020-2023  润新知