A*相关的题目代码们

· · 个人记录

P1379 八数码难题

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
using namespace std;



int yuan[4][4]=
{   {0,0,0,0 },
    {0,1,2,3 },
    {0,8,0,4 },
    {0,7,6,5 },


};
int book[4][4];
int hopx[10]= {0,0,1,-1,0 };
int hopy[10]= {0,1,0,0,-1 };
int a[4][4];


//2019.4.13改*************************OP********************************************
struct state
{   int x1,y1,F_Tot,G_Past;
    int pan[4][4];
    //stack<int > ip;
    int chong;
};
struct cmp
{

    bool operator ()(const state &a, const state &b)
    {   return a.F_Tot>b.F_Tot;// 按照F_Tot从小到大排列
    }
};
int  H_exp( state x)
{   int sum=0;
    for(int i=1; i<=3; i++)
    {   for(int j=1; j<=3; j++)
        {
//cout<<x.pan[i][j]<<" ";
            if(x.pan[i][j]!=yuan[i][j])
            {   sum++;
            }

        }
//cout<<endl;
    }
//cout<<"     "<<sum<<"     "<<endl;
    return sum ;
}


void gs(int x,int y)

{   
    priority_queue<state,vector<state>,cmp> q;
    state first;
    for(int i=1; i<=3; i++)
    {   for(int j=1; j<=3; j++)
        {   first.pan[i][j]=a[i][j];
        }
    }
    first.G_Past=0;
    first.x1=x;
    first.y1=y;
    first.chong=0;
    first.F_Tot=first.G_Past+H_exp(first);
    q.push(first);
    while(!q.empty())
    {   state now=q.top();
        q.pop();

        if(H_exp(now)==0)
        {cout<<now.G_Past<<endl;



            return;
        }


        for(int i=1; i<=4; i++)
        {   if(now.x1+hopx[i]>=1&&now.x1+hopx[i]<=3&&
                    now.y1+hopy[i]>=1&&now.y1+hopy[i]<=3
              )
            {   state next=now;
                next.x1=now.x1+hopx[i];
                next.y1=now.y1+hopy[i];
                //
                int opp=now.pan[now.x1][now.y1];
                next.pan[next.x1][next.y1]=opp;
                next.pan[now.x1][now.y1]=now.pan[next.x1][next.y1];
                //
                next.G_Past=now.G_Past+1;
                next.F_Tot=next.G_Past+H_exp(next);
                next.chong=i;
                if(
                        next.x1>=1&&next.x1<=3&&next.y1>=1&&next.y1<=3
                        &&next.chong+now.chong!=5
                  )
                {
//cout<<next.x1<<"<<=x y=>>"<<next.y1<<endl;
//cout<<" next.F_Tot==>>"<<next.F_Tot<<"next.G_Past==>>"<<next.G_Past<<endl;
                    q.push(next);
//cout<<next.x1<<" "<<next.y1<<endl;
                    //ip.push(i);
                }
            }
        }
    }
    cout<<-1<<endl;
    return;
}

//2019.4.13改*************************ED********************************************
int main()
{
//freopen("now.out","w",stdout);
    int n;
    char w;
    int xx,yy;
        memset(a,0,sizeof(a));
        xx=0;
        yy=0;
        for(int i=1; i<=3; i++)
        {   for(int j=1; j<=3; j++)
            {   cin>>w;
                a[i][j]=w-'0';
                if(w=='0')
                {   a[i][j]=w-'0';
                    xx=i;
                    yy=j;
                }
            }
        }

        gs(xx,yy);
        memset(a,sizeof(a),0);





    return 0;
}

P2324 [SCOI2005]骑士精神

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
using namespace std;



int yuan[10][10]=
{
    {0,0,0,0,0,0 },
    {0,1,1,1,1,1 },
    {0,0,1,1,1,1 },
    {0,0,0,8,1,1 },
    {0,0,0,0,0,1 },
    {0,0,0,0,0,0 },
};
int book[10][10];
int hopx[10]= {0,-2,-2,-1,-1,1,1,2,2 };
int hopy[10]= {0,1,-1,2,-2,2,-2,1,-1 };
int a[10][10];


//2019.4.13改*************************OP********************************************
bool check()
{
    for(int x=1; x<=5; x++)
        {
            for(int y=1; y<=5; y++)
                {
                    if(a[x][y]!=yuan[x][y])
                        {
                            return false;
                        }
                }
        }
    return true;
}


