POJ 1753 棋盘反转(位运算+BFS)
2013-09-30 03:16
344 查看
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
#define ROW 1
#define COL 2
#define MAX 6
#define ENDSTATE ((1<<16)-1)
const int dir[5][3] = { {0,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1} };
int arr[MAX][MAX];
int chess[MAX][MAX];
int change[17];
bool visit[(1 << 16)];
int initVal = 0;
struct Node{
Node():state( 0 ),step( 0 ){};
Node(int sa,int se):state( sa ),step( se ){};
int state;
int step;
};
void getChange(){
int index = 1;
memset( arr, 0, sizeof( arr ) );
for( int i = 1;i <= 4; ++i ){
for( int j = 1;j <= 4; ++j ){
int temp = 0;
for( int k = 1;k <= 4; ++k ){
arr[i + dir[k][ROW]][j + dir[k][COL]] = 1;
}
arr[i][j] = 1;
for( int m = 1;m <= 4; ++m ){
for( int n = 1;n <= 4; ++n ){
temp <<= 1;
if( arr[m]
==1 )
temp += 1;
}
}
change[index] = temp;
index++;
memset( arr, 0, sizeof( arr ) );
}
}
}
int BFS(){
queue<Node>Q;
Node cur( initVal, 0 );
visit[initVal] = true;
Q.push( cur );
while( !Q.empty() ){
Node temp = Q.front();
Q.pop();
if( temp.state == 0 || temp.state == ENDSTATE )
return temp.step;
for( int i = 1; i <= 16; ++i ){
Node nextNode;
int val = temp.state ^ change[i];
if( visit[val] == true )
continue;
nextNode.state = val;
nextNode.step = temp.step + 1;
visit[val] =true;
Q.push( nextNode );
}
}
return -1;
}
int main(){
getChange();
memset( visit, false, sizeof( visit ) );
bool label = false;
int base;
for( int i = 1; i <= 4; ++i ){
for( int j = 1; j <= 4; ++j ){
initVal <<= 1;
char c;
cin >> c;
if( c == 'b' )
initVal += 1;
}
}
int step = BFS();
if( step == -1 )
cout << "Impossible" << endl;
else
cout << step << endl;
return 0;
}
#include <cstring>
#include <queue>
using namespace std;
#define ROW 1
#define COL 2
#define MAX 6
#define ENDSTATE ((1<<16)-1)
const int dir[5][3] = { {0,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1} };
int arr[MAX][MAX];
int chess[MAX][MAX];
int change[17];
bool visit[(1 << 16)];
int initVal = 0;
struct Node{
Node():state( 0 ),step( 0 ){};
Node(int sa,int se):state( sa ),step( se ){};
int state;
int step;
};
void getChange(){
int index = 1;
memset( arr, 0, sizeof( arr ) );
for( int i = 1;i <= 4; ++i ){
for( int j = 1;j <= 4; ++j ){
int temp = 0;
for( int k = 1;k <= 4; ++k ){
arr[i + dir[k][ROW]][j + dir[k][COL]] = 1;
}
arr[i][j] = 1;
for( int m = 1;m <= 4; ++m ){
for( int n = 1;n <= 4; ++n ){
temp <<= 1;
if( arr[m]
==1 )
temp += 1;
}
}
change[index] = temp;
index++;
memset( arr, 0, sizeof( arr ) );
}
}
}
int BFS(){
queue<Node>Q;
Node cur( initVal, 0 );
visit[initVal] = true;
Q.push( cur );
while( !Q.empty() ){
Node temp = Q.front();
Q.pop();
if( temp.state == 0 || temp.state == ENDSTATE )
return temp.step;
for( int i = 1; i <= 16; ++i ){
Node nextNode;
int val = temp.state ^ change[i];
if( visit[val] == true )
continue;
nextNode.state = val;
nextNode.step = temp.step + 1;
visit[val] =true;
Q.push( nextNode );
}
}
return -1;
}
int main(){
getChange();
memset( visit, false, sizeof( visit ) );
bool label = false;
int base;
for( int i = 1; i <= 4; ++i ){
for( int j = 1; j <= 4; ++j ){
initVal <<= 1;
char c;
cin >> c;
if( c == 'b' )
initVal += 1;
}
}
int step = BFS();
if( step == -1 )
cout << "Impossible" << endl;
else
cout << step << endl;
return 0;
}
相关文章推荐
- POj 1753--Flip Game(位运算+BFS)
- POJ 1753 bfs+位运算
- POJ 1753 Flip Game(bfs+位压缩运算)
- POJ1753 BFS+位运算
- poj_1753 Flip Game(dfs+枚举)/(bfs+位运算)
- poj 1753(位运算+BFS)
- 位运算-BFS-Flip Game-POJ 1753
- POJ 1753 - Flip Game (位运算 + BFS)
- poj 1753 Flip Game(位运算+bfs)
- POJ 1753(DFS+枚举||BFS+位运算)
- poj 1321 棋盘问题 - DFS 2251 Dungeon Master - BFS
- POJ1753,Flip Game,枚举,bfs都可以
- POJ 1753 Flip Game BFS+位压缩状态
- POJ1753棋盘翻转 枚举+DFS
- POJ 1753 Flip Game(bfs+状态压缩+位运算)
- poj 1753 状态压缩+bfs
- POJ 1753 Flip Game ------- BFS & 位运算
- poj_2965 The Pilots Brothers' refrigerator(bfs+位运算)
- 【BFS|位运算】POJ-1753 Flip Game
- POJ基本算法bfs1753,2965