蒟蒻求助大佬们!为什么
查看原帖
蒟蒻求助大佬们!为什么
403799
Butane楼主2021/7/23 15:32

好心的各位大佬们: 为什么迭代加深dfsTLE了8个点,而bfs却过了? 迭代加深时间复杂度应该和bfs差不多呀,可是为什么会直接TLE了呢? 我的迭代加深dfsTLE了8个点,可是改成bfs,其他方面基本没怎么动,结果就过了,而且跑得还贼快。。。 迭代加深代码:

#include <stdio.h>
#include <vector>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <map>
#include <queue>
#include <iostream>
#include <cstring>
using namespace std;

const int maxv=10;
const int mod=1e9+7;
const int maxNum=0x7ffffff-10;
const double eps=1e-11;
const double PI=3.1415926535;
typedef long long LL;

int mp1[maxv][maxv],mp2[maxv][maxv],cx[4]={0,0,1,-1},cy[4]={1,-1,0,0};
int ansNum=0,nowNum=0;
bool vis[65000];
struct Node{
    int x,y;
};
vector<pair<Node,Node> > ans;
bool operator <(const pair<Node,Node> &a,const pair<Node,Node> &b){
    return a.first.x<b.first.x;
}
bool judge(int x,int y){
    if(x>=1&&x<=4&&y>=1&&y<=4) return true;
    return false;
}
void change(int &now,int k,int type){
    if(type) now=now|(1<<k);
    else now=now&(~(1<<k));
}
void dfs(int now,int layer){
    if(now==layer+1){
        if(ansNum==nowNum){
            printf("%d\n",now-1);
            for(int i=0;i<ans.size();i++)
                printf("%d%d%d%d\n",ans[i].first.x,ans[i].first.y,ans[i].second.x,ans[i].second.y);
            exit(0);
        }
        return;
    }
    for(int i=1;i<=4;i++)
        for(int j=1;j<=4;j++){
            if(mp1[i][j])
            for(int k=0;k<4;k++){
                int nowX=i+cx[k],nowY=j+cy[k];
                if(judge(nowX,nowY)&&mp1[i][j]!=mp1[nowX][nowY]){
                    swap(mp1[i][j],mp1[nowX][nowY]),change(nowNum,(i-1)*4+j-1,0),change(nowNum,(nowX-1)*4+nowY-1,1);
                    ans.push_back({{i,j},{nowX,nowY}});
                    if(!vis[nowNum])
                        vis[nowNum]=1,dfs(now+1,layer),vis[nowNum]=0;
                    ans.pop_back();
                    swap(mp1[i][j],mp1[nowX][nowY]),change(nowNum,(i-1)*4+j-1,1),change(nowNum,(nowX-1)*4+nowY-1,0);
                }
            }
        }
}
int main(){
    for(int i=1;i<=8;i++)
        for(int j=1;j<=4;j++)
            if(i<=4) scanf("%1d",&mp1[i][j]),change(nowNum,(i-1)*4+j-1,mp1[i][j]);
            else scanf("%1d",&mp2[i-4][j]),change(ansNum,(i-5)*4+j-1,mp2[i-4][j]);
    for(int i=1;i<=1000;i++) memset(vis,0,sizeof(vis)),vis[nowNum]=1,dfs(1,i);
    return 0;
}

bfs代码:

#include <stdio.h>
#include <vector>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <map>
#include <queue>
#include <iostream>
#include <cstring>
using namespace std;

const int maxv=10;
const int mod=1e9+7;
const int maxNum=0x7ffffff-10;
const double eps=1e-11;
const double PI=3.1415926535;
typedef long long LL;

int mp1[maxv][maxv],mp2[maxv][maxv],cx[4]={0,0,1,-1},cy[4]={1,-1,0,0};
int ansNum=0,nowNum=0;
bool vis[65000];
struct Node{
    int x,y;
};
struct Order{
    int x1,y1,x2,y2,pre;
}ans[65000];
bool operator <(const pair<Node,Node> &a,const pair<Node,Node> &b){
    return a.first.x<b.first.x;
}
bool judge(int x,int y){
    if(x>=1&&x<=4&&y>=1&&y<=4) return true;
    return false;
}
void change(int &now,int k,int type){
    if(type) now=now|(1<<k);
    else now=now&(~(1<<k));
}
void bfs(){
    queue<int> q;
    q.push(nowNum);
    while(q.size()){
        int top=q.front();q.pop();
        if(ansNum==top){
            int now=top;
            vector<pair<Node,Node> > all;
            while(top!=nowNum){
                all.push_back({{ans[top].x1,ans[top].y1},{ans[top].x2,ans[top].y2}});
                top=ans[top].pre;
            }
            printf("%d\n",all.size());
            for(int i=all.size()-1;i>=0;i--)
                printf("%d%d%d%d\n",all[i].first.x,all[i].first.y,all[i].second.x,all[i].second.y);
            exit(0);
        }
        for(int i=1;i<=4;i++)
            for(int j=1;j<=4;j++){
                int now=(i-1)*4+j-1;
                if((top>>now)&1)
                for(int k=0;k<4;k++){
                    int nowX=i+cx[k],nowY=j+cy[k];
                    if(judge(nowX,nowY)&&!((top>>((nowX-1)*4+nowY-1))&1)){
                        int x=top;
                        change(x,(i-1)*4+j-1,0),change(x,(nowX-1)*4+nowY-1,1);
                        if(!vis[x]){
                            vis[x]=1;
                            q.push(x);
                            ans[x].pre=top,ans[x].x1=i,ans[x].y1=j,ans[x].x2=nowX,ans[x].y2=nowY;
                        }
                    }
                }
            }
    }
}
int main(){
    for(int i=1;i<=8;i++)
        for(int j=1;j<=4;j++)
            if(i<=4) scanf("%1d",&mp1[i][j]),change(nowNum,(i-1)*4+j-1,mp1[i][j]);
            else scanf("%1d",&mp2[i-4][j]),change(ansNum,(i-5)*4+j-1,mp2[i-4][j]);
    bfs();
    return 0;
}

好心的大佬们拜托了QAQ

2021/7/23 15:32
加载中...