N皇后程序:用户是将输入女王位置的行和列的用户 [英] N queen program: the user is the one that will input the row and column of the position of the queen

查看:101
本文介绍了N皇后程序:用户是将输入女王位置的行和列的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个程序中,用户应该是每次迭代时输入女王的行和列的用户,幸运的是我设法做到了。问题是,在女王的每次迭代中,应该有一个函数来检查行和列是否可以放置。如果存在女王,则该功能必须检查垂直,水平和对角线,如果有,则显示有女王存在的信息并将返回女王的当前计数器。这就是我到目前为止所做的。感谢您的帮助。



In this program, the user should be the one to input the row and column of the queen on every iteration and fortunately I managed to do that. The problem is that, on every iteration of the queen, there should be a function that will check whether the row and column is safe to place or not. The function must check the vertical, horizontal and diagonal if there is a queen exist, if so a message will display that a queen exist and will go back on the current counter of the queen. This is what I've done so far. Any help is highly appreciated, Thank You.

#include<stdio.h>
#include<math.h>
int i, j,n;
int a[20][20];
int row, col;
main()
{
	clrscr();
	printf("Enter no. of queen(s): ");
	scanf("%d",&n);

	print_board();

	for(i=1;i<=n;i++)
	{
		printf("\n\nPosition of Queen %d",i);
		printf("\n\tRow: ");
		scanf("%d",&row);
		printf("\tColumn: ");
		scanf("%d",&col);

		if(safeToPlace(row,col) == 1)
		{
			place_Queen(row,col);
		}

	}
	print_Array();


getch();
}

int print_board()
{
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			a[i][j] = '-';
		}
	}

}

int print_Array()
{
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			printf("%c\t",a[i][j]);
		}
	printf("\n\n");
	}
printf("\n");
}

int place_Queen(int row, int col)
{
	a[row][col] = 'Q';
}

/* The function were I got the problem */
int safeToPlace(int row, int col)
{
	int i, j, flag;
	for(i=1;i<=row;i++)
	{
		for(j=1;j<=n;j++)
		{
			int k = abs(row - i);
			int l = abs(col - n);
			if(a[i][col] != 'Q' && a[k][l] != 'Q')
			{
				flag =1;
			}
			else
			{
				flag =0;
				break;
			}
		}
	}
	return flag;
}







我的尝试:



我试着去谷歌,我明白了。但在另一个N皇后计划是不同的,因为行和列必须是用户输入。



What I have tried:

I tried to google it, and I got the point. But on the other N Queen Program is way different because of the row and column must be a user input.

推荐答案

我们不做你的家庭作业。

HomeWork不会在乞求其他人做你的工作时测试你的技能,它将帮助你的老师检查你对所学课程的理解以及你应用它们时遇到的问题。

任何失败都会帮助你的老师发现你的弱点并设定补救措施。

所以,重读你的课程并开始工作。如果您遇到特定问题,请显示您的代码并解释这个问题,我们可以提供帮助。



[更新]

你应该返回非void类型的函数。
We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we may help.

[Update]
You should return something in functions that are not of type void.


你试图一次做三件事,这绝不是一个好主意。



您基本上需要知道三个问题的答案:



同一列中是否有女王?

同一行中是否有一个女王?

对角线上是否有女王?



最好的方法是编写单独的方法那些,所以你的SafeToPlace()可以看起来像:



You're trying to do three things at once which is never a good idea.

You essentially need to know the answer to three questions:

Is there a queen in the same column?
Is there a queen in the same row?
Is there a queen on a diagonal?

The best approach is to write separate methods for those, so your SafeToPlace() can then look something like:

if (QueensInColumn() == 0 && QueensInRow() == 0 && QueensOnDiagonal() == 0)
     return 1;

return 0;





将代码分解成谨慎的元素有很多好处(你可能会在某些时候遇到单一责任原则),其中最重要的是它使它变得很糟糕更容易调试代码。在这个例子中,如果它对于对角线而言仍然是错误的但是对于行和列有效,那么你确切地知道在哪里看,你不会发现自己正在改变已经工作的位。



另一个更容易调试的提示(以及所有更好的代码)是使用更有意义的变量名称。使用a,i,j,k,l等标签会使您的代码难以阅读,难以阅读的代码难以维护。



作为第三点,您似乎尝试将基于1的索引应用于基于0的索引语言,并在此过程中使您自己感到困惑。 0-base所有事情都应该更加清晰。



There are a great many advantages to breaking code down into discreet elements (you will probably come across the Single Responsibility Principle at some point) not least of which is that it makes it an awful lot easier to debug your code. In this example, if it keeps getting it wrong for the diagonal but works for rows and columns, you know exactly where to look and you won't find yourself changing the bits that are already working.

Another hint for easier debugging (and all round better code) is to use more meaningful variable names. Use of labels like a, i, j, k, l etc. make your code very hard to read and code that is hard to read is hard to maintain.

As a third point, you seem to be trying to apply 1-based indexes to a 0-based index language and confusing yourself in the process. 0-base everything and things should get a bit clearer.


这篇关于N皇后程序:用户是将输入女王位置的行和列的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