C中的骑士国际象棋解决方案

时间:2015-11-21 17:50:00

标签: c solution chess

我必须在C中创建一个程序,它将检查国际象棋棋盘中每个可能起点的骑士问题解决方案(国际象棋)。遗憾的是,在写完所有内容后,它将无法编译,经过长时间的搜索后,我无法找到任何解决方案。

#include <stdio.h>
void print(int **ruchytab,int size);
void zewnetrzne(int size);
int knight(int **ruchytab,int x,int y,int ktory,int size);
int move(int **ruchytab,int x,int y,int wariant,int *newx,int *newy,int size);
int main()
{
    int size=5;
    zewnetrzne(size);
    return 0;
}
void print(int **ruchytab,int size)
{
    for(int i=0;i<size;i++)
    {
        for(int j=0;j<size;j++)
            printf("%2.d ",ruchytab[i][j]);
        putchar('\n');
    }
}
void zewnetrzne(int size)
{
    int ruchytab[size][size];
    for(int j=0;j<size;j++)          //cleaning tab
    {
        for(int i=0;i<size;i++)
            ruchytab[j][i]=0;
    }
    for(int a=0;a<size;a++)          //diffrent start points
        for(int b=0;b<size;b++)
        {
            knight(ruchytab,a,b,1,size);
            for(int j=0;j<size;j++)  //cleaning tab
            {
                for(int i=0;i<size;i++)
                    ruchytab[j][i]=0;
            }
        }
}
int knight(int **ruchytab,int x,int y,int ktory,int size)
{
    int newx,newy;
    ruchytab[x][y]=ktory;
    if(ktory>=size*size) //we have only n^2 possible moves ,we have to be everywhere only once
    {
        print(ruchytab,size);
        return 1;
    }
    else
    {
        for(int war=1;war<=8;war++)
            if(move(ruchytab,x,y,war,&newx,&newy,size)==1)
                if(knight(ruchytab,newx,newy,ktory+1,size)==1)
                    return 1;
    }
    return 0;
}
int move(int **ruchytab,int x,int y,int wariant,int *newx,int *newy,int size)
{
    switch(wariant)         //8 diffrent moves
    {
        case 1:
            *newx=x-1;
            *newy=y-2;
            break;
        case 2:
            *newx=x+1;
            *newy=y-2;
            break;
        case 3:
            *newx=x+2;
            *newy=y-1;
            break;
        case 4:
            *newx=x+2;
            *newy=y+1;
            break;
        case 5:
            *newx=x+1;
            *newy=y+2;
            break;
        case 6:
            *newx=x-1;
            *newy=y+2;
            break;
        case 7:
            *newx=x-2;
            *newy=y+1;
            break;
        case 8:
            *newx=x-2;
            *newy=y-1;
            break;
    }
    if(*newx>=0 && *newx <size && *newy>=0 && *newy<size && ruchytab[*newx][*newy]==0) //checking if the move is possible and if the place was visited already
        return 1;
    else
        return 0;
}

2 个答案:

答案 0 :(得分:1)

几年前复制并通过它相同的作业:)我还有它

int checkAllTheQueenTillNow(queen_t queens[] , int length)
{
int i;
for(i = 0 ; i < length - 1 ; i++)
{

    if(canPlaceQueen(queens[i] , queens[length-1]) == 0)
    {
        return 0;
    }

}

return 1;

}

  int canPlaceQueen(queen_t queen1 , queen_t queen2)
{
    int i;
    for(i = 0 ; queen1.col - i > 0 || queen1.col + i < COLS || queen1.row - i > 0 || queen1.row + i < ROWS ; i++)
{
    if(((queen1.row + i == queen2.row) &&(queen1.col == queen2.col))     ||
       ((queen1.row - i == queen2.row) &&(queen1.col == queen2.col))     ||
       ((queen1.row == queen2.row) &&(queen1.col + i == queen2.col))     ||
       ((queen1.row == queen2.row) &&(queen1.col - i == queen2.col))     ||
       ((queen1.row + i == queen2.row) &&(queen1.col + i == queen2.col)) ||
       ((queen1.row + i == queen2.row) &&(queen1.col - i == queen2.col)) ||
       ((queen1.row - i == queen2.row) &&(queen1.col + i == queen2.col)) ||
       ((queen1.row - i == queen2.row) &&(queen1.col - i == queen2.col)))
    {
        return 0;
    }

}

return 1;

}

n <- length(unique(my.data.frame$Region)) # n is how many regions you have.

all.percents <- vector()  
for(i in 1:n){  # For every region
  percent <- vector()
  new.data.frame <- my.data.frame[my.data.frame$Region==i, ]  #Make a dataframe with just that region
  for(j in 1:dim(new.data.frame)[1]){
    percent[j] <- new.data.frame$area[j]/sum(new.data.frame$area) # and calculate the percent for each land cover type.
  }
  all.percents <- c(all.percents, percent)  # combine the vectors of percents for all of the different regions
}

my.data.frame$percent <- all.percents # put the vector of percents back into your dataframe. 

}

答案 1 :(得分:1)

您无法传递数组:

int ruchytab[size][size];

接受int**指针的函数。 N维数组在内部表示为指向size * size整数的连续内存块的指针(类似于1维数组)。

为了使您的代码工作,您需要更改函数签名以接受int*,并按如下方式线性化2-D数组访问:

void print(int *ruchytab,int size) {
   ...
   ruchytab[i*size + j];
}

如果你想保留[] []语法,有一些方法可以实现,你可以阅读相关问题中的那些。