sagmentation fault
查看原帖
sagmentation fault
315205
Kniqht楼主2022/9/10 22:27

我真服了。。两道几乎是双倍经验的垃圾搜索题,一共写了将近四个小时

有大佬能帮忙看出为什么sagmentation fault就好了,谢谢大佬!

//参考落汐大佬题解
#include<bits/stdc++.h>
#define PII pair<string,int>
#define fir first
#define sec second
#define tx i+dx[d]
#define ty j+dy[d]
#define CHECK tx>=1&&tx<=n&&ty>=1&&ty<=n
#define gh Hash()
using namespace std;
const int N=5;
const int dx[4]={-1,0,1,0};
const int dy[4]={0,1,0,-1};
int n,now[N][N];
string st,ed;
map<string,bool> h;
void get(string str){
    int len=0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++) now[i][j]=str[len++]-'0';
}
string Hash(){
    string str="";
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++) str+=(now[i][j]+'0');
    return str;
}
int ans;
void BFS(){
    //PII: str,depth
    queue<PII> q;
    q.push({st,0});
    h[st]=true;
    while(!q.empty()){
        PII t=q.front();
        q.pop();
        int depth=t.sec;
        string nstr=t.fir;
        // if(h[nstr]) continue;
        // h[nstr]=true;
        if(nstr==ed){
            ans=depth;
            return;
        }
        get(nstr);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++){
                if(!now[i][j]) continue;
                for(int d=0;d<4;d++)
                    if(CHECK&&!now[tx][ty]){
                        now[tx][ty]=1;now[i][j]=0;
                        string newstr=gh;
                        now[tx][ty]=0;now[i][j]=1;
                        if(h[newstr]) continue;
                        h[newstr]=true;
                        q.push({newstr,depth+1});
                        
                    }
            }
    }
}
struct Node{
    int x1,y1,x2,y2;
}pre[N];
int cnt;
void print(){
    for(int i=1;i<=cnt;i++)
        printf("%d%d%d%d\n",pre[i].x1,pre[i].y1,pre[i].x2,pre[i].y2);
    cout<<endl;
}
void DFS(string nowstr,int dep){
    if(nowstr==ed){
        print();
        exit(0);
    }
    if(dep>ans) return;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++){
            get(nowstr);
            // if(i==1&&j==2&&!cnt) cout<<now[2][2];
            if(!now[i][j]) continue;
            for(int d=0;d<4;d++)
                if(CHECK&&!now[tx][ty]){
                    now[tx][ty]=1;now[i][j]=0;
                    string newstr=gh;
                    now[tx][ty]=0;now[i][j]=1;
                    if(gh!=nowstr) cout<<"fuck";
                    if(h[newstr]) continue;
                    h[newstr]=true;
                    pre[++cnt]={i,j,tx,ty};
                    DFS(newstr,dep+1);
                    cnt--;
                }
        }
}
int main(){
    n=4;st="";ed="";
    string str;
    for(int i=1;i<=n;i++){cin>>str;st+=str;}
    for(int i=1;i<=n;i++){cin>>str;ed+=str;}
    // get(st);
    // for(int i=1;i<=n;i++)
    //     for(int j=1;j<=n;j++)
    //         cout<<now[i][j]<<" ";
    BFS();
    // cout<<ans<<endl;
    h.clear();
    h[st]=true;
    cout<<ans<<endl;
    DFS(st,0);
    // BFS();
    // cout<<ans;
    return 0;
}
2022/9/10 22:27
加载中...