第四届蓝桥杯 振兴中华

来源:互联网 发布:acrobat for mac 2015 编辑:程序博客网 时间:2024/06/08 01:13

就是dfs 刚开始还考虑排除部分结果 后来才发现原来只要向右或向下走 每种走法都可以


public class 振兴中华 {static String s = "从我做起振兴中华";static char[][] data = { { '从', '我', '做', '起', '振' },{ '我', '做', '起', '振', '兴' }, { '做', '起', '振', '兴', '中' },{ '起', '振', '兴', '中', '华' } };static int count = 0;public static void main(String[] args) {f(0,0,0);System.out.println(count);}public static void f(int row ,int col ,int step) {if (step == 7) {count++;}if (data[col][row] != s.charAt(step))return;for (int pos = 0; pos <= 1; pos++) {//pos = 0右 pos = 1下if (pos == 0) {if (row < 4)f(row + 1, col, step + 1);}if (pos == 1) {if (col < 3)f(row, col + 1, step + 1);}}}}


原创粉丝点击