750 gives W.A. Please give me someCritical Input & Outpu
Posted: Wed Sep 29, 2004 9:39 am
750 gives W.A. Please give me someCritical Input & Output
Code: Select all
4
1 2
6 7
7 8
8 8
Code: Select all
SOLN COLUMN
# 1 2 3 4 5 6 7 8
1 3 1 7 5 8 2 4 6
2 4 1 5 8 2 7 3 6
3 4 1 5 8 6 3 7 2
4 5 1 4 6 8 2 7 3
5 5 1 8 4 2 7 3 6
6 5 1 8 6 3 7 2 4
7 6 1 5 2 8 3 7 4
8 7 1 3 8 6 4 2 5
SOLN COLUMN
# 1 2 3 4 5 6 7 8
1 1 7 5 8 2 4 6 3
2 2 5 7 1 3 8 6 4
3 2 5 7 4 1 8 6 3
4 2 7 5 8 1 4 6 3
5 4 2 7 5 1 8 6 3
6 4 7 1 8 5 2 6 3
7 4 8 1 5 7 2 6 3
8 5 2 4 7 3 8 6 1
9 5 3 1 7 2 8 6 4
10 5 3 8 4 7 1 6 2
11 5 7 1 4 2 8 6 3
12 5 7 4 1 3 8 6 2
13 5 8 4 1 7 2 6 3
14 7 3 8 2 5 1 6 4
SOLN COLUMN
# 1 2 3 4 5 6 7 8
1 4 2 5 8 6 1 3 7
2 4 2 8 6 1 3 5 7
3 4 6 1 5 2 8 3 7
4 5 2 4 6 8 3 1 7
5 5 3 1 6 8 2 4 7
6 5 8 4 1 3 6 2 7
7 6 3 1 8 5 2 4 7
8 6 3 5 8 1 4 2 7
SOLN COLUMN
# 1 2 3 4 5 6 7 8
1 4 7 5 2 6 1 3 8
2 5 7 2 6 3 1 4 8
3 6 3 5 7 1 4 2 8
4 6 4 7 1 3 5 2 8
Code: Select all
#include<stdio.h>
#include<math.h>
int count;
int x[8];
int P,Q;
bool Place(int k,int i)
{
int j;
for(j=1;j<=k-1;j++)
{
if((x[j]==i)||(abs(x[j]-i))==(abs(j-k)))
return false;
}
return true;
}
void NQueens(int k,int n)
{
int i,j;
for(i=1;i<=n;i++)
{
if(Place(k,i))
{
x[k]=i;
if(k==n&&x[Q]==P)
{
count++;
printf("%2d ",count);
for(j=1;j<=n;j++)
printf("%2d",x[j]);
printf("\n");
}
else
NQueens(k+1,n);
}
}
}
main()
{
bool tag=0;
long test,testcase;
scanf("%ld",&testcase);
for(test=1;test<=testcase;test++)
{
scanf("%d%d",&P,&Q);
if(tag==0)
tag=1;
else
printf("\n");
printf("SOLN COLUMN\n");
printf(" # 1 2 3 4 5 6 7 8\n\n");
count=0;
NQueens(1,8);
}
}
Code: Select all
2
2 2
2 4
Code: Select all
SOLN COLUMN
# 1 2 3 4 5 6 7 8
1 4 2 5 8 6 1 3 7
2 4 2 7 3 6 8 1 5
3 4 2 7 3 6 8 5 1
4 4 2 7 5 1 8 6 3
5 4 2 8 5 7 1 3 6
6 4 2 8 6 1 3 5 7
7 5 2 4 6 8 3 1 7
8 5 2 4 7 3 8 6 1
9 5 2 6 1 7 4 8 3
10 5 2 8 1 4 7 3 6
11 6 2 7 1 3 5 8 4
12 6 2 7 1 4 8 5 3
13 7 2 4 1 8 5 3 6
14 7 2 6 3 1 4 8 5
15 8 2 4 1 7 5 3 6
16 8 2 5 3 1 7 4 6
SOLN COLUMN
# 1 2 3 4 5 6 7 8
1 3 6 4 2 8 5 7 1
2 3 6 8 2 4 1 7 5
3 4 6 8 2 7 1 3 5
4 4 7 5 2 6 1 3 8
5 6 1 5 2 8 3 7 4
6 6 3 7 2 4 8 1 5
7 6 3 7 2 8 5 1 4
8 7 3 8 2 5 1 6 4
Code: Select all
// 750 8 Queens Chess Problem
#include <stdio.h>
#include <stdlib.h>
#define N 8 // the N x N board
int d[N+1],c,r,count;
bool ispossible(int col, int row) {
int i;
if (row==r || abs(row-r)==abs(col-c)) return false;
for (i=1; i<col; i++)
if (row==d[i] || abs(row-d[i])==abs(col-i)) return false;
return true;
}
void step(int n) {
int i;
if (n == N+1) {
printf(" %d ", ++count);
for (i=1; i<=N; i++)
printf(" %d", d[i]);
putchar('\n');
return;
}
if (n == c)
step(n+1);
else
for (i=1 ; i<=N ; i++)
if (ispossible(n,i)) {
d[n] = i;
step(n+1);
}
}
void main() {
int i;
scanf("%d %d", &r, &c);
d[c] = r;
printf("SOLN COLUMN\n # 1 2 3 4 5 6 7 8\n\n");
count = 0;
step(1);
}
Code: Select all
4
1 2
6 7
7 8
8 8
Code: Select all
SOLN COLUMN
# 1 2 3 4 5 6 7 8
1 4 2 5 8 6 1 3 7
2 4 2 7 3 6 8 1 5
3 4 2 7 3 6 8 5 1
4 4 2 7 5 1 8 6 3
5 4 2 8 5 7 1 3 6
6 4 2 8 6 1 3 5 7
7 5 2 4 6 8 3 1 7
8 5 2 4 7 3 8 6 1
9 5 2 6 1 7 4 8 3
10 5 2 8 1 4 7 3 6
11 6 2 7 1 3 5 8 4
12 6 2 7 1 4 8 5 3
13 7 2 4 1 8 5 3 6
14 7 2 6 3 1 4 8 5
15 8 2 4 1 7 5 3 6
16 8 2 5 3 1 7 4 6
SOLN COLUMN
# 1 2 3 4 5 6 7 8
1 3 6 4 2 8 5 7 1
2 3 6 8 2 4 1 7 5
3 4 6 8 2 7 1 3 5
4 4 7 5 2 6 1 3 8
5 6 1 5 2 8 3 7 4
6 6 3 7 2 4 8 1 5
7 6 3 7 2 8 5 1 4
8 7 3 8 2 5 1 6 4