您的位置:首页 > 其它

poj 3768 解题报告

2010-05-12 19:36 411 查看
好久没做acm的题了,呵呵!最近做了一次poj的月赛,杯具,就只做起了两道题。现在发的这道题是一个模拟题poj3768,来看看这道题的题目意思吧:

就是题目给一个模板串,然后在给个重复的次数level。题意很简单。这道题就是个dfs思想,递归模拟。

我们来看看代码吧:

package poj;

import java.util.Scanner;

public class Main3768 {

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
;
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();
}
}
}


http://acm.pku.edu.cn/JudgeOnline/problem?id=3768
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: