机器人塔 第七届蓝桥杯javaA组

来源:互联网 发布:手机找不到网络连接 编辑:程序博客网 时间:2024/05/16 11:37
package 第七届试题;import java.util.Scanner;public class 机器人塔 {public static int m;public static int n;public static char map[][];public static int count = 0;public static void main(String[] args) {Scanner scanner = new Scanner(System.in);m = scanner.nextInt();n = scanner.nextInt();scanner.close();int sum = 0;int high = 0;for (int i = 1; sum < m + n; i++) {sum += i;if (sum == m + n) {high = i;break;}}System.out.println("high=" + high);map = new char[high][high];dfs(high - 1, 0);}public static void dfs(int high, int po) {if (po > high) {if (up(high - 1, m, n)) {count++;for (int i = 0; i <= high; i++) {for (int j = 0; j <= high; j++) {System.out.print(map[i][j]);}System.out.println();}System.out.println("count=" + count+"   m="+m+"    n="+n);}return;}if (m > 0) {map[high][po] = 'A';m--;dfs(high, po + 1);m++;}if (n > 0) {map[high][po] = 'B';n--;dfs(high, po + 1);n++;}}public static boolean up(int high, int lm, int ln) {if (high < 0)return true;for (int i = 0; i <= high; i++) {if (map[high + 1][i] == map[high + 1][i + 1]) {map[high][i] = 'A';if (lm > 0)lm--;else {return false;}} else {map[high][i] = 'B';if (ln > 0)ln--;else {return false;}}}if (up(high - 1, lm, ln))return true;elsereturn false;}}

0 0
原创粉丝点击