【搜索】poj2965

The Pilots Brothers' refrigerator

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
  
这算是个状压搜索吧……
   
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const long x[4][4] = {{0, 1, 2, 3},
                      {4, 5, 6, 7},
                      {8, 9, 10, 11},
                      {12, 13, 14, 15}};
long s = 0,t = 0,l = 1,r = 1;
long pos[70000][2];
long qu[70000];
bool b[70000];
long deng[70000];
long last[70000];
long change(long m, long i, long j)
{
    for(long k = 0; k != 4; k++)
    {
        m = m ^ (1 << x[k][j]);
        m = m ^ (1 << x[i][k]);
    }
    m = m ^ (1 << x[i][j]);
    return m;
}
int bfs(long l, long &r)
{
    qu[r++] = s;
    last[r - 1] = 0;
    deng[r - 1] = 0;
    pos[r - 1][0] = -1;
    pos[r - 1][1] = -1;
    b[s] = true;
    while (l < r)
    {
        long now = qu[l++];
        long num = l - 1;
        long d = deng[num];
        for(long i = 0; i != 4; i++)
        for(long j = 0; j != 4; j++)
        {
            long temp = change(now, i, j);

            if (!b[temp])
            {
                qu[r++] = temp;
                last[r - 1] = num;
                deng[r - 1] = d + 1;
                pos[r - 1][0] = i;
                pos[r - 1][1] = j;
                b[temp] = true;
                //cout<<i<<" "<<j<<" "<<temp<<" "<<pos[r-1][0]<<" "<<pos[r-1][1]<<endl;
                if(temp == 0){ return d + 1;}
            }
        }
    }


}
void print_solution(long r)
{
    long t = r-1;
    while (last[t] != 0)
    {
        cout << pos[t][0] + 1 << " " << pos[t][1] + 1 << endl;
        t = last[t];
    }
}
int main()
{
    freopen("L.in", "r", stdin);
    for (long i = 0; i != 4; i++)
        for (long j = 0; j != 4; j++)
        {
            char c;
            cin >> c;
            if (c == '+')
            {
                s = s | ( 1 << x[i][j]);
            }
        }
    cout << bfs(l, r) << endl;
    print_solution(r);
    return 0;
}


全部评论

相关推荐

头顶尖尖的程序员:我是26届的不太懂,25届不应该是找的正式工作吗?为什么还在找实习?大四还实习的话是为了能转正的的岗位吗
点赞 评论 收藏
分享
码农索隆:想看offer细节
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务