蓝桥杯国赛白给记

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

持续更新ing

最后一道填空题:

hp DFS

#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终止

原文地址:https://www.cnblogs.com/lightac/p/13973226.html