POJ 1753 Flip Game (bfs)

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int ch[20];
int op[4][2]={1,0, -1,0, 0,1, 0,-1};
int mat[200000];
int vis[200000];
void init_ch()
{
    int i,j,k;
    int temp;
    int cnt=0;
    for(i=0;i<4;i++)
    {
        for(j=0;j<4;j++)
        {
            temp=0;
            temp^=(1<<((3-i)*4+(3-j)));
            for(k=0;k<4;k++)
            {
                int x=i+op[k][0];
                int y=j+op[k][1];

                if(x<0||x>3||y<0||y>3) continue;
                temp^=(1<<((3-x)*4+(3-y)));
            }
            ch[cnt++]=temp;
        }
    }
}
void init_ans()
{
    mat[0]=0;
    int i,j,k;
    queue<int> q;
    q.push(0);
    while(!q.empty())
    {
        int now=q.front();
        int next;
        vis[now] = 1;
        q.pop();

        for(i=0;i<16;i++)
        {
            next=now^ch[i];
            if(vis[next]==0)
            {vis[next]=1;
                mat[next]=mat[now]+1;
                q.push(next);
            }
        }
    }
}
int main()
{
   int i,j,k;
   memset(vis,0,sizeof(vis));
   memset(mat,-1,sizeof(mat));
   init_ch();
   init_ans();
   char str[10][10];
   while(scanf("%s",str[0])!=EOF)
   {
       for(i=1;i<4;i++)
       {
           scanf("%s",str[i]);
       }

       /*
       for(i=0;i<4;i++)
       {
           printf("%s\n",str[i]);
       }
       printf("\n");
       */

       int now=0,now1=0;
       for(i=0;i<4;i++)
       {
           for(j=0;j<4;j++)
           {
               now1<<=1;
               now<<=1;
               if(str[i][j]==‘b‘) now+=1;
               if(str[i][j]==‘w‘) now1+=1;
               //printf("%d %d\n",i,now);
           }
       }
       //now1=now^(1<<16-1);

       int ans;

       if(vis[now]==0&&vis[now1]==0)
       {
           printf("Impossible\n");
       }
       else if(vis[now]==0)
       {
           printf("%d\n",mat[now1]);
       }
       else if(vis[now1]==0)
       {
           printf("%d\n",mat[now]);
       }
       else printf("%d\n",min(mat[now],mat[now1]));

   }
    return 0;
}
时间: 2024-08-13 17:31:41

POJ 1753 Flip Game (bfs)的相关文章

poj 1753 Flip Game (bfs + 枚举)

链接:poj 1753 题意:这是翻棋游戏,给定4*4棋盘,棋子一面为黑色(用b表示),另一面为白色(用w表示),问至少要几步可以将棋子翻为全黑或者全白,如不能达到目的,输出"Impossible " 翻转规则:可以选定16个棋子中的任意一个,将其本身以及上下左右相邻的翻转过来 分析:其实每格棋子最多只可以翻转一次(实际是奇数次,但与翻转一次状态一样),只要其中一格重复翻了2次(不论是连续翻动还是不连翻动),那么它以及周边的棋子和没翻动时的状态是一致的,与最初状态未翻转一样,由此就可以

poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

Description 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 f

POJ 1753 Flip Game (高斯消元 枚举自由变元求最小步数)

题目链接 题意:4*4的黑白棋,求把棋全变白或者全变黑的最小步数. 分析:以前用状态压缩做过. 和上题差不多,唯一的不同是这个终态是黑棋或者白棋, 但是只需要把给的初态做不同的两次处理就行了. 感觉现在还只是会套模板,不能独立的思考,好伤心.... 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath&g

POJ 1753 Flip Game(dfs+枚举)

POJ 1753 题意: 输入一个4*4的图像,由黑白两色组成,定义一种操作为:改变某个格子内小球的颜色(黑变白,白变黑),同时其上下左右的格子内小球也将变色.求最少多少次操作能使之成为纯色图案. 思路: 对一个格子操作偶数次等于没有操作,操作奇数次等于操作一次,所以答案在0~16以及impossible之间. 从n=0开始枚举n次操作可能的组成情况,即操作哪几个格子,若某种组合能变图案为纯色则停止. 由于总组合数达到2^16,故枚举组合情况可以用dfs来进行回溯枚举. //还有一种方法是位运算

POJ 1753 Flip Game(枚举+DFS)

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 p

POJ 1753 Flip Game (状压+暴力)

题目链接:http://poj.org/problem?id=1753 题意: 给你一个4*4的棋盘,上面有两种颜色的棋子(一种黑色,一种白色),你一次可以选择一个棋子翻转它(黑色变成白色,同理反之),选择的这枚棋子的上下左右都会被翻动(前提是上下左右都可以被翻动).问最少可以翻动多少颗棋子,让整个棋盘变成全部黑色或者全部白色. 题解: 4*4一共就16个格子,每个格子都可以是翻或者不翻,那么就是216翻法. 所以就可以用状态压缩的方法来解决. 比如说47. 47的二进制是00101111,我们

POJ 1753 Flip Game(二进制枚举)

题目地址链接:http://poj.org/problem?id=1753 题目大意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白->黑)时,其周围上下左右(如果存在的话)的格子的颜色也被反转,问至少反转几个格子可以使4*4的正方形变为纯白或者纯黑? 解题思路: 因为只有16个格子,且只有黑白两种状态,想到用一个二进制整数来表示棋盘的状态.首先你需要明白这个题目的两个性质——任何一个格子翻偶数次等同于不翻:翻格子的顺序对最终的结果是没有影响的,也就

poj 1753 Flip Game(高斯消元)

Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30805   Accepted: 13409 Description 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

POJ 题目1753 Flip Game(DFS)

Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33029   Accepted: 14435 Description 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