简体   繁体   English

创建一个N x N的字母网格,以便没有两个相邻的字母相同

[英]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. 尝试使用一副纸牌,用两张纸牌代表的叠纸堆来尝试一下,以了解其工作原理。

  1. Create two stacks, A and B. Keep a running index for the number of items in A. 创建两个堆栈,A和B。为A中的项目数保留一个运行索引。
  2. Generate a random number. 生成一个随机数。 This is your Candidate. 这是你的候选人。
  3. If there are a multiple of N items in stack A (you just finished a row) skip to step 6. 如果堆栈A中有N个项目的多个(您刚刚完成了一行),请跳至步骤6。
  4. Pop the top of stack A and store it as Check. 弹出堆栈A的顶部,并将其存储为Check。
  5. Is Check adjacent to Candidate? Check与候选人相邻吗? If yes, push Check back onto stack A, and go back to step 2. (null is non-adjacent) 如果是,则将Check推回堆栈A,然后返回到步骤2。(null不相邻)
  6. Push Check onto stack B. Pop N-2 more items, pushing them onto stack B as you go. 将Check推到堆栈B上。再弹出N-2个项目,然后将它们推到堆栈B上。
  7. Pop the top of stack A and store it as Check. 弹出堆栈A的顶部,并将其存储为Check。 (There should be N-1 items in stack B, and one in Check: a whole row) (堆栈B中应该有N-1个项目,而Check中应有一个:整行)
  8. Is Check adjacent to Candidate? Check与候选人相邻吗? (null is non-adjacent) If yes, push Check back onto stack A, pop each of the items from stack B, pushing onto stack A as you go, and go back to step 2. (null不相邻)如果是,则将Check推回堆栈A,从堆栈B弹出每个项目,边推边推到堆栈A,然后返回到步骤2。
  9. push check back onto stack A. Pop each item from stack B, pushing onto stack A as you go. 将支票推回堆栈A。从堆栈B弹出每个项目,同时将其推入堆栈A。
  10. Push Candidate onto stack A. 将候选者推入堆栈A。
  11. Increment the index. 递增索引。 If we now have NxN items in stack A, we're done: pop everything off stack A, writing them down in an NxN grid as you go. 如果现在堆栈A中有NxN个项目,我们就可以完成:将所有内容从堆栈A中弹出,然后在NxN网格中写下它们。 Otherwise return to step 2. 否则,请返回步骤2。

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.

相关问题 两个字符串的字符串匹配的前n个字母 - String-matching first n letters of two strings 在Java中生成长度为2到N的字符串的排列(给定N个字母) - Generate permutations of strings with lengths 2 to N given N letters in Java 如果在字符串中单词中出现的相邻字母相同,则打印“相同”,否则打印“ diff” - If in a String adjacent letters occurring in the word are same then print “same” otherwise “diff” 如何随机打印 10 个带有“n”个字母的字符串,但每个字符串都有相同的第一个字母 - How would one be able to randomly print 10 strings with 'n' letters, but each string has the same first letter 正则表达式恰好匹配n次出现的字母和m次出现的数字 - Regex to match exactly n occurrences of letters and m occurrences of digits println方法 - 最后2个字母(l&n)代表什么? - println method - what do the last 2 letters (l & n) stand for? 如何检查2个字符串是否具有n个字母的公共子字符串 - How to check if 2 strings have common substrings with n letters 辞书最小排列,以使所有相邻字母都不同 - Lexicographic minimum permutation such that all adjacent letters are distinct 相同的字母 - Letters having the same equivalence 如何检查两个字符串是否具有相同的字母,但只打印一次常见的字母? - How can I check if two strings have the same letters, but only print the common letters once?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM