• 八数码难题神奇!!!


    为啥A不了??

    标程如下:【转载】

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<vector>
    #include<cstring>
    #include<queue>
    #include<unordered_map>
    #define IL inline
    #define R register
    #define I int
    #define OP operator
    #define CT const
    #define FUCK friend
    #define FO(i,a,b) for(I i=a;i<=b;++i)
    #define fo(i,a,b) for( I i=a;i<b;++i)
    #define ll long long
    CT I goal = 123804765;
    typedef bool QWQ;
    using namespace std;
    struct ED{
        short a[3][3];
    };
    struct EE{
        int v;
        int id;
        EE(I a=0,I b=0):v(a),id(b){}
    };
    struct D{
        I x;
        I y;
    }dd[]={{0,1},{1,0},{-1,0},{0,-1}};
    I d[10]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};
    
    I toint(ED x){
        I a=0;
        fo(i,0,9){
            a+=x.a[i/3][i%3]*d[i];
        }
        return a;
    }
    ED toju(I x){
        ED s;
        fo(i,0,9){
            s.a[i/3][i%3]=x/d[i]%10;
        }
        return s;
    }
    queue< EE >q;
    unordered_map<I,I> mp;
    I fir;
    I main(){
        scanf("%d",&fir);
        q.push(EE(fir,0));
        while(!q.empty()){
                EE x=q.front();q.pop();
                if(mp.count(x.v))continue;
                mp[x.v]=1;
                if(x.v==goal){
                    cout<<x.id<<endl;
                    return 0;
                }else{
                    ED xx=toju(x.v);
                    fo(i,0,3){
                        fo(j,0,3){
                            if(xx.a[i][j]==0)
                                fo(k,0,4){
                                    I fx,fy;
                                    fx=i+dd[k].x;
                                    fy=j+dd[k].y;
                                    if(fx<0||fy<0||fx>=3||fy>=3)
                                    {
                                        continue;
                                    }else{
                                        ED xxxx=xx;
                                        swap(xxxx.a[i][j],xxxx.a[fx][fy]);
                                        I xxx=toint(xxxx); 
                                        q.push(EE(xxx,x.id+1));
                                    }
                                }
                         }
                    }
                }   
           }
        return 0;
    }
    

    这个是对的,我的就不对了!!神奇!!!!!!

    本人垃圾代码:

    #include<bits/stdc++.h>
    using namespace std;
    struct sd{
    	int x,y;
    	int mmp[4][4];
    }loc[500004];
    int goal[4][4];
    int p1,p2;
    int z[500004];
    int x,y;
    int dirx[2]={1,-1};
    int diry[2]={1,-1};
    int goalx,goaly;
    void init()
    {
    	goalx=2;goaly=2;
    	char ch[14];
    	scanf("%s",ch);
    	loc[0].mmp[1][1]=ch[0]-48;
    	loc[0].mmp[1][2]=ch[1]-48;
    	loc[0].mmp[1][3]=ch[2]-48;
    	loc[0].mmp[2][1]=ch[3]-48;
    	loc[0].mmp[2][2]=ch[4]-48;
    	loc[0].mmp[2][3]=ch[5]-48;
    	loc[0].mmp[3][1]=ch[6]-48;
    	loc[0].mmp[3][2]=ch[7]-48;
    	loc[0].mmp[3][3]=ch[8]-48;
    	goal[1][1]=1;//123804765
    	goal[1][2]=2;
    	goal[1][3]=3;
    	goal[2][1]=8;
    	goal[2][2]=0;
    	goal[2][3]=4;
    	goal[3][1]=7;
    	goal[3][2]=6;
    	goal[3][3]=5;
    	for(int i=0;i<=8;++i)
    	{
    		if(ch[i]=='0')
    		{
    			x=i/3+1;
    			y=i%3+1;
    		}
    	}
    } 
    bool judge(int k)
    {
    	for(int i=1;i<=k;++i)
    	{
    		for(int j=1;j<=3;++j)
    		{
    			if(loc[k].mmp[i][j]!=goal[i][j])
    			return false;
    		} 
    	}
    	return true;
    }
    void seach()
    {
    	loc[0].x=x; loc[0].y=y;
    	int p1=0,p2=0;
    	while(p1<=p2)
    	{
    		for(int i=0;i<=1;++i)
    		{
    			if(loc[p1].x+dirx[i]>3||loc[p1].x+dirx[i]<=0) continue;
    			p2++;
    			for(int j=1;j<=3;++j)
    			{
    				for(int k=1;k<=3;++k)
    				{
    					loc[p2].mmp[j][k]=loc[p1].mmp[j][k];
    				} 
    			}
    			z[p2]=z[p1]+1;
    			loc[p2].x=loc[p1].x+dirx[i];
    			loc[p2].y=loc[p1].y;
    			swap(loc[p2].mmp[loc[p1].x][loc[p1].y],loc[p2].mmp[loc[p2].x][loc[p2].y]); 
    			if(loc[p2].x==goalx&&loc[p2].y==goaly)
    			{
    				if(judge(p2)) {
    					printf("%d",z[p2]);
    					exit(0);
    				}
    			}
    		}
    		for(int i=0;i<=1;++i)
    		{
    			if(loc[p1].y+diry[i]>3||loc[p1].y+diry[i]<=0) continue;
    			p2++;
    			for(int j=1;j<=3;++j)
    			{
    				for(int k=1;k<=3;++k)
    				{
    					loc[p2].mmp[j][k]=loc[p1].mmp[j][k];
    				} 
    			}
    			z[p2]=z[p1]+1;
    			loc[p2].y=loc[p1].y+diry[i];
    			loc[p2].x=loc[p1].x;
    			swap(loc[p2].mmp[loc[p1].x][loc[p1].y],loc[p2].mmp[loc[p2].x][loc[p2].y]);
    			if(loc[p2].x==goalx&&loc[p2].y==goaly)
    			{
    				if(judge(p2)) {
    					printf("%d",z[p2]);
    					exit(0);
    				}
    			}
    		}
    		p1++;
    	}
    }
    int main()
    {
    	init();
    	seach();
    } 
    //283104765
    

    厉害的大佬帮忙看一下,谢谢!!!!!!

  • 相关阅读:
    JS运算符
    JS基础
    0507-弹性盒子
    0506css3(2D、3D)动画
    CSS3边框
    0503-格式与布局
    0502-边框边界
    0502-其他html标签
    0428-专题块状元素
    mysql 数据库学习
  • 原文地址:https://www.cnblogs.com/mudrobot/p/13331184.html
Copyright © 2020-2023  润新知