递归算法题1

递归算法题:

// count the block,如果1周围都是0则计数为1,叫一个块,计数为1,如果1与相邻位置的其他数也为1,则这些相邻位置的所有数成为一个block也计数为1.
 // 0,0,1,1
 // 1,0,1,1
 // 1,0,0,0
 // 0,1,0,1

上面的这些数计数为4.

若二维数据所有的数都为1,则计数为1,大家可测试。

package javatest.suanfa;

public class getBlockCnt {

	public static int[][] rang = new int[][] { { 0, 0, 1, 1 }, { 1, 0, 1, 1 },
			{ 1, 0, 0, 0 }, { 0, 1, 0, 1 } };
	// count the block,如果1周围都是0则计数为1,叫一个块,计数为1,如果1与相邻位置的其他数也为1,则这些相邻位置的所有数成为一个block也计数为1.
	// 0,0,1,1
	// 1,0,1,1
	// 1,0,0,0
	// 0,1,0,1
	

	//计数器
	public static int count = 0;

	public static int[][] blockCount(int[][] tempdata, int m, int n) {

		boolean continueflag = false;
		int[][] returnValue = new int[][] { { 0, 0 }, { 0, 0 }, { 0, 0 } };

		for (int i1 = 0; i1 < tempdata.length; i1++) {

			int x = tempdata[i1][0];
			int y = tempdata[i1][1];

			if (rang[x][y] == 1) {

				rang[x][y] = 0;

				if (x + 1 < m && rang[x + 1][y] != 0) {

					returnValue[0][0] = x + 1;
					returnValue[0][1] = y;

					continueflag = true;
				}

				if (y + 1 < n && rang[x][y + 1] != 0) {

					returnValue[1][0] = x;
					returnValue[1][1] = y + 1;

					continueflag = true;

				}

				if (x - 1 >= 0 && rang[x - 1][y] != 0) {

					returnValue[2][0] = x - 1;
					returnValue[3][1] = y;

					continueflag = true;

				}

				if (!continueflag) {

					count++;

				} else {

					blockCount(returnValue, m, n);

				}

			} else {
				return null;
			}

		}

		return null;

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		for (int i1 = 0; i1 < rang.length; i1++) {

			for (int j1 = 0; j1 < rang[i1].length; j1++) {

				System.out.println(i1 + " === i1  " + j1 + " ==j1 ");

				blockCount(new int[][] { { i1, j1 } }, 4, 4);

				System.out.println("count = " + count);

			}
		}

	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值