您的位置:首页 > 其它

[USACO] The Clocks [位操作 BFS]

2012-08-06 12:37 351 查看
思路:

位操作+广搜。

1到9个时钟,每个时钟状态用两位表示 ,共18位。即每个状态用一个int表示即可。

然后直接广搜,每个状态都是一个int型,每次某状态到达用一个hash表记录。

一次AC。

位操作写得很蛋疼,事后查了一下网上报告,好像没这么做的,就记录一下把。

/*
ID:wuyanyi1
PROG:clocks
LANG:C++
*/

#include<iostream>
#include<fstream>
#include<map>
#include<vector>
#include<string>
#include<memory.h>
#include<algorithm>
#include<queue>
#define Min(a,b) (a<b?a:b)
#define Max(a,b) (a>b?a:b)
#define llong long long int
using namespace std;
const int N=10,inf=(1<<30);
const int M=(1<<18)-1;
int n;
int mp

={{},
{4,1,2,4,5},{3,1,2,3},{4,2,3,5,6},{3,1,4,7},{5,2,4,5,6,8},
{3,3,6,9},{4,4,5,7,8},{3,7,8,9},{4,5,6,8,9}
};
int a
;
int vis[M+10];
int val[M+10];
int chbit(int state,int pos,int bit)
{
if(bit)
{
unsigned int now=1;
now<<=pos;
state|=now;
}
else
{
unsigned int now=M-1;
now=(now<<pos)|(now>>(18-pos));
state&=now;
}
return state&M;
}
int oper(int state,int k)
{
for(int i=1;i<=mp[k][0];i++)
{
int pos=(9-mp[k][i])*2;
int now=(state>>pos)&1;
//	printf("%d ",now);
now+=((state>>(pos+1))&1)*2;
//	printf("%d %d\n",now,pos);
if(now==3)
{
state=chbit(state,pos,0);
state=chbit(state,pos+1,0);
}
else
{
now++;
state=chbit(state,pos,now&1);
state=chbit(state,pos+1,(now>>1)&1);
}
}
return state&M;
}
void bfs(int s)
{
queue<int> que;
que.push(s);
vis[s]=-1;
while(!que.empty())
{
int now=que.front();
que.pop();
for(int i=1;i<=9;i++)
{
int t=oper(now,i);
if(!vis[t])
{
que.push(t);
vis[t]=now;
val[t]=i;
if(!t)
{
return;
}
}
}
}
}
void print(int i)
{
if(vis[i]!=-1)
{
print(vis[i]);
printf("%d",val[i]);
if(i)
printf(" ");
else
printf("\n");
}
/*	else
{
printf("",val[i]);
}*/
}
int main()
{
int s=0;
freopen("clocks.in","r",stdin);
freopen("clocks.out","w",stdout);
for(int i=1;i<=9;i++)
{
scanf("%d",a+i);
a[i]/=3;
a[i]%=4;
//		printf("%d",a[i]);
s<<=2;
s|=a[i];

}

bfs(s);

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