管理员 为什么 compile error!!!!!!!!!

#include<iostream>
#include<cstring>
#include<cstdio>
#define INF 400000
#define hashsize 1000003
using namespace std;
typedef int state[9];
state st[INF];
int head[hashsize],next[hashsize];
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
int gra[9];
int dist[INF];

bool finish(state &s){
    if((s[0]+s[1]+s[2])!=15) return false;
    if((s[3]+s[4]+s[5])!=15) return false;
    if((s[6]+s[7]+s[8])!=15) return false;
    if((s[0]+s[3]+s[6])!=15) return false;
    if((s[1]+s[4]+s[7])!=15) return false;
    if((s[2]+s[5]+s[8])!=15) return false;
    if((s[0]+s[4]+s[8])!=15) return false;
    if((s[2]+s[4]+s[6])!=15) return false;
    return true;
}
int hash(state &s){
    int i,v=0;
    for(i=0;i<9;i++){
        v=v*10+s[i];
    }
    return v%hashsize;
}

bool try_to_insert(int s){
    int h=hash(st[s]);
    int u=head[h];
    while(u){
        if(memcmp(st[u],st[s],sizeof(st[s]))==0) return 0;
        u=next[u];
    }
    next[s]=head[h];
    head[h]=s;
    return 1;
}

int bfs(){
    memset(head,0,sizeof(head));
    memset(dist,INF,sizeof(dist));
    memset(next,0,sizeof(next));
    int h=1,t=2;
    dist[h]=0;
    try_to_insert(h);
    while(h<t){
        state &s=st[h];
        if(finish(s)) return h;
        int i,j,x,y,newx,newy,newz;
        for(i=0;i<9;i++){
            x=i/3; y=i%3;
            for(j=0;j<4;j++){
                newx=x+dx[j];
                newy=y+dy[j];
                if(0<=newx&&newx<=2&&newy>=0&&newy<=2){
                    newz=newx*3+newy;
                    state &q=st[t];
                    memcpy(q,s,sizeof(s));
                    q[newz]=s[i];
                    q[i]=s[newz];
                    dist[t]=dist[h]+1;
                    if(try_to_insert(t)) t++;
                }
            }
        }
    h++;
    }
    return 0;
}
int main(){
    //freopen("in.txt","r",stdin);
    int i;
    for(i=1;i<=3;i++){
        int k;
        for(k=0;k<9;k++)
          cin>>st[1][k];
          int t=bfs();
          if(t==0) cout<<"-1"<<endl;
          else cout<<dist[t]<<endl;
    }
    return 0;
}

2 条评论

  • @ 2016-09-17 20:43:13

    Thank you

  • @ 2016-09-15 23:19:39

    我们开启了C++11,所以您需要修改一下程序中的符号名称,比如hash和next。

  • 1

信息

ID
1029
难度
6
分类
搜索 | 搜索与剪枝 点击显示
标签
递交数
2565
已通过
624
通过率
24%
被复制
14
上传者