poj1753

#include<iostream>
using namespace std;
#include<vector>
#include <cmath>
#include <cstdio>
int chess[4][4];
int total;
#define inf 99999999
int judge()
{
    int temp=chess[0][0];
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            if(chess[i][j]!=temp)
                return 0;
        }
    }
    return 1;
}
void turn(int x,int y)
{
        chess[x][y]=!chess[x][y];
        if(x-1>=0)
            chess[x-1][y]=!chess[x-1][y];
        if(x+1<4)
            chess[x+1][y]=!chess[x+1][y];
        if(y-1>=0)
            chess[x][y-1]=!chess[x][y-1];
        if(y+1<4)
            chess[x][y+1]=!chess[x][y+1];
}
int DFS(int x,int y,int z)
{

  //  cout<<"("<<x+1<<","<<y+1<<")
";
    if(judge())
    {
        if(total>z)
        total=z;
            return 0;
    }
    if(x>=4||y>=4)
        return 0;
    int next_x,next_y;
    next_x=(x+1)%4;
    next_y=y+(x+1)/4;

    DFS(next_x,next_y,z);

    turn(x,y);//反转

    DFS(next_x,next_y,z+1);

    turn(x,y);//变回原样

    return 0;
}
int main()
{
  total=inf;
   char s[10];
   for(int i=0;i<4;i++)
   {
       scanf("%s",s);
       for(int j=0;j<4;j++)
       {
          if(s[j]=='b')
            chess[i][j]=0;
          else if(s[j]=='w')
            chess[i][j]=1;
       }
   }
   DFS(0,0,0);
   if(total==inf)
    cout<<"Impossible
";
   else
    cout<<total<<endl;

   return 0;
}
原文地址:https://www.cnblogs.com/lulichuan/p/6364664.html