由于明天我要去上课,请帮忙。我的职责是“创建一个程序,从皇后所在的字段中确定皇后可用的棋盘区域。棋盘以8x8矩阵表示。皇后的位置从键盘输入[字母] [数字]。并将结果保存在字符串中。“
我有点坚持如何继续,我想我需要像8种不同的if。 2为水平,2为垂直和4为对角线,但我不知道该怎么做。
int main()
{
int horizontal,vertical,i,j,current;
char poz;
printf("Enter the horizontal position of queen(A-H)\n");
scanf("%s", &poz);
if(poz=='A')
{
horizontal=1;
}
else if (poz=='B')
{
horizontal=2;
}
else if (poz=='C')
{
horizontal=3;
}
else if (poz=='D')
{
horizontal=4;
}
else if (poz=='E')
{
horizontal=5;
}
else if (poz=='F')
{
horizontal=6;
}
else if (poz=='G')
{
horizontal=7;
}
else if (poz=='H')
{
horizontal=8;
}
printf("Enter the vertical position of queen(1-8)\n");
scanf("%d",&vertical);
int n=8,m=8;
int chess[8][8]={
{1,2,3,4,5,6,7,8},
{1,2,3,4,5,6,7,8},
{1,2,3,4,5,6,7,8},
{1,2,3,4,5,6,7,8},
{1,2,3,4,5,6,7,8},
{1,2,3,4,5,6,7,8},
{1,2,3,4,5,6,7,8},
{1,2,3,4,5,6,7,8},
};
printf("Queen pos is %c-%d",poz,vertical );
return 0;
}
答案 0 :(得分:0)
强烈建议将水平位置和垂直位置的索引都修改为0 ... 7,然后包括:(其中“ r”是行,“ c”是列)
All positions in the column (0..7)/(c) where the queen is placed except (r)/(c)
All positions in the row (r)/(0..7) where the queen is placed except (r)/(c)
然后从皇后位置(r)/(c)开始
all successive positions (r+1)/(c-1) until edge of board
all successive positions (r+1)/(c+1) until edge of board
all successive positions (r-1)/(c-1) until edge of board
all successive positions (r-1)/(c+1) until edge of board
以上是一些'for()'语句,应该相对容易实现。