C++深度优先搜索错误

题目:

为什么好多次做题都这样,样例对了还不给过?


#include<iostream>
#include<cstdio>
using namespace std;
int MX[9],ML[16],MR[16],S[9][9],ans=0;
void print()
{
    ans++;
    cout<<"NO. "<<ans<<endl;
    for(int i=1;i<=8;i++)
    {
        for(int j=1;j<=8;j++)
        {
            
            cout<<S[j][i]<<" ";
        }
        cout<<endl;
    }
}
void DFS(int y)
{
    if(y==9)
    {
        print();
    }
    else
    {
        int Y=y+1;
        for(int i=1;i<=8;i++)
        {
            if(MR[Y+i-1]==0 && ML[Y-i+8]==0 && MX[i]==0)
            {
                S[y][i]=1;
                MR[Y+i-1]=1;
                ML[Y-i+8]=1;
                MX[i]=1;
                DFS(Y);
                MR[Y+i-1]=0;
                ML[Y-i+8]=0;
                MX[i]=0;
                S[y][i]=0;
            }
        }
    }
}
int main()
{
    DFS(1);
}

@ada;
回答一下

  • 这有个类似的问题, 你可以参考下: https://ask.csdn.net/questions/7485378
  • 你也可以参考下这篇文章:C++ 编写一个函数,把字符串中的数字字符、英文字母字符和其它字符分开。
  • 除此之外, 这篇博客: C++ 并发与多线程学习笔记(三)线程传参隐患 成员函数指针做线程函数中的 用成员函数指针做线程函数 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 上代码:

    #include <string>
    #include <iostream>
    using namespace std;
    class A
    {
    public:
    	int m_i;
    	A(int a) :m_i(a)
    	{
    		cout << "A:A(int a)构造函数执行"  << " ,id: " << this_thread::get_id() << endl;
    	}
    	A(const A &a) :m_i(a.m_i)
    	{
    		cout << "A:A(const A &a)构造函数执行" << " ,id: " << this_thread::get_id() << endl;
    	}
    	~A()
    	{
    		cout << "析构函数执行" << " ,id: " << this_thread::get_id() << endl;
    	}
    
    	void thread_work(int n)
    	{
    		cout << "thread_work执行了" << n << " ,id: " << this_thread::get_id() << endl;
    	}
    };
    
    int main()
    {
    	int mvar = 1;
    	int mysecondpar = 12;
    	
    	A a(10);
    
    	thread mytobj(&A::thread_work, a,10);
    
    	mytobj.join();
    	//mytobj.detach();
    	
    	cout << "主线程结束" << endl;
    	//system("pause");
    	return 0;
    }
    

    主要注意用法的不同:
    依次为成员函数、类对象、需要传入函数的参数。

    thread mytobj(&A::thread_work, a,10);
    

    运行结果:
    在这里插入图片描述
    可以尝试,这里的类对象仍可以用ref来引用并操作。
    需要注意的是,子线程使用的对象会调用该类的拷贝构造函数。
    多线程的内容需要不断积累和总结,内容虽然繁杂,但并不困难,多加练习和与实际应用相结合,便能加深理解。

  • 您还可以看一下 夏曹俊老师的C++微服务架构及安全云盘项目实训课程中的 完成了客户端的文件上传指令处理小节, 巩固相关知识点