[英]Creating an N by N grid of letters so that no two adjacent letters are the same
I need to print out a N by N grid containing the letters A to F, so that no two adjacent letters are the same. 我需要打印出一个N到N的网格,其中包含字母A到F,以便没有两个相邻的字母相同。 The code below prints out an N by N grid, however I can only get the letters on the left and right to be different.
下面的代码打印出N x N的网格,但是我只能使左右的字母不同。 I can't find a way to get the letters above and below to be different as well.
我找不到一种使上下字母不同的方法。 I need to solve this problem without the use of arrays.
我需要在不使用数组的情况下解决此问题。 The letters have to be randomized.
字母必须随机。
public static void main(String[] args) {
int N = StdIn.readInt();
for (int column = 0; column < N; column++) {
int x = 0;
for (int row = 0; row < N; row++) {
int c = (int) (Math.random() * 6 + 1);
while (x == c) {
c = (int) (Math.random() * 6 + 1);
}
if (c == 1) {
System.out.print("A ");
}
if (c == 2) {
System.out.print("B ");
}
if (c == 3) {
System.out.print("C ");
}
if (c == 4) {
System.out.print("D ");
}
if (c == 5) {
System.out.print("E ");
}
if (c == 6) {
System.out.print("F ");
}
x = c;
}
System.out.println();
}
I did you the favor of simplifying your for loop a bit 我帮你简化了for循环
for (int row = 0; row < N; row++) {
char c = (char) (Math.random() * 6 + 'A');
while (x == c) {
c = (char) (Math.random() * 6 + 'A');
}
System.out.print(c + " ");
x = c;
}
That uses the ASCII values of the letters so you don't need a big if statement. 它使用字母的ASCII值,因此您不需要大的if语句。
Your question is unclear about what storage formats are allowed, but consider this: If you could store each row as a string (one temporary string that is deleted after moving on a row), how would you check if a letter in the row you're building below it matches the letter above? 您的问题尚不清楚允许使用哪种存储格式,但是请考虑以下问题:如果您可以将每一行存储为一个字符串(在移动一行后将删除一个临时字符串),那么如何检查该行中的字母是否为在它下面重新构建与上面的字母匹配? Think of a string as an array (which is its true form).
将字符串视为数组(这是其真实形式)。
Easy to do with 2 queues. 轻松完成2个队列。
If you analyse the below, you should realise that you really only need one size N linked-list. 如果您分析以下内容,您应该意识到您实际上只需要一个大小为N的链表。
current
is the current row, each new element is simply enqueued. current
是当前行,每个新元素都只是排队。
last
is the previous row. last
是上一行。 It takes the value of current
once you're done with a row, then the first element we want to check is at the front. 完成一行后,它将获取
current
的值,然后我们要检查的第一个元素在最前面。
I separated the first row from the others to make things simpler and more understandable. 我将第一行与其他行分开,以使事情更简单和更容易理解。
int N = 10;
Queue<Character> last,
current = new Queue<Character>();
char prev = '0';
for (int row = 0; row < N; row++)
{
char c;
do { c = (char)(Math.random() * 6 + 'A'); }
while (prev == c);
current.enqueue(c);
prev = c;
System.out.print(c + " ");
}
System.out.println();
for (int col = 1; col < N; col++)
{
last = current;
current = new Queue<Character>();
prev = '0';
for (int row = 0; row < N; row++)
{
char c;
do { c = (char)(Math.random() * 6 + 'A'); }
while (last.peek() == c || prev == c);
current.enqueue(c);
last.dequeue();
prev = c;
System.out.print(c + " ");
}
System.out.println();
}
You can do this with a pair of stacks and a candidate character. 您可以使用一对堆栈和一个候选字符来执行此操作。 The NxN grid is represented by a stack containing up to N times N characters.
NxN网格由最多包含N个N个字符的堆栈表示。 Try this with a deck of cards, with the stacks represented by two spots for stacks of cards, to get a feel for how it works.
尝试使用一副纸牌,用两张纸牌代表的叠纸堆来尝试一下,以了解其工作原理。
Basically, you're drawing a card, and checking up to two items. 基本上,您要绘制一张卡片,并最多检查两个项目。 Because stack A is guaranteed never to have adjacencies in it, once an item is placed, it never has to be removed.
因为保证堆栈A永远不会有邻接,所以一旦放置项目,就永远不必将其删除。 So once you get to the end, you're done.
所以一旦结束,就完成了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.