没什么思维难度的搜索,但比较锻炼思路的清晰程度
外加:
1、x & (1 << j),x第j位为1,得到的是(1 << j)
2、’||’、’&&’在使用时,对下面的影响一定要想清楚
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN = 10 + 5;
struct zt{
bool first;
int map[4];
int x1,y1,x2,y2;
int step;
}chu;
queue <zt> q;
string s;
bool check(zt u){
for(int i = 0;i < 4;i ++){
if(i == u.x1 || i == u.x2)continue;
bool flag = true;
for(int j = 0;j < 4;j ++){
if((bool)(u.map[i] & (1 << j)) != (bool)(u.map[i] & 1)){
flag = false;
break;
}
}
if(flag)return true;
}
for(int j = 0;j < 4;j ++){
if(j == u.y1 || j == u.y2)continue;
bool flag = true;
for(int i = 0;i < 4;i ++){
if((bool)(u.map[i] & (1 << j)) != (bool)(u.map[0] & (1 << j))){
flag = false;
break;
}
}
if(flag)return true;
}
//if(u.x1 == u.y1 || u.x1 + u.y1 == 3)return false;
//if(u.x2 == u.y2 || u.x2 + u.y2 == 3)return false;
//一开始这里没想清楚,左对角线上有空格也直接把右对角线过掉了,wa了四个点
if(u.x1 == u.y1 || u.x2 == u.y2)return false;
/*if(u.step == 4){
printf("haha");
printf("");
}*/
int ch = (bool)(u.map[0] & 1);
bool flag = true;
for(int i = 1;i < 4;i ++){
if((bool)(u.map[i] & (1 << i)) != ch){
flag = false;
break;
}
}
if(flag)return true;
if(u.x1 + u.y1 == 3 || u.x2 + u.y2 == 3)return false;
ch = (bool)(u.map[3] & 1);
flag = true;
for(int i = 2;i >= 0;i --){
if((bool)(u.map[i] & (1 << (3 - i))) != ch){
flag = false;
break;
}
}
if(flag)return true;
return false;
}
int get(zt u,int x,int y){
return (bool)(u.map[x] & (1 << y));
}
int mu[] = {0,1,-1,0},
mr[] = {1,0,0,-1};
bool used[16][16][16][16][5][5][5][5][2];
bool can(int x,int y){
if(x < 0 || y < 0)return false;
if(x > 3 || y > 3)return false;
return true;
}
bool in(zt u){
return used[u.map[0]][u.map[1]][u.map[2]][u.map[3]][u.x1][u.y1][u.x2][u.y2][u.first];
}
void add(zt u){
used[u.map[0]][u.map[1]][u.map[2]][u.map[3]][u.x1][u.y1][u.x2][u.y2][u.first] = true;
}
int ans,x2,y2,x1,y1;
int main(){
chu.x1 = -1;
for(int i = 0;i < 4;i ++){
cin >> s;
for(int j = 0;j < 4;j ++){
if(s[j] == 'B'){
chu.map[i] += (1 << j);
}
if(s[j] == 'O' && chu.x1 == -1){
chu.x1 = i;
chu.y1 = j;
}
else if(s[j] == 'O'){
chu.x2 = i;
chu.y2 = j;
}
}
}
chu.first = 0;
q.push(chu);
chu.first = 1;
q.push(chu);
used[chu.map[0]][chu.map[1]][chu.map[2]][chu.map[3]][chu.x1][chu.y1][chu.x2][chu.y2][0] = true;
used[chu.map[0]][chu.map[1]][chu.map[2]][chu.map[3]][chu.x1][chu.y1][chu.x2][chu.y2][1] = true;
while(!q.empty()){
zt u = q.front();
q.pop();
// if(u.map[0] == 5)printf("haha");
for(int i = 0;i <= 3;i ++){
int xx = u.x1 + mu[i];
int yy = u.y1 + mr[i];
if(can(xx,yy) && ((bool)(u.map[xx] & (1 << yy)) == u.first) && (xx != u.x2 || yy != u.y2)){
zt tmp = u;
int tmp1 = get(tmp,xx,yy);
tmp.map[xx] -= (tmp1 << yy);
tmp.map[u.x1] += (tmp1 << u.y1);
tmp.x1 = xx;
tmp.y1 = yy;
tmp.first ^= 1;
if(!in(tmp)){
add(tmp);
tmp.step ++;
if(check(tmp)){/*
for(int i = 0;i < 4;i ++){
printf("%d\n",tmp.map[i]);
}
check(tmp);*/
printf("%d",tmp.step);
return 0;
}
q.push(tmp);
}
}
xx = u.x2 + mu[i];
yy = u.y2 + mr[i];
if(can(xx,yy) && ((bool)(u.map[xx] & (1 << yy)) == u.first) && (xx != u.x1 || yy != u.y1)){
zt tmp = u;
int tmp1 = get(tmp,xx,yy);
tmp.map[xx] -= (tmp1 << yy);
tmp.map[u.x2] += (tmp1 << u.y2);
tmp.x2 = xx;
tmp.y2 = yy;
tmp.first ^= 1;
if(!in(tmp)){
add(tmp);
tmp.step ++;
if(check(tmp)){
//check(tmp);
printf("%d",tmp.step);
return 0;
}
q.push(tmp);
}
}
}
}
}