洛谷 2346 四子连棋 bfs

题目:
https://www.luogu.org/problem/show?pid=2346

40分钟打出代码(80分)+5个小时调试=AC

水题,没有任何技巧的bfs,注意状态的储存;

目标状态: 横竖斜4个同色;

but! 由于忘了判竖着的情况,所以一直80,调了5、6个小时?;
洛谷不让下数据……所以只能手算……然后和电脑比较答案……;
不过这个题用来练bfs挺好的

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int mod=131373,base=137;
bool vis[2000005];
struct hh 
{
    int a[8][8],x1,y1,x2,y2,step,up;
}star;
string s[8];//1 hei ,2 bai;
queue<hh>q;
int ans,X[6]={0,1,0,-1,0},Y[6]={0,0,-1,0,1},fx,fy;
int hashs(hh now)
{
    int tot=0;
    for(int i=1;i<=4;i++)
        for(int j=1;j<=4;j++)
            tot=(base*tot+(int)now.a[i][j])%mod;
    return tot;
}
bool pd(hh now)
{
    if(now.a[2][2]==now.a[1][1] && now.a[3][3]==now.a[1][1] && now.a[4][4]==now.a[1][1]) return true;
    for(int i=1;i<=4;i++)
    {
        if(now.a[i][1]==now.a[i][2] && now.a[i][1]==now.a[i][3] && now.a[i][1]==now.a[i][4]) return true;
        if(now.a[1][i]==now.a[2][i] && now.a[1][i]==now.a[3][i] && now.a[1][i]==now.a[4][i]) return true;
    }
    return false;
}
int bfs()
{
    memset(vis,0,sizeof(vis));
    while(!q.empty()) q.pop();
    q.push(star),vis[hashs(star)]=1;
    while(!q.empty())
    {
        hh now=q.front(),nxt;
        q.pop();
        if(pd(now)) return now.step;
        else
        {
            for(int i=1;i<=8;i++)
            {
                if(i<=4) fx=now.x1+X[i],fy=now.y1+Y[i];
                else fx=now.x2+X[i-4],fy=now.y2+Y[i-4];
                nxt=now;
                if(now.a[fx][fy]!=now.up || fx<1 || fx>4 || fy<1 || fy>4) continue;
                if(i<=4) swap(nxt.a[fx][fy],nxt.a[now.x1][now.y1]);
                else swap(nxt.a[fx][fy],nxt.a[now.x2][now.y2]);
                if(!vis[hashs(nxt)])
                {
                    if(i<=4) nxt.x1=fx,nxt.y1=fy;
                    else nxt.x2=fx,nxt.y2=fy;
                    vis[(hashs(nxt))]=1;
                    if(now.up & 1) nxt.up=2;
                    else nxt.up=1;
                    nxt.step=now.step+1;
                    q.push(nxt);
                }
            }
        }
    }
}
void solve()
{
    for(int i=1;i<=4;i++)
    {
        cin>>s[i];
        for(int j=0;j<4;j++)
            if(s[i][j]=='B') star.a[i][j+1]=1;
            else if(s[i][j]=='W') star.a[i][j+1]=2;
            else if(star.x1) star.x2=i,star.y2=j+1;
            else star.x1=i,star.y1=j+1;
    }
    star.up=1,ans=bfs(),star.up=2;
    cout<<min(bfs(),ans);
    return;
}
int main()
{
    solve();
    return 0;
}

2017.10.26 感谢Sher_lock指出BUG;
修改后的

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int mod=131373,base=137;
bool vis[2000005];
struct hh 
{
    int a[8][8],x1,y1,x2,y2,step,up;
}star;
string s[8];//1 hei ,2 bai;
queue<hh>q;
int ans,X[6]={0,1,0,-1,0},Y[6]={0,0,-1,0,1},fx,fy;
int hashs(hh now)
{
    int tot=0;
    for(int i=1;i<=4;i++)
        for(int j=1;j<=4;j++)
            tot=(base*tot+(int)now.a[i][j])%mod;
    return tot;
}
bool pd(hh now)
{
    if(now.a[2][2]==now.a[1][1] && now.a[3][3]==now.a[1][1] && now.a[4][4]==now.a[1][1]) return true;
    if(now.a[1][4]==now.a[2][3] && now.a[3][2]==now.a[1][4] && now.a[4][1]==now.a[1][4]) return true;
    for(int i=1;i<=4;i++)
    {
        if(now.a[i][1]==now.a[i][2] && now.a[i][1]==now.a[i][3] && now.a[i][1]==now.a[i][4]) return true;
        if(now.a[1][i]==now.a[2][i] && now.a[1][i]==now.a[3][i] && now.a[1][i]==now.a[4][i]) return true;
    }
    return false;
}
int bfs()
{
    memset(vis,0,sizeof(vis));
    while(!q.empty()) q.pop();
    q.push(star),vis[hashs(star)]=1;
    while(!q.empty())
    {
        hh now=q.front(),nxt;
        q.pop();
        if(pd(now)) return now.step;
        else
        {
            for(int i=1;i<=8;i++)
            {
                if(i<=4) fx=now.x1+X[i],fy=now.y1+Y[i];
                else fx=now.x2+X[i-4],fy=now.y2+Y[i-4];
                nxt=now;
                if(now.a[fx][fy]!=now.up || fx<1 || fx>4 || fy<1 || fy>4) continue;
                if(i<=4) swap(nxt.a[fx][fy],nxt.a[now.x1][now.y1]);
                else swap(nxt.a[fx][fy],nxt.a[now.x2][now.y2]);
                if(!vis[hashs(nxt)])
                {
                    if(i<=4) nxt.x1=fx,nxt.y1=fy;
                    else nxt.x2=fx,nxt.y2=fy;
                    vis[(hashs(nxt))]=1;
                    if(now.up & 1) nxt.up=2;
                    else nxt.up=1;
                    nxt.step=now.step+1;
                    q.push(nxt);
                }
            }
        }
    }
}
void solve()
{
    for(int i=1;i<=4;i++)
    {
        cin>>s[i];
        for(int j=0;j<4;j++)
            if(s[i][j]=='B') star.a[i][j+1]=1;
            else if(s[i][j]=='W') star.a[i][j+1]=2;
            else if(star.x1) star.x2=i,star.y2=j+1;
            else star.x1=i,star.y1=j+1;
    }
    star.up=1,ans=bfs(),star.up=2;
    cout<<min(bfs(),ans);
    return;
}
int main()
{
    solve();
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值