【Java】 劍指offer(12) 機器人的運動範圍

本文參考自《劍指offer》一書,代碼採用Java語言。html

更多:《劍指Offer》Java實現合集  java

題目

  地上有一個m行n列的方格。一個機器人從座標(0, 0)的格子開始移動,它每一次能夠向左、右、上、下移動一格,但不能進入行座標和列座標的數位之和大於k的格子。例如,當k爲18時,機器人可以進入方格(35, 37),由於3+5+3+7=18。但它不能進入方格(35, 38),由於3+5+3+8=19。請問該機器人可以到達多少個格子?git

思路

  與【Java】 劍指offer(11) 矩陣中的路徑相似,也採用回溯法,先判斷機器人可否進入(i,j),再判斷周圍4個格子。這題返回的是int值。github

測試用例面試

  1.功能測試(多行多列矩陣,k爲正數)ide

  2.邊界值測試(矩陣只有一行或一列;k=0)函數

  3.特殊輸入測試(k爲負數)post

完整Java代碼

(含測試代碼,測試代碼引用於:RobotMove.cpp測試

/**
 * 
 * @Description 面試題13:機器人的運動範圍
 *
 * @author yongh
 * @date 2018年9月17日 上午8:33:07
 */

// 題目:地上有一個m行n列的方格。一個機器人從座標(0, 0)的格子開始移動,它
// 每一次能夠向左、右、上、下移動一格,但不能進入行座標和列座標的數位之和
// 大於k的格子。例如,當k爲18時,機器人可以進入方格(35, 37),由於3+5+3+7=18。
// 但它不能進入方格(35, 38),由於3+5+3+8=19。請問該機器人可以到達多少個格子?

public class RobotMove {
	public int movingCount(int threshold, int rows, int cols) {
		if (rows <= 0 || cols <= 0 || threshold < 0)
			return 0;

		boolean[] isVisited = new boolean[rows * cols];
		int count = movingCountCore(threshold, rows, cols, 0, 0, isVisited);// 用兩種方法試一下
		return count;
	}

	private int movingCountCore(int threshold, int rows, int cols, int row, int col, boolean[] isVisited) {
		if (row < 0 || col < 0 || row >= rows || col >= cols || isVisited[row * cols + col]
				|| cal(row) + cal(col) > threshold)
			return 0;
		isVisited[row * cols + col] = true;
		return 1 + movingCountCore(threshold, rows, cols, row - 1, col, isVisited)
				+ movingCountCore(threshold, rows, cols, row + 1, col, isVisited)
				+ movingCountCore(threshold, rows, cols, row, col - 1, isVisited)
				+ movingCountCore(threshold, rows, cols, row, col + 1, isVisited);
	}

	private int cal(int num) {
		int sum = 0;
		while (num > 0) {
			sum += num % 10;
			num /= 10;
		}
		return sum;
	}

	// ========測試代碼=========
	void test(String testName, int threshold, int rows, int cols, int expected) {
		if (testName != null)
			System.out.print(testName + ":");

		if (movingCount(threshold, rows, cols) == expected)
			System.out.println("Passed.");
		else
			System.out.println("Failed.");
	}

	// 方格多行多列
	void test1() {
		test("Test1", 5, 10, 10, 21);
	}

	// 方格多行多列
	void test2() {
		test("Test2", 15, 20, 20, 359);
	}

	// 方格只有一行,機器人只能到達部分方格
	void test3() {
		test("Test3", 10, 1, 100, 29);
	}

	// 方格只有一行,機器人能到達全部方格
	void test4() {
		test("Test4", 10, 1, 10, 10);
	}

	// 方格只有一列,機器人只能到達部分方格
	void test5() {
		test("Test5", 15, 100, 1, 79);
	}

	// 方格只有一列,機器人能到達全部方格
	void test6() {
		test("Test6", 15, 10, 1, 10);
	}

	// 方格只有一行一列
	void test7() {
		test("Test7", 15, 1, 1, 1);
	}

	// 方格只有一行一列
	void test8() {
		test("Test8", 0, 1, 1, 1);
	}

	// 機器人不能進入任意一個方格
	void test9() {
		test("Test9", -10, 10, 10, 0);
	}

	public static void main(String[] args) {
		RobotMove demo = new RobotMove();
		demo.test1();
		demo.test2();
		demo.test3();
		demo.test4();
		demo.test5();
		demo.test6();
		demo.test7();
		demo.test8();
		demo.test9();
	}

}

  

Test1:Passed.
Test2:Passed.
Test3:Passed.
Test4:Passed.
Test5:Passed.
Test6:Passed.
Test7:Passed.
Test8:Passed.
Test9:Passed.
RobotMove

 

收穫

  1.計算數位之和時,要注意數字不必定是十位數,多是百位、千位甚至更多,因此cal()函數別寫成計算十位數的方法了。url

 

更多:《劍指Offer》Java實現合集 

相關文章
相關標籤/搜索