我是开了个六维的数组来记住它的每一步
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cctype> 5 #include <cmath> 6 #include <time.h> 7 #include <string> 8 #include <map> 9 #include <stack> 10 #include <set> 11 #include <queue> 12 #include <vector> 13 #include <algorithm> 14 #include <iostream> 15 using namespace std; 16 typedef long long ll; 17 typedef pair<int,int> P; 18 #define PI acos( -1.0 ) 19 const double E = 1e-8; 20 const int INF = 0x7fffffff; 21 22 int a[10][10], b[10]; 23 int dir[][2] = { 0, -1, 0, 1, -1, 0, 1, 0 }; 24 int mark[10][10][10][10][10][10]; 25 int ans = 0; 26 27 void dfs( int x, int y, int num ) 28 { 29 if( num == 7 ) 30 { 31 if( !mark[b[0]][b[1]][b[2]][b[3]][b[4]][b[5]] ) 32 { 33 mark[b[0]][b[1]][b[2]][b[3]][b[4]][b[5]] = 1; 34 ++ans; 35 } 36 return; 37 } 38 for( int k = 0; k < 4; ++k ) 39 { 40 int dx = x + dir[k][0]; 41 int dy = y + dir[k][1]; 42 if( dx >= 0 && dy >= 0 && dy< 5 && dx < 5 ) { 43 b[num] = a[dx][dy]; 44 dfs( dx, dy, num+1 ); 45 } 46 } 47 } 48 49 int main() 50 { 51 for( int i = 0; i < 5; ++i ) 52 for( int j = 0; j < 5; ++j ) 53 scanf( "%d", a[i]+j ); 54 memset( mark, 0, sizeof( mark ) ); 55 for( int i = 0; i < 5; ++i ) 56 for( int j = 0; j < 5; ++j ) { 57 b[0] = a[i][j]; 58 dfs( i, j, 1 ); 59 } 60 printf( "%d ", ans ); 61 return 0; 62 }