poj 1753 flip[ 枚举 ]
2015-02-09 14:45
246 查看
传送门:http://poj.org/problem?id=1753
思路:16格用16位的int表示,然后用bfs的层次关系枚举;
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long LL;
const int N=100007;
int vis[65536],ok;
int step
;
int flip(int state,int i,int j)
{
state=state^(1<<(4*i+j));
if(i-1>=0) state=state^(1<<((i-1)*4+j));
if(i+1<4) state=state^(1<<((i+1)*4+j));
if(j-1>=0) state=state^(1<<(i*4+j-1));
if(j+1<4) state=state^(1<<(i*4+j+1));
return state;
}
void bfs(int cs)
{
queue<int> que;
que.push(cs);
while(!que.empty())
{
int cur=que.front();
que.pop();
if(cur==0||cur==65535)
{
ok=1;
printf("%d\n",step[cur]);
break;
}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
int tmp=flip(cur,i,j);
if(!vis[tmp])
{
vis[tmp]=1;
que.push(tmp);
step[tmp]=step[cur]+1;
}
}
}
}
}
int main()
{
mem(vis,0);
int state=0;
for(int i=0;i<4;i++)
{
char str[5];
scanf("%s",str);
for(int j=0;j<4;j++)
{
if(str[j]=='b')
{
state=state|(1<<((4*i)+j));
}
}
}
vis[state]=1;
ok=0;
bfs(state);
if(!ok)
{
printf("Impossible\n");
}
return 0;
}
思路:16格用16位的int表示,然后用bfs的层次关系枚举;
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long LL;
const int N=100007;
int vis[65536],ok;
int step
;
int flip(int state,int i,int j)
{
state=state^(1<<(4*i+j));
if(i-1>=0) state=state^(1<<((i-1)*4+j));
if(i+1<4) state=state^(1<<((i+1)*4+j));
if(j-1>=0) state=state^(1<<(i*4+j-1));
if(j+1<4) state=state^(1<<(i*4+j+1));
return state;
}
void bfs(int cs)
{
queue<int> que;
que.push(cs);
while(!que.empty())
{
int cur=que.front();
que.pop();
if(cur==0||cur==65535)
{
ok=1;
printf("%d\n",step[cur]);
break;
}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
int tmp=flip(cur,i,j);
if(!vis[tmp])
{
vis[tmp]=1;
que.push(tmp);
step[tmp]=step[cur]+1;
}
}
}
}
}
int main()
{
mem(vis,0);
int state=0;
for(int i=0;i<4;i++)
{
char str[5];
scanf("%s",str);
for(int j=0;j<4;j++)
{
if(str[j]=='b')
{
state=state|(1<<((4*i)+j));
}
}
}
vis[state]=1;
ok=0;
bfs(state);
if(!ok)
{
printf("Impossible\n");
}
return 0;
}
相关文章推荐
- POJ 1753 Flip Game 暴力搜索(dfs加枚举)
- (枚举初级+回溯)poj1753 Flip Game
- POJ-1753 Flip Game【暴力枚举】
- 1753 poj Flip Game 据说是枚举,我这种DFS算是枚举不?
- poj 1753 Flip Game——DFS(分类是枚举)
- POJ1753 Flip Game(搜索,枚举,位运算压缩)
- 文章标题 POJ 1753 : Flip Game(枚举+dfs)
- Fliptile POJ - 3279 (状态压缩+枚举) && Flip Game POJ - 1753(状态压缩+bfs暴力)
- poj 1753 : Flip Game (枚举+dfs)
- poj 1753 : Flip Game (枚举+dfs)
- POJ 1753:Flip Game:棋盘枚举1
- Flip Game【POJ--1753】【枚举】【高斯消元】
- 【状态压缩枚举】poj 1753 Flip Game
- 枚举 POJ 1753 Flip Game
- POJ 1753 FLIP GAME——枚举深度的dfs
- POJ-1753:Flip Game
- POJ1753棋盘翻转 枚举+DFS
- POJ 1753 Flip Game (递归枚举)
- POJ 1753 (枚举+DFS)
- poj1753 Flip Game