91分求助pwp
查看原帖
91分求助pwp
389997
夏米亚丁楼主2020/11/2 16:57

求助,本萌新实在在不知道哪里有问题,得了91分pwp

#include<bits/stdc++.h> 
using namespace std;
const int MAXN = 2400005;
const int P = 2333333;
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
bool xc[MAXN];
char c;
bool out(int x,int y) 
{
    if(x < 1 || x > 3 || y < 1 || y > 3)
        return false;
    else
        return true;
}

struct edge
{
    int maps[5][5];
    int g,h,x,y;
    int hash;

    void gets()
    {
        hash = 0;
        for(int i = 1; i <= 3; i ++)
        {
            for(int j = 1; j <= 3; j ++)
            {
                hash *= 10;
                hash += maps[i][j];
                hash %= P; 
            }
        }
        return;
    }

    void clear()
    {
        for(int i = 1; i <= 3; i ++)
            for(int j = 1; j <= 3; j ++)
                if(!maps[i][j])
                    x = i,y = j;
        return;
    }

    bool operator < (const edge a)  const
    {
        return h + g > a.h + a.g;
    }

    bool can()
    {
        gets();
        if(xc[hash])    return false; 
        return true;
    }
}aim;
void get_h(edge &x)
{
    x.h = 0;
    for(int i = 1; i <= 3; i ++)
        for(int j = 1; j <= 3; j ++)
            x.h += (x.maps[i][j] != aim.maps[i][j]);
    return;

}

priority_queue < edge > q;
edge s;
int Astar(edge s)
{
    aim.maps[1][1] = 1;
    aim.maps[1][2] = 2;
    aim.maps[1][3] = 3;
    aim.maps[2][1] = 8;
    aim.maps[2][2] = 0;
    aim.maps[2][3] = 4;
    aim.maps[3][1] = 7;
    aim.maps[3][2] = 6;
    aim.maps[3][3] = 5; 
    aim.h = 0;
    aim.gets();
    s.gets();
    xc[s.hash] = true;
    q.push(s);
    while(!q.empty())
    {
        edge u = q.top();
        q.pop();
        if(u.hash == aim.hash)
            return u.g;
        u.clear();  
        for(int i = 0; i < 4; i ++)
        {
            edge v = u;
            int tx = v.x + dx[i];
            int ty = v.y + dy[i];
            if(out(tx,ty)) 
                swap(v.maps[v.x][v.y],v.maps[tx][ty]);
            v.x += dx[i],v.y += dy[i];
            v.clear(); 
            if(!v.can())    continue; 
            get_h(v); 
            v.g = u.g + 1;
            xc[v.hash] = true;
            q.push(v); 
        }   
    }
    return -1;
}

int main(){
    for(int i = 1; i <= 3; i ++)
    {
        for(int j = 1; j <= 3; j ++)
        {
            c = getchar();
            while(!isdigit(c))
                c = getchar();
            s.maps[i][j] = c - '0';
        }
    }
    printf("%d\n",Astar(s));
    return 0;
}

第12,14,30点错了pwp

2020/11/2 16:57
加载中...