C Pointer Programming Path---tenth notes

Source: Internet
Author: User

Examples of pointers in search algorithms
Maze algorithm
The longest search is in depth-first search and breadth-first search
Depth-First search is like a name, searching the end of every road,
In order to prevent the idea, specially set up the stack this data structure
So that every time you find the idea of the time can also exit to the starting point.
//
//
Breadth First Search
Breadth-first search is to use the nature of the first-out nature of the queue, put each search results into the queue,
Exclusion of ideas and other conditions
//
Backtracking method
is to enumerate each possible judgment, and if it can be executed, it is not possible to return to the beginning of the place
The realization of the eight Queens: Backtracking method
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace Std;
int a[9] = {100};
Number of solutions to store
int count = 0;
int place (int i, int value)
{
Int J;
if (i = = 1)
return 1;
for (j = 1; j < i; ++j)
{
if (a[j] = = value)
return 0;
if (ABS (i-j) = = ABS (Value-a[j]))
return 0;
}
return 1;
}


void Showresult ()
{
int I, J;
for (j = 1; J <= 8; ++j)
{
for (i = 1; i < a[j]; ++i)
{
printf ("*");
}
printf ("Q");
for (i = i + 1; I <= 8; ++i)
{
printf ("*");
}
printf ("\ n");
}
}


void Backtrack (int t)
{
int i;
if (T > 8)
{
printf ("*********************");
Showresult ();
count++;
Return
}
Else
{
for (i = 1; I <= 8; ++i)
{
if (place (t, i))
{
A[t] = i;
Backtrack (t + 1);
}
}
}
}






void Showcount ()
{
printf ("\ n Eight queen's question total%d solutions: \ n", count);
}


int main ()
{
Backtrack (1);
Showcount ();
return 0;


}

C Pointer Programming Path---tenth notes

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.