Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define min(a,b) a < b ? a:b;
- int queens[8];int path[8];
- int valid (int r,int c){
- int j;
- for (j=0;j<r;j++){
- if (c==path[j]) return 0;
- if (abs(r-j)==abs(c-path[j])) return 0;
- }
- return 1;
- }
- int search(int n){
- int i;
- if (n==8) return 0;
- int tot = 100000;
- /*buscamos en cada fila*/
- for (i=0;i<8;i++){
- if (valid(n,i)){
- path[n]=i;
- if (i==queens[n]){
- tot=min(tot,search(n+1));
- }else{
- tot=min(tot,1+search(n+1));
- }
- }
- }
- return tot;
- }
- int main(int argc, char** argv) {
- int i;int cont=1;
- while (scanf("%d %d %d %d %d %d %d %d", &queens[0], &queens[1],
- &queens[2], &queens[3], &queens[4], &queens[5], &queens[6],
- &queens[7]) != EOF){
- for (i=0;i<8;i++){
- queens[i]=queens[i]-1;
- path[i]=queens[i];
- }
- printf("Case %d: %d\n", cont, search(0));
- cont++;
- }
- return (EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement