赞
踩
import java.util.Scanner; /* 使用递归,上下左右都走,得到最终值 */ public class Main{ public static int k; public static int m; public static int n; public static int res; public static int[][] ints; public static void main(String[] args) { Scanner sc = new Scanner(System.in); m = sc.nextInt(); n = sc.nextInt(); k = sc.nextInt(); ints = new int[m][n]; handle(0,0); System.out.println(res); } public static void handle(int x, int y){ int count = jisuan( x, y); if(count > k || ints[x][y] == 1){ //已经走过的,或者值大于k的直接返回 return; }else { res ++; } ints[x][y] = 1; if(x > 0){ //上 handle( x-1, y); } if(x < m-1){ //下 handle( x+1, y); } if(y > 0){ //左 handle( x, y-1); } if(y < n-1){ //右 handle(x, y+1); } } public static int jisuan(int x, int y){ int a,b,c,d; if(x>=10){ String[] xs = String.valueOf(x).split(""); a = Integer.parseInt(xs[0]); b = Integer.parseInt(xs[1]); }else { a = 0; b = x; } if(y>=10){ String[] ys = String.valueOf(y).split(""); c = Integer.parseInt(ys[0]); d = Integer.parseInt(ys[1]); }else { c = 0; d = y; } return a + b + c + d; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。