Skip to content

Instantly share code, notes, and snippets.

@pocari
Last active December 19, 2015 17:18
Show Gist options
  • Save pocari/5989685 to your computer and use it in GitHub Desktop.
Save pocari/5989685 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.List;
public class Haisen {
private static final int ROW = 3;
private static final int COL = 5;
private static void printAnswer(List<Integer> cols) {
for (Integer i : cols) {
System.out.print(i + " ");
}
for (int i = 0; i < COL - cols.size(); i++) {
System.out.print("0 ");
}
System.out.println();
printPattern(cols);
}
private static void search(List<Integer> cols, int num) {
if (num == 0) {
printAnswer(cols);
} else {
for (int i = 1; i <= ROW; i++) {
if ((cols.size() < COL) && (num - i >= 0)) {
List<Integer> added = new ArrayList<Integer>(cols);
added.add(i);
search(added, num - i);
}
}
}
}
private static void printPattern(List<Integer> pattern) {
for (int r = 0; r < ROW; r++) {
for (int c = 0; c < COL; c++) {
if (pattern.size() <= c) {
System.out.print("□");
} else if (r < pattern.get(c)) {
System.out.print("■");
} else {
System.out.print("□");
}
}
System.out.println("");
}
System.out.println("");
}
public static void main(String[] args) {
for (int i = 1; i <= ROW * COL; i++) {
System.out.println(i + "個塗るパターン------------------------------");
search(new ArrayList<Integer>(), i);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment