首页 > 专题系列 > Java解POJ > POJ 3752 字母旋转游戏 [解题报告] Java
2013
11-13

POJ 3752 字母旋转游戏 [解题报告] Java

字母旋转游戏

问题描述 :

给定两个整数M,N,生成一个M*N的矩阵,矩阵中元素取值为A至Z的26个字母中的一个,A在左上角,其余各数按顺时针方向旋转前进,依次递增放置,当超过26时又从A开始填充。例如,当M=5,N=8时,矩阵中的内容如下:

   A   B   C   D   E   F   G   H

   V   W   X   Y   Z   A   B   I

   U   J   K   L   M   N   C   J

   T   I   H   G   F   E   D   K

   S   R   Q   P   O   N   M   L

 

输入:

M为行数,N为列数,其中M,N都为大于0的整数。

输出:

分行输出相应的结果

样例输入:

4 9

样例输出:

   A   B   C   D   E   F   G   H   I
   V   W   X   Y   Z   A   B   C   J
   U   J   I   H   G   F   E   D   K
   T   S   R   Q   P   O   N   M   L

解题代码:

import java.io.BufferedInputStream;   
import java.util.Scanner;   
  
/**  
 *  
 *poj3752  
 * 模拟  
 * @author NC  
 */  
public class Main {   
  
    public static void main(String[] args) {   
        Scanner scan = new Scanner(new BufferedInputStream(System.in));   
        if (scan.hasNext()) {   
            int m = scan.nextInt();   
            int n = scan.nextInt();   
            int num = m * n;   
            int[][] letter = new int[m][n];   
            for (int i = 0; i < letter.length; i++) {   
                for (int j = 0; j < letter[i].length; j++) {   
                    letter[i][j] = -1;   
                }   
            }   
            int i = 0, j = 0;   
            int count = 0;   
            if (m != 1 && n != 1) {   
                while (true) {   
                    if (letter[i][j] != -1) {   
                        break;   
                    }   
                    for (int k = 0; k < n; k++) {   
                        letter[i][j + k] = count;   
                        count++;   
                    }   
                    j = j + n - 1;   
                    if (m != 1) {   
                        for (int k = 1; k < m - 1; k++) {   
                            letter[i + k][j] = count;   
                            count++;   
                        }   
                        i = i + m - 1;   
  
                        for (int k = 0, c = 0; c < n; k--, c++) {   
                            letter[i][j + k] = count;   
                            count++;   
                        }   
                        j = j - n + 1;   
                        for (int k = -1, c = 1; c < m - 1; c++, k--) {   
                            letter[i + k][j] = count;   
                            count++;   
                        }   
                        i = i - m + 2;//这里不一样   
                    }   
                    j = j + 1;   
                    m = m - 2;   
                    n = n - 2;   
                }   
            } else if (m == 1 && n == 1) {   
                letter[0][0] = 0;   
            } else if (m == 1) {   
                for (int k = 0; k < n; k++) {   
                    letter[0][k] = k;   
                }   
            } else {   
                for (int k = 0; k < m; k++) {   
                    letter[k][0] = k;   
                }   
            }   
            for (int k = 0; k < letter.length; k++) {   
                for (int v = 0; v < letter[k].length; v++) {   
                    char ch = (char) (letter[k][v] % 26 + 'A');   
                    System.out.print("   " + ch);   
                }   
                System.out.println();   
            }   
        }   
  
    }   
}

  1. public static void main(String[] args) { Scanner scan = new Scanner(new BufferedInputStream(System.in)); if (scan.hasNext()) { int m = scan.nextInt(); int n = scan.nextInt(); char a = ‘A’; for (int i = 0; i < m; i++) { List line = new ArrayList(); for (int j = 0; j < n; j++) { line.add(String.valueOf((char) (a + (i * m + j) % 26))); } System.out.println(String.join(” “, line)); } } }

    • public static void main(String[] args) { Scanner scan = new Scanner(new BufferedInputStream(System.in)); if (scan.hasNext()) { int m = scan.nextInt(); int n = scan.nextInt(); char a = ‘A’; for (int i = 0; i < m; i++) { List line = new ArrayList(); for (int j = 0; j < n; j++) { line.add(String.valueOf((char) (a + (i * n + j) % 26))); } System.out.println(String.join(” “, line)); } } }

  2. 老实说,这种方法就是穷举,复杂度是2^n,之所以能够AC是应为题目的测试数据有问题,要么数据量很小,要么能够得到k == t,否则即使n = 30,也要很久才能得出结果,本人亲测