是我太菜了!!!又用不来map,set,只能暴力DFS“回去种田”
#include <bits/stdc++.h>
using namespace std;
const int N = 9;
//上下左右
const int dx[] = {0, 0, -1, 1};
const int dy[] = {-1, 1, 0, 0};
int n, m, k;
int a[N][N], res;
vector<int> q;
//判断越界
inline int check(int x, int y){
if(x < 1 || x > n || y < 1 || y > m) return false ;
return true;
}
// 到 (x,y) 时已经走了 t 次, 此时数字为 num
void dfs(int x,int y, int t, int num){
if(t > k){
for(int i = 0; i < q.size(); i++) if(num == q[i]) return ;
q.push_back(num);res ++ ;return ;
}
//四个方向扩展
for(int i = 0; i < 4; i++){
int xx = x + dx[i];
int yy = y + dy[i];
//越界了
if(!check(xx,yy)) continue;
dfs(xx, yy, t + 1, (num*10 + a[xx][yy]));
}
}
int main(){
cin>> n >> m >> k ;
for(int i = 1; i <= n ; i++)
for(int j = 1; j <= m; j++)
cin>> a[i][j];
//任意位置dfs, 初始值都为 0
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
dfs(i, j, 0, 0);
cout<<res<<endl;
return 0;
}