2017-12-26 8 views
0

現在、8つのクイーンズ・クイーンズ・クイーンズ・クイーンズ・クイーンズ・クイーンズ・クイーンズ・クイーンズ・クイーンズ・クイーンズ・クイーンズ今、私はプログラムをほとんど作りましたが、私はプログラムを斜めにチェックする方法に固執しています。8クイーンズ・ダイアゴナル・チェック

これは(未完)コードです:

#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 
int check_r_c(int**chess,int*p,int N,int M) 
{ 
    int times=0; 
    for(int i=0; i<N; i++) 
    { 
     times=0; 
     for(int j=0; j<M; j++) 
     { 
      if(chess[i][j] == 1) 
       times++; 

     } 
     if(times != 1) 
     { 
      *p=1; 
      return 1; 
     } 
    } 
    for(int j=0; j<M; j++) 
    { 
     times=0; 
     for(int i=0; i<N; i++) 
     { 
      if(chess[i][j] == 1) 
       times++; 
     } 
     if(times != 1) 
     { 
      *p=1; 
      return 1; 
     } 
    } 
    *p=0; 
    return 0; 
} 
int main() 
{ 
    int N,M; 
    printf("Give the number of rows: \n"); 
    scanf("%d",&N); 
    printf("Give the number of columns: \n"); 
    scanf("%d",&M); 
    int**chess = malloc(N*sizeof(int)); 
    int i,j,ch; 
    int*ptr; 
    ptr=&ch; 
    if(chess==NULL) 
    { 
     printf("\nMemory cannot be allocated\n"); 
     return 1; 
    } 
    for(i=0; i<N; i++) 
    { 
     if((chess[i] = malloc(M*sizeof(int)))==NULL) 
     { 
      printf("\nMemory cannot be allocated\n"); 
      return 1; 
     } 
    } 
    for(int i=0; i<N; i++) 
     for(int j=0; j<M; j++) 
      chess[i][j]= 0; 
    for(int k=0; k<N; k++) 
    { 
     printf("Give the position of the %d queen\n",k+1); 
     scanf("%d",&i); 
     scanf("%d",&j); 
     if(chess[i][j] == 1) 
     { 
      printf("You cant put 2 queens in the same place!!!\n"); 
      return 0; 
     } 
     chess[i][j] = 1; 
    } 
    check_r_c(chess,ptr,N,M); 
    if(ch == 0) 
     printf("Solution is correct!"); 
    else 
     printf("Solution is incorrect!"); 
    for(int i=0; i<N; i++) 
     free(chess[i]); 
    free(chess); 
} 
+0

「私は立ち往生しています」というのは良い問題の説明ではありません。あなたの質問は何ですか?何か試しましたか?どのように動作しませんでしたか? – mkrieger1

答えて

0

Firstableは、すべてのそれらのxとは何ですか? 二次的には、迷惑コードを書いても問題ありませんが、共有している場合は、ほとんどの読者にとって理解しやすいように修正してください。 最後に、あなたの質問に答えるために、それを行うための一つの方法は次のとおりです。

for every cell on the left most column 
while there is a cell in position (column + i, line + i) (i starting at zero) 
if there is a queen count one 
end while 
if count is not one then return false 
end for 
1

置か女王のために対角線チェックするためのロジックは、((pは、qが)に位置するすべての場所を確認するためです私のp + 、q + i)であり、p + iとq + iの両方がボード内にある。負の場合は、(p-i、q-i)を使用します。

関連する問題