Enum:Hopscotch(POJ 3050)

            

              跳格子

  题目大意:牛像我们一样跳格子,一个5*5的方格,方格有数字,给牛跳5次,可以组成一个6个数字组合字符串,请问能组合多少个字符串?

  题目规模很小,暴力枚举,然后用map这个玩具来检测存不存在就可以了,水题

  

 1 #include <iostream>
 2 #include <functional>
 3 #include <algorithm>
 4 #include <string>
 5 #include <map>
 6 
 7 using namespace std;
 8 
 9 static int matrix[5][5];//
10 static int ans;
11 string s_tmp = "000000";
12 map<string, char>dp;
13 
14 void Search(const int, const int,const int);
15 
16 int main(void)
17 {
18     for (int i = 0; i < 5; i++)
19         for (int j = 0; j < 5; j++)
20             scanf("%d", &matrix[i][j]);
21 
22     for (int i = 0; i < 5; i++)
23         for (int j = 0; j < 5; j++)
24             Search(i, j, 0);
25     cout << ans << endl;
26 
27     return 0;
28 }
29 
30 void Search(const int y, const int x,const int level)
31 {
32     if (level == 6)
33     {
34         if (dp.find(s_tmp) == dp.end())
35         {
36             dp[s_tmp] = 1;
37             ans++;
38         }
39     }
40     else
41     {
42         s_tmp[level] = matrix[y][x] + '0';
43         if (y - 1 >= 0)
44             Search(y - 1, x, level + 1);
45         if (y + 1 < 5)
46             Search(y + 1,x, level + 1);
47         if (x - 1 >= 0)
48             Search(y, x - 1, level + 1);
49         if (x + 1 < 5)
50             Search(y, x + 1, level + 1);
51     }
52 }

  

  这速度差不多了

原文地址:https://www.cnblogs.com/Philip-Tell-Truth/p/4866960.html