比走迷宫还简单的dfs,直接暴力搜索完事,判断一个边界就行,可以走重复的路也省了一个标记数组的开销
ac代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _for(i, a, b) for(int i = a; i <= b; i++)
int n, m, k;
const int N = 10;
int a[N][N];
int dict[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
set<int> st;
void dfs(int x, int y, int step, int res, int cur){
res = res * 10 + cur;
if(step == k){
st.insert(res);
return;
}
for(int i = 0; i < 4; i++){
int tx = x + dict[i][0];
int ty = y + dict[i][1];
if(tx > n || tx < 1 || ty > m || ty < 1){
continue;
}
dfs(tx, ty, step+1, res, a[tx][ty]);
}
}
int main(){
cin>>n>>m>>k;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin>>a[i][j];
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
dfs(i,j,0,0,a[i][j]);
}
}
cout<< st.size() <<endl;
return 0;
}