鏈哄櫒浜虹殑娲诲姩鑼冨洿
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鏈哄櫒浜虹殑娲诲姩鑼冨洿相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/next' title='next'>next for 澶氬皯 i++ max void res pre
鍦颁笂鏈変竴涓猰琛屽拰n鍒楃殑鏂规牸銆備竴涓満鍣ㄤ汉浠庡潗鏍?,0鐨勬牸瀛愬紑濮嬬Щ鍔紝姣忎竴娆″彧鑳藉悜宸︼紝鍙筹紝涓婏紝涓嬪洓涓柟鍚戠Щ鍔ㄤ竴鏍硷紝浣嗘槸涓嶈兘杩涘叆琛屽潗鏍囧拰鍒楀潗鏍囩殑鏁颁綅涔嬪拰澶т簬k鐨勬牸瀛愩€?渚嬪锛屽綋k涓?8鏃讹紝鏈哄櫒浜鸿兘澶熻繘鍏ユ柟鏍硷紙35,37锛夛紝鍥犱负3+5+3+7 = 18銆備絾鏄紝瀹冧笉鑳借繘鍏ユ柟鏍硷紙35,38锛夛紝鍥犱负3+5+3+8 = 19銆傝闂鏈哄櫒浜鸿兘澶熻揪鍒板灏戜釜鏍煎瓙锛?/p>
鍥炴函锛?/p>
private int[][] next = 0, -1, 0, 1, -1, 0, 1, 0; private int count = 0; private int rows; private int cols; private int threshold; private int[][] digitSum; public int movingCount(int threshold, int rows, int cols) this.rows = rows; this.cols = cols; this.threshold = threshold; boolean[][] marked = new boolean[rows][cols]; initDigitSum(); dfs(marked, 0, 0); return count; private void dfs(boolean[][] marked, int r, int c) if (r < 0 || r >= rows || c < 0 || c >= cols || marked[r][c]) return; marked[r][c] = true; if (this.digitSum[r][c] > threshold) return; count++; for (int[] n : digitSum) dfs(marked, r + n[0], c + n[1]); private void initDigitSum() int[] digitSumOne = new int[Math.max(rows, cols)]; for (int i = 0; i < digitSumOne.length; i++) int n = i; while (n > 0) digitSumOne[i] += n % 10; n /= 10; this.digitSum = new int[rows][cols]; for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) this.digitSum[i][j] = digitSumOne[i] + digitSumOne[j];
以上是关于鏈哄櫒浜虹殑娲诲姩鑼冨洿的主要内容,如果未能解决你的问题,请参考以下文章