首页 > 专题系列 > Java解POJ > POJ 3768 Repeater [解题报告] Java
2013
11-13

POJ 3768 Repeater [解题报告] Java

Repeater

问题描述 :

Harmony is indispensible in our daily life and no one can live without it—-may be Facer is the only exception. One day it is rumored that repeat painting will create harmony and then hundreds of people started their endless drawing. Their paintings were based on a small template and a simple method of duplicating. Though Facer can easily imagine the style of the whole picture, but he cannot find the essential harmony. Now you need to help Facer by showing the picture on computer.

You will be given a template containing only one kind of character and spaces, and the template shows how the endless picture is created—-use the characters as basic elements and put them in the right position to form a bigger template, and then repeat and repeat doing that. Here is an example.


# #
# <-template
# #

So the Level 1 picture will be


# #
#
# #

Level 2 picture will be



# # # #
# #
# # # #
# #
#
# #
# # # #
# #
# # # #

输入:

The input contains multiple test cases.
The first line of each case is an integer N, representing the size of the template is N*N (N could only be 3, 4 or 5).
Next N lines describe the template.
The following line contains an integer Q, which is the Scale Level of the picture.
Input is ended with a case of N=0.
It is guaranteed that the size of one picture will not exceed 3000*3000.

输出:

For each test case, just print the Level Q picture by using the given template.

样例输入:

3
# #
 # 
# #
1
3
# #
 # 
# #
3
4
 OO 
O  O
O  O
 OO 
2
0

样例输出:

# #
 # 
# #
# #   # #         # #   # #
 #     #           #     # 
# #   # #         # #   # #
   # #               # #   
    #                 #    
   # #               # #   
# #   # #         # #   # #
 #     #           #     # 
# #   # #         # #   # #
         # #   # #         
          #     #          
         # #   # #         
            # #            
             #             
            # #            
         # #   # #         
          #     #          
         # #   # #         
# #   # #         # #   # #
 #     #           #     # 
# #   # #         # #   # #
   # #               # #   
    #                 #    
   # #               # #   
# #   # #         # #   # #
 #     #           #     # 
# #   # #         # #   # #
     OO  OO     
    O  OO  O    
    O  OO  O    
     OO  OO     
 OO          OO 
O  O        O  O
O  O        O  O
 OO          OO 
 OO          OO 
O  O        O  O
O  O        O  O
 OO          OO 
     OO  OO     
    O  OO  O    
    O  OO  O    
     OO  OO     

解题代码:

import java.util.Scanner;   
  
public class Main {   
  
    public static void main(String[] args) {   
        Scanner sc = new Scanner(System.in);   
        while (sc.hasNextInt()) {   
            int n = sc.nextInt();   
            if (n == 0)   
                break;   
            sc.nextLine();   
            String[] template = new String[n];   
            for (int i = 0; i < n; ++i) {   
                String s = sc.nextLine();   
                template[i] = new String(s);   
            }   
            // sc.nextLine();   
            // sc.nextLine();   
            int level = sc.nextInt();   
            copy(template, template, level);   
        }   
    }   
  
    private static void copy(String[] template1, String[] template2, int level) {   
        --level;   
        if (level > 0) {   
            int n1 = template1.length;   
            int n2 = template2.length;   
            String[] tmpTemplate = new String[n1 * n2];   
            int k = 0;   
            String spaceS = spaceStr(n2);   
            for (int i = 0; i < n1; ++i) {   
                for (int j = 0; j < n1; ++j) {   
                    k = i * n2;   
                    if (template1[i].charAt(j) == ' ') {   
                        for (int x = 0; x < n2; ++x) {   
                            if (tmpTemplate[k] != null)   
                                tmpTemplate[k] += (spaceS);   
                            else  
                                tmpTemplate[k] = new String(spaceS);   
                            k++;   
                        }   
                    } else {   
                        for (int x = 0; x < n2; ++x) {   
                            if (tmpTemplate[k] != null)   
                                tmpTemplate[k] += template2[x];   
                            else  
                                tmpTemplate[k] = template2[x];   
                            k++;   
                        }   
                    }   
                }   
                // print(tmpTemplate);   
            }   
            copy(template1, tmpTemplate, level);   
        } else  
            print(template2);   
  
    }   
  
    private static String spaceStr(int n) {   
        StringBuilder sb = new StringBuilder();   
        for (int i = 0; i < n; ++i) {   
            sb.append(" ");   
        }   
        return sb.toString();   
    }   
  
    private static void print(String[] template) {   
        int n = template.length;   
        // System.out.println(n);   
        for (int i = 0; i < n; ++i) {   
            System.out.print(template[i]);   
            System.out.println();   
        }   
    }   
}