USACO Section 1.4 The Clocks (clocks)

bfs+剪枝。

如果某个钟表状态在之前出现过那么就不需要再加入到队列中了。由于不会哈希所以开了个9维数组判重。

之前因为路径数组和clock数组合并在一起写了,结果一直内存超限。

这个题因为clock里面有个长度是10的数组所以在用路径的时候这个数组是没用的,所以这俩东西应该分开写。

总之这个WA,TLE,RE了N次。

/*
ID:kkkwjx1
LANG:C++
TASK:clocks
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
using namespace std;
struct Clocks
{
    int clo[10];
    int id;

};
struct Path
{
    int id,pre,op;
};
void Move(int n,Clocks &p)
{
    p.clo[n]+=3;
    if(p.clo[n]>12) p.clo[n]=3;
}
void Change(int n,Clocks &p)
{
    if(n==1)
    {
        Move(1,p);
        Move(2,p);
        Move(4,p);
        Move(5,p);
    }
    else if(n==2)
    {

        Move(1,p);
        Move(2,p);
        Move(3,p);
    }
    else if(n==3)
    {
        Move(2,p);
        Move(3,p);
        Move(5,p);
        Move(6,p);
    }
    else if(n==4)
    {
        Move(1,p);
        Move(4,p);
        Move(7,p);
    }
    else if(n==5)
    {
        Move(2,p);
        Move(4,p);
        Move(5,p);
        Move(6,p);
        Move(8,p);
    }
    else if(n==6)
    {
        Move(3,p);
        Move(6,p);
        Move(9,p);
    }
    else if(n==7)
    {
        Move(4,p);
        Move(5,p);
        Move(7,p);
        Move(8,p);
    }
    else if(n==8)
    {
        Move(7,p);
        Move(8,p);
        Move(9,p);
    }
    else if(n==9)
    {
        Move(5,p);
        Move(6,p);
        Move(8,p);
        Move(9,p);
    }
}
bool Check(Clocks &p)
{
    for(int i=1; i<=9; ++i)
        if(p.clo[i]!=12) return false;
    return true;
}
Path path[500000];
int ans[100],pos=0;
void Print_path(Path &p)
{
    if(p.id==0) return;
    Print_path(path[p.pre]);
    ans[pos++]=p.op;
}
bool vis[4][4][4][4][4][4][4][4][4];
int main()
{
    freopen("clocks.in","r",stdin);
    freopen("clocks.out","w",stdout);
    Clocks p;
    for(int i=1; i<=9; ++i) scanf("%d",&p.clo[i]);
    vis[p.clo[1]/3-1][p.clo[2]/3-1][p.clo[3]/3-1][p.clo[4]/3-1][p.clo[5]/3-1][p.clo[6]/3-1][p.clo[7]/3-1][p.clo[8]/3-1][p.clo[9]/3-1]=true;
    queue<Clocks> q;
    int _id=0;
    p.id=_id;
    path[_id].id=p.id;
    path[_id++].pre=-1;
    q.push(p);
    bool ok=false;
    while(!q.empty()&&!ok)
    {
        Clocks tmp=q.front();
        q.pop();
        for(int i=1; i<=9; ++i)
        {
            Clocks t=tmp;
            Change(i,t);
            if(vis[t.clo[1]/3-1][t.clo[2]/3-1][t.clo[3]/3-1][t.clo[4]/3-1][t.clo[5]/3-1][t.clo[6]/3-1][t.clo[7]/3-1][t.clo[8]/3-1][t.clo[9]/3-1])
                continue;
            else vis[t.clo[1]/3-1][t.clo[2]/3-1][t.clo[3]/3-1][t.clo[4]/3-1][t.clo[5]/3-1][t.clo[6]/3-1][t.clo[7]/3-1][t.clo[8]/3-1][t.clo[9]/3-1]=true;
            t.id=_id;
            path[_id].id=t.id;
            path[_id].pre=tmp.id;
            path[_id++].op=i;
            if(Check(t))
            {
                ok=true;
                break;
            }
            else q.push(t);
        }
    }
    Print_path(path[_id-1]);
    sort(ans,ans+pos);
    for(int i=0; i<pos; ++i)
        if(i==0) printf("%d",ans[i]);
        else printf(" %d",ans[i]);
    printf("\n");
    return 0;
}


 

你可能感兴趣的:(bfs,剪枝,好题)