POJ 1753 BFS+状压

给出4*4的黑白棋盘

可以对任意一个点进行翻转(黑->白,或 白->黑),同时会翻转其相邻的四个点

问最少的步骤使棋盘变成全黑或者全白,状压存状态即可

#include "stdio.h"
#include "string.h"
#include "queue"
using namespace std;

int dir[5][2]={ {1,0},{-1,0},{0,1},{0,-1},{0,0} };
char str[10][10];
int b[4][4]=
{ {1,2,4,8},
  {16,32,64,128},
  {256,512,1024,2048},
  {4096,8192,16384,32768}
};

int used[100010];

int bfs()
{
    queue<int>q;
    int cur,next;
    int i,j,x,y,l;
    cur=0;
    for (i=0;i<4;i++)
        for (j=0;j<4;j++)
        if (str[i][j]=='b')
        cur+=b[i][j];
    q.push(cur);
    if (cur==0 || cur==65535) return 0;
    memset(used,-1,sizeof(used));
    used[cur]=0;
    while (!q.empty())
    {
        cur=q.front();
        q.pop();
        for (i=0;i<4;i++)
            for (j=0;j<4;j++)
            {
                next=cur;
                for (l=0;l<5;l++)
                {
                    x=i+dir[l][0];
                    y=j+dir[l][1];
                    if (x<0 || x>=4 || y<0 || y>=4) continue;
                    next^=b[x][y];
                }
                if (next==0 || next==65535) return used[cur]+1;
                if (used[next]==-1)
                {
                    used[next]=used[cur]+1;
                    q.push(next);
                }
            }

    }
    return -1;
}

int main()
{
    int i,ans;
    while (scanf("%s",str[0])!=EOF)
    {
        for (i=1;i<4;i++)
            scanf("%s",str[i]);
        ans=bfs();
        if (ans==-1)
            printf("Impossible\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-12 05:04:49

POJ 1753 BFS+状压的相关文章

POJ 2688 BFS+状压DP

标准的TSP问题 m*n矩阵,有不超过10个需要走到的点,给出起点,问走最少的步子把所有点走完 BFS出每个必须走到的点的最短距离 然后状压DP即可 #include "stdio.h" #include "string.h" #include "queue" using namespace std; const int dir[4][2]={ {1,0},{-1,0},{0,1},{0,-1} }; int inf=0x7fffffff; in

hdu 5094 Maze (BFS+状压)

题意: n*m的迷宫.多多要从(1,1)到达(n,m).每移动一步消耗1秒.有P种钥匙. 有K个门或墙.给出K个信息:x1,y1,x2,y2,gi    含义是(x1,y1)与(x2,y2)之间有gi.gi=0:墙   1,2,3.... :第1种门,第2种门,第3种门..... 有S把钥匙.给出S个信息:x1,y1,qi    含义是位置(x1,y1)上有一把第qi种的钥匙. 问多多最少花多少秒到达(n,m).若无法到达输出-1. 数据范围: (1<= n, m <=50, 0<= p

hdu 4771 Stealing Harry Potter&#39;s Precious (BFS+状压)

题意: n*m的迷宫,有一些格能走(“.”),有一些格不能走(“#”).起始点为“@”. 有K个物体.(K<=4),每个物体都是放在“.”上. 问最少花多少步可以取完所有物体. 思路: BFS+状压,看代码. 代码: struct node{ int x,s; node(int _x,int _s){ x=_x, s=_s; } }; int n,m,k,sx,sy; char graph[105][105]; int px[5],py[5]; int dp[105][105][35]; int

【HDU 4771 Stealing Harry Potter&#39;s Precious】BFS+状压

2013杭州区域赛现场赛二水... 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点,搜索下一个最近的宝藏,直至找到全部k个宝藏.有点贪心的感觉. 由于求最短时间,BFS更快捷,但耗内存,这道题就卡在这里了... 这里记录了我几次剪枝的历史...题目要求内存上限32768KB,就差最后600KB了...但我从理论上觉得已经不能再剪了,留下的结点都是盲目式搜索必然要访问的结点. 在此贴

HDU 4856 Tunnels(BFS+状压DP)

HDU 4856 Tunnels 题目链接 题意:给定一些管道,然后管道之间走是不用时间的,陆地上有障碍,陆地上走一步花费时间1,求遍历所有管道需要的最短时间,每个管道只能走一次 思路:先BFS预处理出两两管道的距离,然后状态压缩DP求解,dp[s][i]表示状态s,停在管道i时候的最小花费 代码: #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using

HDU-3502-Huson&#39;s Adventure Island(BFS+状压DP)

Problem Description A few days ago, Tom was tired of all the PC-games, so he went back to some old FC-games. "Hudson's Adventure Island" was his favorite which he had played thousands of times. But to his disappointed, the more he played, the mo

HDU-4856 Tunnels (BFS+状压DP)

Problem Description Bob is travelling in Xi’an. He finds many secret tunnels beneath the city. In his eyes, the city is a grid. He can’t enter a grid with a barrier. In one minute, he can move into an adjacent grid with no barrier. Bob is full of cur

HDU 5025 BFS+状压

2014 ACM/ICPC Asia Regional Guangzhou Online N*N矩阵 M个钥匙 K起点,T终点,S点需多花费1点且只需要一次,1-9表示9把钥匙,只有当前有I号钥匙才能拿I+1号钥匙,可以不拿钥匙只从上面走过 4维数组判重,第三维表示钥匙已经拿到第几把,第四维表示已经走过的S的状况,用状压存储 #include "stdio.h" #include "string.h" #include "queue" using

hdu 1429 bfs+状压

题意:这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方.刚开始 Ignatius被关在(sx,sy)的位置,离开地牢的门在(ex,ey)的位置.Ignatius每分钟只能从一个坐标走到相邻四个坐标中的其中一 个.魔王每t分钟回地牢视察一次,若发现Ignatius不在原位置便把他拎回去.经过若干次的尝试,Ignatius已画出整个地牢的地图.现在请你帮 他计算能否再次成功逃亡.只要在魔王下次视察之前走到出口就算离开地牢