当前位置: 代码迷 >> 综合 >> POJ 1753 位运算 + BFS
  详细解决方案

POJ 1753 位运算 + BFS

热度:52   发布时间:2024-01-13 18:20:12.0

一道简单的BFS。由于只有16个点,所以只有2^16= 65536个状态。 当最终状态等于0或者65535时,即找到结果。




/*
ID: sdj22251
PROG: lamps
LANG: C++
*/
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#define MAX 2000000000
#define LOCA
using namespace std;
char s[6][6];
bool v[70000];
int turn[16] = {19, 39, 78, 140, 305, 626, 1252, 2248, 4880, 10016, 20032, 35968, 12544, 29184, 58368, 51200};
struct wwj
{int tp;int num;wwj(){}wwj(int a, int b){tp = a;num = b;}
}q[77777];
int main()
{
#ifdef LOCALfreopen("lamps.in","r",stdin);freopen("lamps.out","w",stdout);
#endifint i, j, tp = 0, ans = -1;for(i = 0; i < 4; i++)scanf("%s", s[i]);for(i = 0; i < 4; i++){for(j = 0; j < 4; j++){if(s[i][j] == 'w')tp |= (1 << (i * 4 + j));}}int tail = 0, head = 0;wwj A(tp, 0);q[tail++] = A;v[A.tp] = true;while(head < tail){wwj tmp = q[head++];if(tmp.tp == 65535 || tmp.tp == 0){ans = tmp.num;break;}for(i = 0; i < 16; i++){wwj B(tmp.tp ^ turn[i], tmp.num + 1);if(v[B.tp] == false){q[tail++] = B;v[B.tp] = true;}}}if(ans >= 0)printf("%d\n", ans);else printf("Impossible\n");return 0;
}