struct van
{
    int x1,y1,tot,bu;
    int pan[6][6];
    //stack<int > ip;
    int opq;
};
struct cmp
{

    bool operator ()(const van &a, const van &b)
    {
        return a.tot>b.tot;// 按照tot从小到大排列
    }
};
int  pd( van x)
{
    int sum=0;
    for(int i=1; i<=5; i++)
        {
            for(int j=1; j<=5; j++)
                {

                    if(x.pan[i][j]!=yuan[i][j])
                        {
                            sum++;
                        }
                }
        }
    return sum ;
}


void gs(int x,int y)

{
    priority_queue<van,vector<van>,cmp> q;
    van first;
    for(int i=1; i<=5; i++)
        {
            for(int j=1; j<=5; j++)
                {
                    first.pan[i][j]=a[i][j];
                }
        }
    first.bu=0;
    first.x1=x;
    first.y1=y;
    first.opq=0;
    first.tot=first.bu+pd(first);
    q.push(first);
    while(!q.empty())
        {
            van now=q.top();
            q.pop();

            if(pd(now)==0)
                {
                    if(now.bu<=15)cout<<now.bu<<endl;
                    else
                        {
                            cout<<"-1"<<endl;
                        }
                    return;
                }
            for(int i=1; i<=8; i++)
                {
                    if(now.x1+hopx[i]>=1&&now.x1+hopx[i]<=5&&
                            now.y1+hopy[i]>=1&&now.y1+hopy[i]<=5
                      )
                        {
                            van next=now;
                            next.x1=now.x1+hopx[i];
                            next.y1=now.y1+hopy[i];
                            //
                            int opp=now.pan[now.x1][now.y1];
                            next.pan[next.x1][next.y1]=opp;
                            next.pan[now.x1][now.y1]=now.pan[next.x1][next.y1];
                            //
                            next.bu=now.bu+1;
                            next.tot=next.bu+pd(next);
                            next.opq=i;
                            if(next.tot<=19&&
                                    next.x1>=1&&next.x1<=5&&next.y1>=1&&next.y1<=5
                                    &&i+now.opq!=9
                              )
                                {
                                    q.push(next);
                                }
                        }
                }
        }
    cout<<-1<<endl;
    return;
}

//2019.4.13改*************************ED********************************************
int main()
{
    int n;
    char w;
    int xx,yy;
    cin>>n;
    for(int x=1; x<=n; x++)
        {
            memset(a,0,sizeof(a));
            xx=0;
            yy=0;
            for(int i=1; i<=5; i++)
                {
                    for(int j=1; j<=5; j++)
                        {
                            cin>>w;
                            if(w=='1')
                                {
                                    a[i][j]=1;
                                }
                            if(w=='0')
                                {
                                    a[i][j]=0;
                                }
                            if(w=='*')
                                {
                                    a[i][j]=8;
                                    xx=i;
                                    yy=j;
                                }
                        }
                }

            gs(xx,yy);
            memset(a,sizeof(a),0);
        }




    return 0;
}

P3116 [USACO15JAN]Meeting Time S约会时间

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
using namespace std;
int n,m;//n 点 m 边;
struct edge {
    int f;
    int t;
    int c;
};
vector<edge> bro[100+10];
vector<edge> sis[100+10];
vector<edge> antibro[100+10];
vector<edge> antisis[100+10];
int ansb[100000000];
int anss[100000000];
int bmin=0;
int smin=0;
/////////////////////////////////////////////////
int inqb[100+10];
int todb[100+10];
void antispfab(int x) {
    memset(todb,0x3f,sizeof(todb));
    queue<int> q;
    q.push(x);
    todb[x]=0;
    inqb[x]=true;
    while(!q.empty()) {
        int now=q.front();
        q.pop();
        inqb[now]=false;
        int lb=antibro[now].size();
        for(int i=0; i<lb; i++) {
            edge next=antibro[now][i];
            if(todb[next.t]>todb[next.f]+next.c) {
                todb[next.t]=todb[next.f]+next.c;
                if(inqb[next.t]!=true) {
                    q.push(next.t);
                    inqb[next.t]=true;
                }
            }
        }
    }
    return ;
}
////////////////////////////////////////////////////
int inqs[100+10];
int tods[100+10];
void antispfas(int x) {
    memset(tods,0x3f,sizeof(tods));
    queue<int> q;
    q.push(x);
    tods[x]=0;
    inqs[x]=true;
    while(!q.empty()) {
        int now=q.front();
        q.pop();
        inqs[now]=false;
        int ls=antisis[now].size();
        for(int i=0; i<ls; i++) {
            edge next=antisis[now][i];
            if(tods[next.t]>tods[next.f]+next.c) {
                tods[next.t]=tods[next.f]+next.c;
                if(inqs[next.t]!=true) {
                    q.push(next.t);
                    inqs[next.t]=true;
                }
            }
        }
    }
    return ;
}
////////////////////////////////////////////////////

