BFS题目。。。明明不是很难但是就是要写很长时间。。。。
使用了message结构来保存需要的数据(当前图的是怎么样的,上次移动的元素是什么),使用map来判重和记录到达某个message需要多少步。然后为了节约空间,利用3进制数对message进行压缩以保存。
/*
1004 四子连棋
时间限制: 1 s
空间限制: 128000 KB
题目等级 : 黄金 Gold
题解
题目描述 Description
在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑白双方交替走棋,任意一方可以先走,如果某个时刻使得任意一种颜色的棋子形成四个一线(包括斜线),这样的状态为目标棋局。
● ○ ●
○ ● ○ ●
● ○ ● ○
○ ● ○
输入描述 Input Description
从文件中读入一个4*4的初始棋局,黑棋子用B表示,白棋子用W表示,空格地带用O表示。
输出描述 Output Description
用最少的步数移动到目标棋局的步数。
样例输入 Sample Input
BWBO
WBWB
BWBW
WBWO
样例输出 Sample Output
5
数据范围及提示 Data Size & Hint
hi
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
//B == 1 W == 2 O == 0相当于三进制
struct message{
char b[4][4];
char buf;//用于保存上一次移动的棋子'O'为第一步,没有上一次的棋子
int m_to_i(){
int ret = 0, i, j;
for (i = 0; i < 4; i++){
for (j = 0; j < 4; j++){
ret *= 3;
switch (b[i][j]){
case'B':ret += 1; break;
case'W':ret += 2; break;
case'O':ret += 0; break;
default:throw("WHAT 1");
}
}
}
ret *= 3;
switch (buf){
case'B':ret += 1; break;
case'W':ret += 2; break;
case'O':ret += 0; break;
default:throw("WHAT 1");
}
return ret;
}
void i_to_m(int input){
int i, j;
switch (input % 3){
case 1:buf = 'B'; break;
case 2:buf = 'W'; break;
case 0:buf = 'O'; break;
default:throw("WHAT 2");
}
input /= 3;
for (i = 3; i >= 0; i--){
for (j = 3; j >= 0; j--){
switch (input % 3){
case 1:b[i][j] = 'B'; break;
case 2:b[i][j] = 'W'; break;
case 0:b[i][j] = 'O'; break;
default:throw("WHAT 2");
}
input /= 3;
}
}
}
};
message msg;
const int dx[] = { 1, 0, 0, -1 };
const int dy[] = { 0, 1, -1, 0 };
bool success(int x, int y){
char buf = msg.b[x][y];
int i;
for (i = 0; i < 4 && msg.b[x][i] == buf; i++);
if (i == 4)
return true;
for (i = 0; i < 4 && msg.b[i][y] == buf; i++);
if (i == 4)
return true;
if (x == y){
for (i = 0; i < 4 && msg.b[i][i] == buf; i++);
if (i == 4)
return true;
}
if (x + y == 3){
for (i = 0; i < 4 && msg.b[i][3 - i] == buf; i++);
if (i == 4)
return true;
}
return false;
}
bool check(int x, int y,message &msg){
if (x < 0 || x > 3)return false;
if (y < 0 || y > 3)return false;
if (msg.b[x][y] == '0')return false;
if (msg.b[x][y] == msg.buf)return false;//与上次移动的棋子相同
return true;
}
int BFS_solve(int x,int y,queue<int>&que,map<int,int> &maze){
int i, x2, y2, tmp, base = maze[msg.m_to_i()];
char status;
for (i = 0; i < 4; i++){
x2 = x + dx[i];
y2 = y + dy[i];
if (check(x2,y2, msg)){
status = msg.buf;
msg.buf = msg.b[x2][y2];
msg.b[x][y] = msg.b[x2][y2];
msg.b[x2][y2] = 'O';
tmp = msg.m_to_i();
if (maze[tmp] == 0){
if (success(x, y))
return base + 1;
maze[tmp] = base + 1;
que.push(tmp);
}
msg.b[x2][y2] = msg.b[x][y];
msg.b[x][y] = 'O';
msg.buf = status;
}
}
return -1;
}
int BFS(){
queue<int> que;
map<int, int> maze;
int i, j;
que.push(msg.m_to_i());
maze[msg.m_to_i()] = 0;//作为标记,深度为0
while (!que.empty()){
msg.i_to_m(que.front());
que.pop();
for (i = 0; i < 4; i++){
for (j = 0; j < 4; j++){
if (msg.b[i][j] == 'O'){
int ret = BFS_solve(i,j,que,maze);
if (ret >= 0)return ret;
}
}
}
}
throw("WTF3");
return 0;
}
int main(){
int i, j;
msg.buf = 'O';
for (i = 0; i < 4; i++){
for (j = 0; j < 4; j++){
cin >> msg.b[i][j];
}
}
int p = msg.m_to_i();
msg.i_to_m(p);
cout << BFS() << endl;
return 0;
}