蓝桥杯国赛白给记

A310电脑就离谱,明年去北京再战吧

持续更新ing

最后一道填空题:

hp DFS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<bits/stdc++.h>
using namespace std;
int N;
bool vis[6][6];
int ans = 0;
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool check() {
    for(int i = 1; i <= N; ++i) {
        for(int j = 1; j <= N; ++j) {
            if(!vis[i][j]) {
                return 0;
            }
        }
    }
    return 1;
}
void DFS(int x, int y) {
    //cout << x << " " << y << endl;
    if(x <= 0 || y <= 0 || x > N || y > N) {
        return;
    }
    if(check()) {
        ans++;
        return;
    }
    for(int i = 0; i < 4; ++i) {
        int xx = x + dir[i][0];
        int yy = y + dir[i][1];
        if(!vis[xx][yy]) {
            vis[xx][yy] = 1;
            DFS(xx, yy);
            vis[xx][yy] = 0;           
        }
    }
}
int main () {
    while(cin >> N) {
        ans = 0;
        memset(vis, 0, sizeof vis);
        for(int i = 1; i <= N; ++i) {
            for(int j = 1; j <= N; ++j) {
                memset(vis, 0, sizeof vis);
                vis[i][j] = 1;
                DFS(i, j);
            }
        }
        cout << ans << endl;       
    }
}

 经验:用notepad配环境或者用命令行直接编译运行(g++ sbA310.cpp -> a.exe)死循环ctrl c终止

 

posted @   LightAc  阅读(140)  评论(0编辑  收藏  举报
返回顶端
点击右上角即可分享
微信分享提示