#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
int path(int rows,int cols,int k) {
if (rows <= 0 || cols <= 0 || k <= 0) return 0;
vector<int> v(100);
return move(rows, cols, k,0,0,0,v);
}
int move(int rows, int cols, int k,int row,int col,int sum, vector<int> &v) {
if (row>=0&&row<rows&&col>=0&&col<cols&&check(row, col, k, v)&&!v[row*cols + col]) {
v[row*cols + col] = 1;
sum = 1 + move(rows, cols, k, row + 1, col, sum, v) + move(rows, cols, k, row - 1, col, sum, v)
+ move(rows, cols, k, row, col + 1, sum, v) + move(rows, cols, k, row, col - 1, sum, v);
}
else return 0;
return sum;
}
bool check(int row,int col,int k, vector<int> &v) {
int s = 0;
while (row){
s += row % 10;
row /= 10;
}
while (col)
{
s += col % 10;
col /= 10;
}
return s <= k;
}
};
int main() {
Solution s;
cout << s.path(10, 10, 8) << endl;
return 0;
}
剑指offer13
最新推荐文章于 2023-12-22 20:49:02 发布