⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reines.c

📁 For the incomplete methods, we kept the representation of the queens by a table and the method of ca
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>int nbReines = 0;int nbSolution = 0;int *place;//emplacement de la reine sur la ligneint **echiquier;char reponse = 'n';void afficherDansFic2(int nbElem, int **matrix){	FILE *placement;	int i,j,k;	int ligneNOK = 1;		placement=fopen("placement.txt","w");	for(i=0;i<nbElem;i++){		for(j=0;j<nbElem;j++){			if(matrix[i][j] == 2){				fprintf(placement,"R");				ligneNOK = ligneNOK && 0;			}			else fprintf(placement,"%d",matrix[i][j]);		}		fprintf(placement,"\n");		//fprintf(placement," %d ",nbPlaces(matrix,i,nbElem));		//if(ligneNOK) fprintf(placement,"erreur !\n");		//else fprintf(placement,"ok\n");		ligneNOK = 1;	}	fprintf(placement,"\n\n%d reines plac閑s avec succ鑣.\n",nbElem);	fclose(placement);	}void affTexte(int nbElem, int **matrix){	int i,j,k;	char temp;		for(i=0;i<nbElem;i++){		for(j=0;j<nbElem;j++){			if(matrix[i][j] == 2) printf("R");			else printf("%d",matrix[i][j]);		}		printf("\n");	}	//printf("Appuyer sur une touche pour continuer...\n");	scanf("%c",&temp);}int bienPlacee(int **matrix, int x, int y, int nbReines){//fonction booleenne	int i;	for(i=0;i<nbReines;i++) if((matrix[i][y] == 2)||(matrix[x][i] == 2)) return 0;	i=0;	while(((x-i)>=0)&&((y-i)>=0)){		if(matrix[x-i][y-i] == 2) return 0;		i++;	}	i=0;	while(((x+i)<nbReines)&&((y+i)<nbReines)){		if(matrix[x+i][y+i] == 2) return 0;		i++;	}	i=0;	while(((x+i)<nbReines)&&((y-i)>=0)){		if(matrix[x+i][y-i] == 2) return 0;		i++;	}	i=0;	while(((x-i)>=0)&&((y+i)<nbReines)){		if(matrix[x-i][y+i] == 2) return 0;		i++;	}	return 1;}int verifPlacement(int **matrix, int nbReines){	int i,j;	int compt = 0;	for(i=0;i<nbReines;i++){		for(j=0;j<nbReines;j++){			if(matrix[i][j] == 2){				matrix[i][j] = 0;				if(!bienPlacee(matrix,i,j,nbReines)) return 0;				matrix[i][j] = 2;				compt++;			}		}	}	return (compt == nbReines);}int conflit(int i1, int j1, int i2, int j2){	return (i1 == i2) || (j1 == j2) || ((abs(i1-i2)) == (abs(j1-j2)));}	int compatible(int i, int j){	int k;	int comp;//booleen		comp = 1;	k = 0;	while(comp && (k<i)){		comp = !conflit(i,j,k,place[k]);		k = k+1;	}	return comp;}void reines(int i){	if(i>=nbReines){		char temp;		nbSolution++;		printf("%d solution(s) trouvee(s).\n",nbSolution);		if((reponse=='o')||(reponse=='O')){			int j,k;			for(k=0;k<nbReines;k++){				for(j=0;j<nbReines;j++) echiquier[k][j] = 0;			}				for(j=0;j<nbReines;j++) echiquier[j][place[j]] = 2;			affTexte(nbReines,echiquier);			afficherDansFic2(nbReines,echiquier);			if(verifPlacement(echiquier,nbReines)) printf("placement OK\n");			else printf("mauvais placement !");			printf("Afficher la solution suivante a l'ecran ?\n");			scanf("%c",&reponse);		}	}else{		int j;		for(j=0;j<nbReines;j++){			if(compatible(i,j)){				place[i] = j;				reines(i+1);			}		}	}}int main(int argc,char *argv[]){int i,j,k;printf("Combien de Reines desirez-vous placer ? ");scanf("%d",&nbReines);scanf("%c",&reponse);echiquier=(int**)calloc(nbReines,sizeof(int*));place=(int*)calloc(nbReines,sizeof(int));for(i=0;i<nbReines;i++){	place[i]=0;	echiquier[i]=(int*)calloc(nbReines,sizeof(int));	for(j=0;j<nbReines;j++) echiquier[i][j] = 0;}printf("Afficher la solution suivante a l'ecran ?\n");scanf("%c",&reponse);reines(0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -