这道题还是相对来说比较简单的,只需要无脑搜,而且因为是找出所有结果,所以深搜比较好。
#include <iostream>
#include <set>
using namespace std;
set<int> number;
int M[5][5];
int dx[4] = {-1,1,0,0};
int dy[4] = {0,0,-1,1};
void dfs(int len,int cur,int x,int y){
if(len==6){
number.insert(cur);
return;
}
for(int i=0;i<4;i++){
int newx = x + dx[i];
int newy = y + dy[i];
if(newx<0 || newx >=5 || newy <0 || newy>=5)continue;
dfs(len+1,cur*10+M[newx][newy],newx,newy);
}
}
int main(int argc, const char * argv[]) {
int i,j;
for(i=0;i<5;i++){
for(j=0;j<5;j++){
cin>>M[i][j];
}
}
for(i=0;i<5;i++){
for(j=0;j<5;j++){
dfs(0,0,i,j);
}
}
cout<<number.size()<<endl;
return 0;
}