您的位置:首页 > 其它

蓝桥杯-历届试题-九宫重排-BFS+剪枝

2017-03-25 10:57 316 查看
题解:直接BFS,判重剪枝优化一下就行。

我看了网上一些题解,一些都是用字符串储存然后转化为整数判重。然后我觉得还是直接用字符串判重简单些,每想到还真过了。就是数据很极限,都900多ms,差点就超时了。

基本没什么说的,就BFS,然后map判重剪一下枝就过了。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <map>
using namespace std;

const int INF = 0x3f3f3f3f;
int xx[4]={0,0,1,-1};
int yy[4]={1,-1,0,0};
map<string,int>mp;

struct node{
char sz[3][3];
int step,x,y;
};

int main()
{
string start,finally,tmp;
node n,x;
int ans=INF,num=0;
cin>>start>>finally;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
n.sz[i][j] = start[num++];
if(n.sz[i][j]=='.')
{
n.x = i;
n.y = j;
}
}
n.step=0;
queue<node>q;
q.push(n);
mp[start]++;
while(!q.empty())
{
n=q.front(),q.pop();
for(int i=0;i<4;i++)
{
x = n;
x.x = n.x+xx[i];
x.y = n.y+yy[i];
if(x.x<0 || x.x>2 || x.y<0 || x.y>2) continue;
x.step = n.step+1;
x.sz[n.x][n.y] = x.sz[x.x][x.y];
x.sz[x.x][x.y] = '.';
tmp = "";
for(int j=0;j<3;j++)
{
for(int k=0;k<3;k++)
{
tmp += x.sz[j][k];
}
}
if(tmp==finally)
{
if(x.step<ans) ans=x.step;
continue;
}
if(!mp[tmp])
{
mp[tmp]++;
q.push(x);
}
}
}
if(ans==INF) printf("-1\n");
else printf("%d\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: