POJ 2965The Pilots Brothers' refrigerator (bfs)

Description

The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.

There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.

The task is to determine the minimum number of handle switching necessary to open the refrigerator.

Input

The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.

Output

The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.

Sample Input

-+--
----
----
-+--

Sample Output

6
1 1
1 3
1 4
4 1
4 3
4 4
这个题先把二维数组转化为一维数组,为了方便。然后a表示棋盘布局,x表示当前到达的点,ans表示已翻的棋子数,Vis用来记录翻过的棋子,便于输出。然后用bfs搜索。
#include <stdio.h>
#include <string.h>
int map[20];
struct node
{
    int a[20];
    int x;
    int ans;
    int vis[20];
} q[100000];
void bfs()
{
    node f1, f2;
    int s=0, e=0, i, j;
    for(i=0; i<16; i++)
    {
        f1.a[i]=map[i];
        f1.vis[i]=0;
    }
    f1.x=0;
    f1.ans=0;
    q[s++]=f1;
    while(s>e)
    {
        f1=q[e++];
        /*printf("%d\n",e);
        printf("%d\n",f1.ans);
        for(i=0; i<16; i++)
            printf("%d ",f1.vis[i]);
        printf("\n");*/
        int ss=0;
        for(i=0; i<16; i++)
        {
            ss+=f1.a[i];
        }
        if(ss==0)//判断是否全白或全黑
        {
            printf("%d\n",f1.ans);
            for(i=0;i<16;i++)
            {
                if(f1.vis[i])
                {
                    printf("%d %d\n",i/4+1,i%4+1);
                }
            }
            return ;
        }
        for(i=0; i<16; i++)
        {
            if(f1.x<=i)
            {
                for(j=0; j<16; j++)
                {
                    f2.a[j]=f1.a[j];
                    f2.vis[j]=f1.vis[j];
                }
                for(j=i-i%4; j<i+4-i%4; j++)
                    f2.a[j]=1-f1.a[j];
                for(j=i%4; j<16; j+=4)
                    f2.a[j]=1-f1.a[j];
                f2.a[i]=1-f1.a[i];//反转当前棋子
                f2.ans=f1.ans+1;
                f2.x=i+1;
                f2.vis[i]=1;
                q[s++]=f2;
            }
        }
    }
    printf("Impossible\n");
    return ;
}
int main()
{
    int i, j;
    char s[20];
    for(i=0; i<4; i++)
    {
        scanf("%s",s);
        for(j=0; j<4; j++)
        {
            if(s[j]=='+')
                map[4*i+j]=1;
            else
                map[4*i+j]=0;
        }
    }
    bfs();
    return 0;
}


你可能感兴趣的:(编程,C语言,poj,bfs)