Full arrangement + n queens problem (C++ depth-first search and breadth-first search)

3 full array

Input a string without repeated elements from the keyboard, and output the full arrangement of all characters in the string

Input format:

A string, the input guarantees that there are no repeated characters in the string, the length of the string does not exceed 10, and the string does not contain spaces.

Output format:

Output the full permutation of all characters in this string in lexicographical order. There are no spaces between each character. Line breaks between each permutation.

Input example 1:

ABC

Output sample 1:

ABC
ACB
BAC
BCA
CAB
CBA

Input example 2:

CAB

Sample output 2:

ABC
ACB
BAC
BCA
CAB
CBA

Input example 3:

ATOM

Output sample 3:

AMOT
AMTO
AOMT
AOTM
ATMO
ATOM
MAOT
MATO
MOAT
MOTA
MTAO
MTOA
OAMT
OATM
OMAT
OMTA
OTAM
OTMA
TAMO
TAOM
TMAO
TMOA
TOAM
TOMA

code length limit

16 KB

time limit

400 ms

memory limit

64 MB

#include<iostream>
#include <memory.h>
#include <string>
#include <algorithm>

using namespace std;
bool visited[12] = {false};
string s;
int num = 0;
char mark[12];


void dfs(int n);

int main()
{

    cin >> s;
    for(int i = 0; i < s.size()-1; i++)
        for(int j = i+1; j < s.size(); j++)
            if(s[i] > s[j])
            {
                char m = s[i];
                s[i] = s[j];
                s[j] = m;
            }
    bfs(0);

}

void dfs(int n)
{
    //输出字符串所有字符后换行返回
    if(num == s.size())
    {

        cout << mark << endl;

        return;
    }


    for(int i = 0; i < s.size(); i++)
    {


        //如果没访问过该字符、并且不是当前字符,就接着bfs该字符
        if(visited[i] == false)
        {
            visited[i] = true;  //设为访问过了
            mark[num++] = s[i]; //将该字符存入mark
            dfs(i);             //bfs该字符
            visited[i] = false; //调用完回溯回来,设为没访问过
            num--;              //访问过的字符长度-1
        }
    }
}

4 n queens problem

*It is required to place n queens who will not attack each other on a chessboard of n n grids. According to the rules of chess, a queen can attack a piece that is on the same row or column or on the same diagonal as the queen.

Input format:

There are multiple groups of test data, which are processed to the end of the file. For each set of tests, enter the size n of the chessboard (1<n<12).

Output format:

For each set of tests, output the number of solutions that meet the requirements.

Input sample:

4

Sample output:

2

code length limit

16 KB

Python (python3)

time limit

4000 ms

memory limit

64 MB

other compilers

time limit

400 ms

memory limit

64 MB

#include<iostream>
#include <memory.h>
#include <string>
#include <algorithm>
#include <stdio.h>

using namespace std;
void HanShu(int x, int y);
int mark = 0;
int n;
int point[12][12] = {0};
int row = 0; //到第几行该放棋子了

int main()
{

    while(cin >> n)
    {
        mark = 0; row = 0;
        memset(point, 0, sizeof(point));

        //从第0行开始判断
        HanShu(0, 0);
        cout << mark << endl;

    }
return 0;
}

void HanShu(int x, int y)
{
    //判断到第n行结束
    if(x == n)
    {
        mark++;
        return;
    }


    for(int s = 0; s < n; s++)
    {
        int m = 0; //标记是否有冲突棋子
        y = s;
        //把当前坐标放上棋子
        point[x][y] = 1;
        //cout<<"我放置了棋子在"<<'(' << x << ' ' << y <<')'<<endl;
        //当与他一行或一列或斜线有1时,停止循环
        for(int i = 0; i < x; i++)
        {
            if(point[i][y] == 1)
            {
                //cout<<'(' << x << ' ' << y <<')'<<"同列有棋子"<<'(' << i << ' ' << y <<')'<<endl;
                point[x][y] = 0;
                m = 1;
                break;
            }

        }
        for(int i = 0; i < y; i++)
        {

            if(point[x][i] == 1)
            {
                //cout<<'(' << x << ' ' << y <<')' << "同行有棋子"<<'(' << x << ' ' << i <<')'<<endl;
                point[x][y] = 0;
                m = 1;
                break;
            }
        }
        //判断左上斜线的1
        int x2 = x-1, y2 = y-1;
        while(x2 >= 0 && y2 >= 0)
        {

            if(point[x2][y2] == 1)
            {
                //cout<<'(' << x << ' ' << y <<')'<<"左上斜线有棋子"<<'(' << x2 << ' ' << y2 <<')'<<endl;
                point[x][y] = 0;
                m = 1;
                break;
            }
            x2--;
            y2--;
        }
        //判断右上斜线的1
        x2 = x-1;
        y2 = y+1;
        while(x2 >= 0 && y2 < n)
        {

            if(point[x2][y2] == 1)
            {
                //cout<<'(' << x << ' ' << y <<')'<<"右上斜线有棋子"<<'(' << x2 << ' ' << y2 <<')'<<endl;
                point[x][y] = 0;
                m = 1;
                break;
            }
            x2--;
            y2++;
        }
        if(m == 1) continue;
        //到这说明该位置可以放棋子,继续往下调用,换到下一行
        //cout<<'(' << x << ' ' << y <<')'<<"没有冲突的棋子,我接下来放" <<'(' << x+1 << ' ' << 0 <<')'<<endl;
        HanShu(x+1, 0);
        //回溯后棋子拿下
        point[x][y] = 0;
    }
}

Guess you like

Origin blog.csdn.net/weixin_63484669/article/details/127600703