AcWing 118. 分形
原题链接
简单
作者:
嫌疑人x
,
2020-03-09 09:10:27
,
所有人可见
,
阅读 633
递归分形
JAVA 代码
import java.io.*;
import java.util.*;
public class Main {
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
tokenizer = null;
}
public boolean hasNext(){
while(tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
return false;
}
}
return true;
}
public String next() {
if(hasNext()) return tokenizer.nextToken();
return null;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {return Long.parseLong(next());}
}
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
Task task = new Task();
task.solve(in, out);
out.flush();
}
static class Task {
int N = 3000;
int[][] p = new int[N + 1][N + 1];
public void solve(InputReader in, PrintWriter out) {
dfs(7, 1,1);
while(in.hasNext()){
int n = in.nextInt();
if(n == -1) {
break;
}
int len = (int)Math.pow(3, n - 1);
for(int i = 1; i <= len; i ++) {
for(int j = 1; j <= len;j ++) {
if(p[i][j] != 0) {
out.print("X");
}else {
out.print(' ');
}
}
out.println();
}
out.println('-');
}
}
public void dfs(int n, int x, int y) {
if(n == 1) {
p[x][y] = 'X';
return;
}
int len = (int)Math.pow(3, n - 2);
dfs(n - 1, x, y);
dfs(n - 1, x, y + 2 * len);
dfs(n - 1, x + 1 * len, y + 1 * len);
dfs(n - 1, x + 2 * len, y);
dfs(n - 1, x + 2 * len, y + 2 * len);
return;
}
}
}