为什么这个程序本地运行没有问题,在线ide运行错
  • 板块灌水区
  • 楼主AcGreen
  • 当前回复4
  • 已保存回复4
  • 发布时间2022/5/28 21:53
  • 上次更新2023/10/28 00:25:05
查看原帖
为什么这个程序本地运行没有问题,在线ide运行错
421986
AcGreen楼主2022/5/28 21:53

rt

#include <bits/stdc++.h>
using namespace std;
struct Chess
{
    int brd[4][4];
    bool operator < (Chess others) const
    {
        for (int i = 0; i < 4; ++i)
            for (int j = 0; j < 4; ++j)
                if (this->brd[i][j]!=others.brd[i][j]) return this->brd[i][j]<others.brd[i][j];
        return false;
    }
    bool operator == (Chess others) const
    {
        for (int i = 0; i < 4; ++i)
            for (int j = 0; j < 4; ++j)
                if (this->brd[i][j]!=others.brd[i][j]) return false;
        return true;
    }
};
pair<int,int> finb(Chess x)
{
    for (int i = 0; i < 4; ++i)
        for (int j = 0; j < 4; ++j)
            if (x.brd[i][j]==0) return make_pair(i,j);
    return make_pair(0,0);
}
map<Chess,int> mpb,mpe;
Chess beger,ender;
int toward[2][2]={{-1,1},{-1,1}};
bool c_mve(Chess ths,int ud,int lr)
{
    pair<int,int> ths_m=finb(ths);
    return ths_m.first+ud>=0&&
           ths_m.first+ud<4&&
           ths_m.second+ud>=0&&
           ths_m.second+ud<4;
}
int main() {
    char ed[4][4]={
            {1,2,3,4},
            {5,6,7,8},
            {9,10,11,12},
            {13,14,15,0}
    };
    for (int i = 0; i < 4; ++i)
        for (int j = 0; j < 4; ++j)
            ender.brd[i][j]=ed[i][j];
    for (int i = 0; i < 4; ++i)
        for (int j = 0; j < 4; ++j)
            cin>>beger.brd[i][j];
    queue<Chess> Qb,Qe;
    Qb.push(beger);
    Qe.push(ender);
    mpb[beger]=0;
    mpe[ender]=0;
    while (!Qb.empty()&&!Qe.empty()){
        pair<int,int> posb= finb(Qb.front());
        pair<int,int> pose= finb(Qe.front());
        for (int i = -1; i <=1 ; i+=2) {
            Chess the;
            the=Qe.front();
            if(c_mve(the,0,i))
            {
                swap(the.brd[pose.first][pose.second],the.brd[pose.first][pose.second+i]);
                if(!mpe.count(the))
                {
                    Qe.push(the);
                    mpe[the]=mpe[Qe.front()]+1;
                }
                if (mpb.count(the))
                {
                    cout<<mpb[the]+mpe[the]<<endl;
                    return 0;
                }

            }
        }
        for (int i = -1; i <=1 ; i+=2) {
            Chess the;
            the=Qe.front();
            if(c_mve(the,i,0))
            {
                swap(the.brd[pose.first][pose.second],the.brd[pose.first+i][pose.second]);
                if(!mpe.count(the))
                {
                    Qe.push(the);
                    mpe[the]=mpe[Qe.front()]+1;
                }
                if (mpb.count(the))
                {
                    cout<<mpb[the]+mpe[the]<<endl;
                    return 0;
                }

            }
        }
        for (int i = -1; i <=1 ; i+=2) {
            Chess thb;
            thb=Qb.front();
            if(c_mve(thb,0,i))
            {
                swap(thb.brd[posb.first][posb.second],thb.brd[posb.first][posb.second+i]);
                if(!mpb.count(thb))
                {
                    Qb.push(thb);
                    mpb[thb]=mpb[Qb.front()]+1;
                }
                if (mpe.count(thb))
                {
                    cout<<mpb[thb]+mpe[thb]<<endl;
                    return 0;
                }

            }
        }
        for (int i = -1; i <=1 ; i+=2) {
            Chess thb;
            thb=Qb.front();
            if(c_mve(thb,0,i))
            {
                swap(thb.brd[posb.first][posb.second],thb.brd[posb.first+i][posb.second]);
                if(!mpb.count(thb))
                {
                    Qb.push(thb);
                    mpb[thb]=mpb[Qb.front()]+1;
                }
                if (mpe.count(thb))
                {
                    cout<<mpb[thb]+mpe[thb]<<endl;
                    return 0;
                }

            }
        }
        Qb.pop();
        Qe.pop();
    }
    return 0;
}

附图

2022/5/28 21:53
加载中...