码迷,mamicode.com
首页 > 编程语言 > 详细

解决二维数组矩阵闭环数字的替换

时间:2016-05-12 16:03:28      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

如下,存在矩阵形如:

0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0
0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0
0 1 0 1 1 0 0 0 0 9 0 0 0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0
0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0
0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0
0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 1 1 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

其中1将0包围成一个闭合的环,要求将改环中的0替换成2。

解决思路如图:

最终代码:

package date0510.pm.数组闭环替换;

import java.util.ArrayList;
import java.util.List;

public class ResplaceNum {
    private static final int ARRAY[][] = {
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0},
            {0, 1, 0, 1, 1, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0},
            {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
            {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0},
            {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0},
            {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0},
            {0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
            {0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    };
    private static int biNum;


    public static void main(String[] args) {
        StringBuffer nc = new StringBuffer();
        int[][] nums = ARRAY.clone();//比较复杂的逻辑,防止数据出错不能挽回

        System.out.println("矩阵初始:\n");
        //初始矩阵打印
        printlnArrays(nums);

        for (int i = 0; i < nums.length; i++) {
            int[] hang = nums[i];

            for (int j = 0; j < hang.length; j++) {
                int ge = hang[j];
                boolean topFlag = false;
                boolean buttomFlag = false;
                boolean leftFlag = false;
                boolean rightFlag = false;

                if (ge == 0) {
                    //判断上下方
                    for (int k = 0; k < nums.length; k++) {
                        if(nums[k][j] == 1 ){
                            if (k < i ) {
                                topFlag = true;
                            }else if (k > i) {
                                buttomFlag = true;
                            }
                        }
                    }
                    //判断前后方
                    for (int l = 0;  l < hang.length; l++) {
                        if (nums[i][l] == 1) {
                            if (l<j) {
                                leftFlag = true;
                            }else if (l>j) {
                                rightFlag = true;
                            }
                        }
                    }

                    if (leftFlag && rightFlag && topFlag && buttomFlag) {
                        hang[j] = 2;

                    }

                }

            }

        }

        /*
         * 第一次筛选完毕,输出数组
         */
        System.out.println("第一次筛选结果:\n");
        printlnArrays(nums);

        /*
         * 开始第二次筛选
         */

        boolean beihighFlag = true;
        boolean frontFlag = true;
        for (int j = 0; j < nums.length; j++) {
            int[] hang = nums[j];
            for (int k = 0; k < hang.length; k++) {
                int ge = hang[k];

                if (ge == 2) {
                    //遍历该行,右边离1最近部分存在0,说明没有闭合
                    for (int l = k; l < hang.length; l++) {
                        int ls = hang[l];
                        if (ls == 0) {
                            beihighFlag = false;
                            break;
                        }
                        if (ls == 1) {
                            break;
                        }
                    }
                    //遍历该行,左边离1最近部分存在0,也说明没有闭合
                    for (int l = k; l > 0; l--) {
                        int ls= hang[l];
                        if (ls == 0) {
                            frontFlag = false;
                            break;
                        }
                        if (ls == 1) {
                            break;
                        }

                    }

                }

            }
        }

        System.out.println("筛选结果:\n");
        if (frontFlag == false || beihighFlag == false) {
            System.out.println("可能至少存在一个环未闭合");
            //此处应该遍历数组,将所有改成2的值变回来——0,因使用了数组克隆,所以偷懒了
            return;
        }
            printlnArrays(nums);
        }


    private static void printlnArrays(int[][] nums){
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < nums.length; i++) {
            int[] hang = nums[i];
            for (int j = 0; j < hang.length; j++) {
                int ge = hang[j];
                if (ge == 2) {//改动输出,将2变成,以便更醒目

                    sb.append("② ");
                }
                else {
                    sb.append(ge+" ");                  
                }
            }
            sb.append("\n");
        }
        System.out.println(sb);

    }
}

运行结果:
技术分享
技术分享

仍然存在的问题:
(1)没有判断多个圆环的情况。
(2)如果有一个圆环,但同时也有其他未闭合的环,那么久不能愉快的玩耍了。。。

也就是复杂一点的就不对头了……
增加条件继续判断或许可行。

思路是分几次筛选,第一次遍历数组,如果某个位置的值为0并且上下左右都存在一个1(循环遍历一行和一列),替换为2。第一次筛选后可能仍然不合理,无法应对未闭合的情况。第二次筛选则对为闭合的进行处理:第一筛选时确保值为2的位置前后上下被1包围,遍历数组,当值为2时,其到离他最近的1是否存在0,只要某个值为2的地方存在这种情况,圆环就一定未闭合。

问题来自:http://bbs.csdn.net/topics/391948572?page=1#post-401127140

解决二维数组矩阵闭环数字的替换

标签:

原文地址:http://blog.csdn.net/mingyueyixi/article/details/51366051

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!