您的位置:首页 > 其它

POJ 1753

2015-06-10 16:43 246 查看
#include <iostream>
#include <queue>
using namespace std;

char _m[4][4];
bool mark[100000];

struct node
{
int step;
int state;
};

bool BFS(node p);

bool boo;

node t;

void move(int r)//位操作的改变状态~
{
t.state^=(1<<(15-r));
if(r>=4)
t.state^=(1<<(15-r+4));
if(r<=11)
t.state^=(1<<(15-r-4));
if((r%4))
t.state^=(1<<(15-r+1));
if(((r+1)%4))
t.state^=(1<<(15-r-1));
}

queue<node> coll;

int main()
{
//freopen("acm.acm","r",stdin);
int i;
int j;
node begin;
begin.state = 0;
begin.step = 0;

for(i = 0; i < 4; ++ i)
{
for(j = 0; j < 4; ++ j)
{
cin>>_m[i][j];
if(_m[i][j] == 'b')
{
begin.state += 1;
}
begin.state <<= 1;
}

}
begin.state >>= 1;
memset(mark,false,sizeof(mark));
if(begin.state == 0 || begin.state == 65535)
{
cout<<"0"<<endl;
}
else
{
coll.push(begin);
mark[begin.state] = true;
boo = false;
while(!coll.empty() && !BFS(coll.front()))
{
coll.pop();
}
if(!boo)
{
cout<<"Impossible"<<endl;
}
}

}

bool BFS(node p)
{
//    cout<<p.state<<endl;
if(p.state == 0 || p.state == 65535)
{
boo = true;
cout<<p.step<<endl;
return true;
}
int i;

for(i = 0; i < 16; ++ i)
{

t = p;
move(i);
//    cout<<t.state<<endl;
if(!mark[t.state])
{
t.step = p.step + 1;
coll.push(t);
mark[t.state] = true;
}
}

return false;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: