8个皇后问题需要帮助 [英] 8 queens problem need help

查看:63
本文介绍了8个皇后问题需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在尝试允许用户放置自己的8个皇后,并允许用户回溯。然而,我不能让女王被放置在棋盘上并且没有启动isSafe算法。如果我能得到一些方向会很好,谢谢你。第一次用谷歌搜索问题,但我试图在我自己的第一个尝试这个,不想成为一个懒惰的程序员,只是给我的答案,这将打败我在大学的原因。

currently i am trying to allow the user to place his own set of 8 queens and allows user to backtrack. i cannot however get the queens to be placed on the board and haven't started the isSafe algorithm. if i can get some direction that would be nice thank you. first googled the problem but i'm trying to attempt this on my own first, don't want to be a lazy programmer and just have the answers given to me that would defeat the reason why i'm in college.

#include <iostream>
using namespace std;

void printGrid(char grid[][8], int w, int h);
void printQueen(char grid[][8], int w, int h);

int main(){
	//create a grid 8x8
	char grid[8][8];
	//loop for row
	for (int count =0; count<8;count++)
	{
		//colums 
		for (int count1 =0; count1<8;count1++)
	{
		grid[count][count1];
		}
	}
	//print grid
	
	printGrid(grid,8,8);
	
	system("pause");
	return 0;
}
void printGrid(char grid[][8],int w, int h)
{
	for (int i =0;i<w;i++)>
	{
		for (int j =0;j<h;j++)>
		{
			
			cout<<grid[i][j]<<'_';
		}
		
		cout<<endl;
	}
	printQueen(grid,8,8);
}
void printQueen(char grid[][8], int w, int h)
{
	int j,k;

	for (int i =0;i<7;i++)
	{
	//asks user for cooridinates to place queen doesnt check if queen is safe
      cout<<"enter the cooridinates for the queen (must be integer values between 1-8):"<<endl;
	  cout<<"enter row then colum : ";
	  cin>>j>>k;
	  cout<<grid[j][k]<<'Q';
	  printGrid(grid,8,8);
	}
	
	
}</iostream>

推荐答案

首先你在main()的嵌套循环中尝试了什么?

First of all, what are you attempting in the nested loops in the main()?
The line
grid[count][count1];

doesn'什么都做。

你打算初始化数组:

doesn't do anything.
Did you intend to initialize the array:

grid[count][count1] = ' ';



我不确定你的 printQueen 例程实际上就是你想要的。



而不是从打印板开始,

从解决问题开始。



考虑如何手动完成:

获取(或绘制)棋盘(8x8网格)

get代表女王的8个标记。

获取一些标记来表示很多女王正在攻击一个位置。

(在美国,我会使用8个季度和很多便士。)

在f的第一个位置放置一个女王第一行,然后在女王可以攻击的每个位置放置一个标记。

在第二行的第一个打开位置放置第二个女王。

添加一个标记到第二任女王可以攻击的位置。有些位置会有2个标记!

重复添加皇后,直到你放置第8个皇后或者没有空位。

如果没有未平仓头寸,请删除最后放置女王,从女王那里取下相应的标记,并将女王放在下一个可用位置。

继续放置和移除女王和标记,直到放置第8个。



现在尝试在代码中实现这个。

想想网格数据结构需要什么。


I'm not sure your printQueen routine is actually what you want.

Instead of starting with printing the board,
start with solving the problem.

Think about how you would do it manually:
Get (or draw) a chessboard (8x8 grid)
get 8 markers to represent the queens.
Get some markers to represent HOW MANY queens are attacking a position.
(In the US, I'd use 8 Quarters and a lot of pennies.)
Place a "queen" in the first position of the first row and then place a marker in every position that the queen could attack.
Place a second queen in the first open position of the second row.
Add a marker to the positions the second queen could attack. Some positions will have 2 markers!
repeat adding queens until you've placed the 8th queen or there are no open positions.
If there are no open positions, remove the last placed queen, remove the corresponding markers from that queen, and place the queen in the next available position.
Keep placing and removing queens and markers until the 8th is placed.

Now try to implement this in code.
Think about what the grid data structure needs to be.


你似乎遇到的主要问题是对事件的顺序很清楚:你有自己需要在程序中完成的事情的元素,但是如果你得到的话,你就会穿上鞋子。我的意思。



我强烈建议您查看问题定义并在几个(复杂)步骤中描述它,需要一个接一个地执行。这些步骤中的每一步都代表了您需要编写的功能。然后继续执行每个功能并执行相同的操作:以稍微小一些的步骤描述它们。经过几次迭代后,您将得到可以直接放入程序代码的步骤。



然后对数据结构执行相同的操作:查看函数定义,考虑您需要传递给每个功能的信息。你可能会发现有些人并不需要那些倾向于传递的信息。并且您可能能够识别始终需要一起传递的数据元素,并且有利于在数据结构中进行分组。



在您的情况下,您的目的是来b $ b)从用户输入中读取女王位置

2)评估位置是否解决了问题

3)允许用户回溯

4)重复步骤2)

5)打印位置



以上步骤是你的主要内容功能应该是这样的。第4步意味着某种循环。您应该考虑如何退出该循环,并确保您不会陷入其中。其他步骤只是函数调用。



步骤1):为什么你从用户输入调用函数读取位置printQueen对我来说是一个谜。除此之外,这个函数显示了一些问题:

- 参数w和h的目的是什么?

- 你的for循环有多少次迭代?

- 你真的需要两个输入坐标吗?

- 你真的需要三个变量(i,j,k)吗?

- 为什么每次输入后都打印网格吗?

- 为什么要打印它?这不是打印网格的功能,它是读取位置的功能!

- grid [0] [0],grid [0] [1]等的值是多少?用户输入?



步骤2):正如你所说,你还需要写这个功能



步骤3):有点模糊 - 当'回溯'时,用户究竟应该允许更改什么?答案决定了您与用户沟通的方式。更重要的是,您如何参考目前存储的头寸?更改后如何删除它们?



步骤4):再次遇到正确处理维度信息的问题。

- 你正在使用参数w和h,但是网格的第二维表示为8.这没有意义:如果h大于8,则读过数组的末尾。

- 对printQueen()的调用不使用h和w?

- 为什么你甚至调用printQueen()?这不是此功能的目的!



上面列出的一些问题没有简单的答案。请随意询问您是否无法针对任何个点提出解决方案。
The main problem you seem to be having is being clear about the sequence of events: you have elements of the things that need to be done in your program, but you're putting on socks after the shoes, if you get my meaning.

I strongly suggest you look at the problem definition and describe it in just a few (complex) steps, that need to be performed one after the other. Each of those steps represents a function that you will need to write. Then proceed to each function and do the same: describe them in somewhat smaller steps. After a few iterations you'll arrive at steps that can be put into program code directly.

Then you do the same for the data structures: looking at the function definitions, consider what information you need to pass to each function. You may find that some don't require nearly as much information as are inclined to pass along. And you may be able to recognize data elements that always need to be passed together, and would be advantagous to group in a data structure.

In your case, your purpose is to
1) read queen positions from user input
2) evaluate whether the position solves the problem
3) allow the user to backtrack
4) repeat from step 2)
5) print positions

The above steps are what your main function should look like. Step 4 implies a loop of some kind. You should consider how to exit that loop, and make sure that you don't remain stuck within. The other steps are just function calls.

Step 1): Why you call the function for reading the positions from user input "printQueen" is a mystery to me. Apart from that, this function shows a couple of problems:
- what is the purpose of the arguments w and h?
- how many iterations does your for loop make?
- do you actually need two input coordinates?
- do you actually need three variables (i, j, k)?
- why do you print the grid after every single input?
- why do you print it at all? This is not the function for printing the grid, it's the function for reading positions!
- what is the value of grid[0][0], grid[0][1], etc. after user input?

Step 2): As you said, you still need to write that function

Step 3): is somewhat vague - what exactly should the user be allowed to change when 'backtracking'? The answer determines how you go about communicating with the user. More importantly, how do you refer to the positions currently stored? How do you erase them when changed?

Step 4): again you are having issues for correctly dealing with dimensionality information.
- you are using parameters w and h, but then the second dimension of grid is indicated as 8. This doesn't make sense: if h were greater than 8, you'd read past the end of the array.
- the call to printQueen() doesn't use h and w?
- why do you even call printQueen()? This is not the purpose of this function!

Some of the issues listed above don't have trivial answers. Feel free to ask if you can't come up with a solution to any individual point.


这篇关于8个皇后问题需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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