第八届蓝桥杯省赛C/C++ A组第4题 方格分割

参考了http://blog.csdn.net/y1196645376/article/details/69718192,这个大哥的思路很巧妙。

思路:

dfs。

实现:

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 
 5 const int N = 6;
 6 const int dx[4] = { 0, 1, -1, 0 };
 7 const int dy[4] = { 1, 0, 0, -1 };
 8 
 9 bool vis[6][6];
10 
11 int dfs(int x, int y)
12 {
13     if (x == 0 || x == N || y == 0 || y == N)
14     {
15         return 1;
16     }
17     int cnt = 0;
18     for (int i = 0; i < 4; i++)
19     {
20         int nx = x + dx[i];
21         int ny = y + dy[i];
22         if (nx >= 0 && nx <= N && ny >= 0 && ny <= N && !vis[nx][ny])
23         {
24             vis[nx][ny] = true;
25             vis[N - nx][N - ny] = true;
26             cnt += dfs(nx, ny);
27             vis[nx][ny] = false;
28             vis[N - nx][N - ny] = false;
29         }
30     }
31     return cnt;
32 }
33 
34 int main()
35 {
36     vis[3][3] = true;
37     cout << dfs(3, 3) / 4 << endl;
38     return 0;
39 }
原文地址:https://www.cnblogs.com/wangyiming/p/6686489.html