1 to 9 full array (with deep search language C + +)

Source: Internet
Author: User

C + + code:

#include <bits/stdc++.h>
using namespace Std;
#define FO (i,a,b) for (int i=a;i<=b;i++)
BOOL Visit[11];
int a[10];
void Dfs (int index)
{
Ios::sync_with_stdio (false); Cin.tie (0); Cout.tie (0);//make C + + output as fast as C
if (index==10)
{
cout<<a[1]<<a[2]<<a[3]<<a[4]<<a[5]<<a[6]<<a[7]<<a[8]<<a[ 9]<<endl;
Return
}
Fo (i,1,9)
{
if (!visit[i])
{
Visit[i]=true;
A[index]=i;
DFS (INDEX+1);
Visit[i]=false;
}
}
}
int main ()
{
DFS (1);
return 0;
}

Incidentally with C code (666):

#include <stdio.h>
int a[10];
BOOL Visit[11];
void Dfs (int index)
{
if (index==10)
{
printf ("%d%d%d%d%d%d%d%d%d\n", a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
Return
}
for (int i=1;i<=9;i++)
if (!visit[i])
{
Visit[i]=true;
A[index]=i;
DFS (INDEX+1);
Visit[i]=false;
}
}
int main ()
{
DFS (1);
return 0;
}

About here Dfs inside the recursive personal interpretation: (the first set of data 123456789 to the second set of data 123456798 changes process)

First look at the first set of data, must be an increment of 123456789; directly after Index=9 (A1-A9 is assigned to 1-9 in turn), DFS (index+1/* equals 10*/) starts outputting 123456789, and the 9th loop also ends, so the last loop is returned. That is, the 8th cycle of the seat, by the way, at this time visit[9] has been assigned to false (after the definition of a bool after false, used to become true, after the end of use to false), but Visit[1]-visit[7] is still true (and the A1-A7 is still assigned a value of 1-7,index or 8, and the I value is changed from 8 (i++) to 9 due to the end of the 9th loop, and Visit[8] becomes false due to the recursive end of the i=8 of the 8th layer), So A8 is given 9 (because the 8th loop is i=9 start), visit[9] becomes true at the same time, and then goes into the 9th loop (I start from 1), but because only visit[8] is false at this time, DFS (9) will a9= 8; Finally DFS (10) output 123456798, then visit[7]visit[8] and visit[9] to false, into the 7th loop, and the 7th layer I becomes 8 ...

/*

Visit[i]=true;

...

Visit[i]=false;

It can be understood here that, once transformed, the original Visit[i] will become false, because once I has changed, the recursion in this I has ended, the Visit[i]=false is executed, and the i++ is executed, so that I will change. So the I value changes, then the original visit[i] must be turned back to false (at least I think so)

*/

1 to 9 full array (with deep search language C + +)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.