java吧 关注:1,271,860贴子:12,779,624
  • 5回复贴,共1

【java题目】考验你编程能力和算法的时候到了

取消只看楼主收藏回复

木块砌墙题目详情:
用 1×1×1, 1× 2×1以及2×1×1的三种木块,

搭建K × 2^N × 1的墙,不能翻转、旋转(0<=N<=1024,1<=K<=5)

有多少种方案,输出结果对1000000007取模。
举例:
举个例子如给定N=1 K=2
答案是7,如下图所示

摘自CSDN论坛


IP属地:北京1楼2013-05-26 12:37回复
    public class CalculateClass {
    public static int calculate(int n,int k) {
    return 0;
    }
    public static void main(String args[]) {
    }
    }


    IP属地:北京4楼2013-05-26 12:53
    回复
      2025-07-31 06:53:18
      广告
      不感兴趣
      开通SVIP免广告


      IP属地:北京6楼2013-05-26 13:13
      收起回复
        这是我写的,但是算法很复杂,唉,求高手啊
        package com.sky.woodwall;
        import java.util.ArrayList;
        import java.util.Arrays;
        import java.util.HashSet;
        import java.util.List;
        import java.util.Set;
        public class
        CalculateClass{
        //用于填充方块的元素,支持任意图形,1代表此位置有填充物,0代表此处是空的
        private static int[][][] blocks = {
        {{1}},//表示基本方块
        {{1,1}},//表示横着2格长度的方块
        {{1},{1}},//表示竖着2格长度的方块
        //{{1,0,1},{1,1,1}}//表示凹字的方块
        };
        //用于保存每种方案的步骤
        private static List<String> list=new
        ArrayList<String>();
        //根据n和k计算填充方案的方法
        public static int calculate(int n, int k) {
        int[][] wall=new int[k][2*n];
        //开始填充
        run(wall,"");
        //去掉方案中有重复项的方案
        removeDuplicate(list);
        return list.size();
        }
        private static void run(int[][] wall,String
        steps){
        for(int i=0;i<blocks.length;i++){
        //每次循环都必须和参数中的wall一致。
        int[][] annotherWall=copy(wall);
        //进行填充操作并记录步骤
        String step = fill(annotherWall, blocks[i]);
        if(step==null){
        if(isFillFull(annotherWall)) {
        list.add(steps);
        break;
        }
        }else{
        //递归
        run(annotherWall,steps+step+"\n");
        }
        }
        }
        //拷贝二维数组
        private static int[][] copy(int[][] wall){
        int[][] temp=new int[wall.length][];
        for(int i=0;i<wall.length;i++){
        temp[i]=Arrays.copyOf(wall[i],
        wall[i].length);
        }
        return temp;
        }
        //判断是否填满
        private static boolean isFillFull(int[][]
        wall){
        for(int i=0;i<wall.length;i++){
        for(int j=0;j<wall[i].length;j++){
        if(wall[i][j]==0) return false;
        }
        }
        return true;
        }
        //将指定的填充方块填入墙面中
        private static String fill(int[][]
        wall,int[][] block){
        int[] position = getPosition(wall, block);
        if (position[0]==-1) {
        return null;
        }
        String step="将"+sysout(block)+"插入"+sysout(position)+"坐标位置";
        for(int m=0;m<block.length;m++){
        for(int n=0;n<block[m].length;n++){
        if(block[m][n]==1){
        wall[position[0]+m][position[1]+n]=1;
        }
        }
        }
        return step;
        }
        //计算填充的起始点
        private static int[] getPosition(int[][]
        wall,int[][] block){
        int[] startPosition={-1,-1};
        for(int i=0;i<wall.length;i++){
        for(int j=0;j<wall[i].length;j++){
        if(wall[i][j]==1) continue;
        boolean error=false;
        for(int m=0;m<block.length;m++){
        for(int n=0;n<block[m].length;n++){
        if(block[m][n]==1 &&
        (i+m)<wall.length && j+n<wall[i+m].length && wall[i+m][j+n]==0){
        if(startPosition[0]==-1){
        startPosition=new int[]{i,j};
        }
        }else{
        error=true;
        break;
        }
        }
        if(error) break;
        }
        if(!error) return startPosition;
        else {
        startPosition=new int[]{-1,-1};
        }
        }
        }
        return startPosition;
        }
        //去除重复的步骤
        private static void
        removeDuplicate(List<String> strList){
        Set<String> set=new
        HashSet<String>();
        for (String str1 : strList) {
        String[] split = str1.split("\n");
        Arrays.sort(split);
        set.add(Arrays.toString(split));
        }
        list=new ArrayList<String>(set);
        }
        private static String sysout(int[][] temp){
        StringBuilder sb=new StringBuilder();
        for (int[] is : temp) {
        sb.append(sysout(is)).append(",");
        }
        return
        sb.length()>0?sb.substring(0,sb.length()-1):sb.toString();
        }
        private static String sysout(int[] is) {
        //System.out.println(Arrays.toString(is));
        return Arrays.toString(is);
        }
        // start 提示:自动阅卷起始唯一标识,请勿删除或增加。
        public static void main(String args[]) {
        int calculate = calculate(1, 2);
        System.out.println("总共的方案一共有"+calculate+"种。");
        for (String string : list) {
        System.out.println(string);
        }
        }
        // end //提示:自动阅卷结束唯一标识,请勿删除或增加。
        }


        IP属地:北京7楼2013-05-26 14:29
        收起回复
          print:
          总共的方案一共有7种。
          [将[1],[1]插入[0, 0]坐标位置, 将[1],[1]插入[0, 1]坐标位置]
          [将[1,1]插入[0, 0]坐标位置, 将[1]插入[1, 0]坐标位置, 将[1]插入[1, 1]坐标位置]
          [将[1,1]插入[1, 0]坐标位置, 将[1]插入[0, 0]坐标位置, 将[1]插入[0, 1]坐标位置]
          [将[1,1]插入[0, 0]坐标位置, 将[1, 1]插入[1, 0]坐标位置]
          [将[1],[1]插入[0, 0]坐标位置, 将[1]插入[0, 1]坐标位置, 将[1]插入[1, 1]坐标位置]
          [将[1],[1]插入[0, 1]坐标位置, 将[1]插入[0, 0]坐标位置, 将[1]插入[1, 0]坐标位置]
          [将[1]插入[0, 0]坐标位置, 将[1]插入[0, 1]坐标位置, 将[1]插入[1, 0]坐标位置, 将[1]插入[1, 1]坐标位置]


          IP属地:北京8楼2013-05-26 14:30
          收起回复
            这个帖子怎么又排到第一页了?!
            各位一起想想解决思路啊,比如比较简单的解决方案


            IP属地:北京12楼2013-05-30 14:35
            回复