struct zt {
    int dot;
    int past;
    int tot;
};
struct cmp {
    bool operator ()(const zt &a,const zt &b) {
        if(a.tot>b.tot) return true;
        else return false;
    }
};
void astars();
bool flag=false;
void astarb() {

    priority_queue<zt,vector<zt>,cmp> q;
    zt first;
    first.dot=1;
    first.past=0;
    first.tot=first.past+todb[first.dot];
    q.push(first);
    while(!q.empty()) {
if(flag==true){return;}
        zt now=q.top();
        q.pop();
        if(now.dot==n) {
            if(ansb[now.past]==true) {
//cout<<"找到一条哥哥之前走过的路,不算数,路径长为"<<now.past<<"米"<<endl;
                continue;
            } else {
//cout<<"找到一条哥哥新路,路径长为"<<now.past<<"米"<<endl;
                bmin=max(bmin,now.past);
                ansb[now.past]=true;
                if(anss[now.past]==true) {
                    flag=true;
                    cout<<now.past<<endl;
                    return;
                }
                if(now.past>smin){
//cout<<"现在要问问一抹多走过没有?一抹多听得到吗"<<endl;
                astars();
                }
                else{
                continue;
                }

            }
        }
        int lb=bro[now.dot].size();
        for(int i=0; i<lb; i++) {
            edge temp=bro[now.dot][i];
            zt next;
            next.dot=temp.t;
            next.past=now.past+temp.c;
            next.tot=next.past+todb[next.dot];
            q.push(next);
        }
    }
    return;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void astars() {
if(flag==true){return;}

    priority_queue<zt,vector<zt>,cmp> q;
    zt first;
    first.dot=1;
    first.past=0;
    first.tot=first.past+tods[first.dot];
    q.push(first);
    while(!q.empty()) {
if(flag==true){return;}
        zt now=q.top();
        q.pop();
        if(now.dot==n) {
            if(anss[now.past]==true) {
//cout<<"找到一条妹妹之前走过的路,不算数,路径长为"<<now.past<<"米"<<endl;
                continue;
            } else {
//cout<<"找到一条妹妹新路,路径长为"<<now.past<<"米"<<endl;
                smin=max(smin,now.past);
                anss[now.past]=true;
                if(ansb[now.past]==true) {
//cout<<"这个长度哥哥也走过耶,那这就是答案了==>>";
                    cout<<now.past<<endl;
                    flag=true;
                    return;
                } else {
 //cout <<"妹妹长度为==>>"<<now.past<<endl;
                    if(now.past>bmin) {
 //cout <<"妹妹太长了,让哥哥长一点"<<endl;
                        astarb();
                    } else {
//cout <<"妹妹不够长,妹妹要更长呢"<<endl;
                        continue;
                    }
                }
            }

        }


        int ls=sis[now.dot].size();
        for(int i=0; i<ls; i++) {
            edge temp=sis[now.dot][i];
            zt next;
            next.dot=temp.t;
            next.past=now.past+temp.c;
            next.tot=next.past+tods[next.dot];
            q.push(next);
        }
    }
    return;
}
int main() {
    cin>>n>>m;
    for(int x=1; x<=m; x++) {
        int a1,a2,a3,a4;
        cin>>a1>>a2>>a3>>a4;
        edge tempb,temps;
        tempb.f=a1;
        tempb.t=a2;
        tempb.c=a3;
        temps.f=a1;
        temps.t=a2;
        temps.c=a4;
        bro[tempb.f].push_back(tempb);
        swap(tempb.f,tempb.t);
        antibro[tempb.f].push_back(tempb);


        sis[temps.f].push_back(temps);
        swap(temps.f,temps.t);
        antisis[temps.f].push_back(temps);
    }
    antispfab(n);
    antispfas(n);
//  for(int x=1;x<=n;x++){
//  cout<<todb[x]<<" ";
//  }
//  cout<<endl;
//  for(int x=1;x<=n;x++){
//  cout<<tods[x]<<" ";
//  }
    astarb();
    astars();
    if(flag==false)
    cout<<"IMPOSSIBLE"<<endl; 
    return 0;
}