N-Queen problem
problem Description:
Place the N Queens on the chessboard of the nxn against each other. According to the rules of chess, the Queen can attack a piece that is on the same line or in the same column or in the same slash. n the problem is equivalent to placing n queens on the NxN chessboard, any 2 queens may wish to be on the same line or in the same column or on the same slash.
Requirement Input:
Given the size of the chessboard N (n≤13)
Demand output:
How many placement methods are available for the output.
#include <stdio.h> #include <math.h> #define MAX int total = 0; char m[
Max][max];
int is_ok (int s,int i,int *a) {int J; For (J=1 j< s; j + +) if (ABS (i-a[j) = = ABS (s-j)) | | (i = = A[j])
/* Fill in by line, so as long as the check column and oblique column whether there is Queen can be * * * return 0;
return 1;
} void Nfind (int s,int n,int *a) {int i,p,q;
if (s > N) {total + +;
printf ("========the num of%d is ==========", total);
for (p=1; p<= N; p++) {for (q=1; q<=n; q++) printf ("%c", m[p][q);
printf ("");
} else {for (i=1; i<= n; i++) if (IS_OK (s,i,a)) {a[s]=i;
m[s][i]= ' O ';
Nfind (S+1,n,a);
m[s][i]= ' * ';
int main (void) {int n, a[max]={0}}};
scanf ("%d", &n);
memset (M, ' * ', sizeof (m));
Nfind (1,n,a);
printf ("total=%d", total);
System ("pause");
return 0; }
Elevator problem
Problem Description:
Oh, one day I had a dream, dreamed of a very strange elevator. Elevators are available on every floor of the building, and there is a digital Ki (0<=ki<=n) on the Level I floor (1<=i<=n). The elevator has only four buttons: open, close, up, down. The number of layers up and down equals the number on the current floor. Of course, if the request is not met, the corresponding button will fail. For example: 3 3 1 2 5 represent Ki (k1=3,k2=3,......), starting from the first floor. On the first floor, press "up" to the 4 floor, press "down" is not working because there is no-2 floor. So, how many buttons do we have to press at least a few times from building a to B?
Requirement Input:
The input file has two lines, the first behavior three positive integers separated by spaces, representing N,a,b (1≤n≤200, 1≤a,b≤n), and the second behavior N a positive integer separated by a space, representing Ki.
Demand output:
Output file is only one line, that is, the minimum number of keystrokes, if not reachable, output-1.
Sample input:
Sample output:
Copy Code code as follows:
Implementation code:
#include <stdio.h>
#define MAX 501
int a,b,n;
int v[max],flag=0;
void Trytofind (int x,int total)
{
if (x-v[x] = = B | | x+v[x] = = b)
{
if (Total < flag | | flag = 0)
Flag = total;
}
else
{
if (X+v[x] <= N)
trytofind (x+v[x],total+1);
if (X-v[x] >= 1)
trytofind (x-v[x],total+1);
}
}
int main (void)
{
int i,j,k;
scanf ("%d%d%d", &n,&a,&b);
for (i=1; i<= n; i++)
scanf ("%d", &v[i]);
Trytofind (a,1);
if (flag)
printf ("%d", flag);
else
printf ("-1");
System ("pause");
return 0;
}