当前位置:网站首页>[depth first search] Ji Suan Ke: Betsy's trip
[depth first search] Ji Suan Ke: Betsy's trip
2022-07-06 01:57:00 【muse_ age】

DFS: Start to finish , If you happen to leave at the end n*n Step ,ans++;
If you don't prune , Will timeout
prune :
1、 Avoid walking into a dead end
2、 Avoid forming isolated areas

Code :
#include<iostream>
using namespace std;
int n;
int ans=0;
bool vis[8][8];
void dfs(int x,int y,int step){
if(!(x>=0&&y>=0&&x<n&&y<n))return;
if(vis[x][y]==true)return;
if(x==n-1&&y==0){
if(step==n*n-1){
ans++;
}
return;
}
if(x==n-1&&vis[x][y+1]==false&&vis[x][y-1]==false&&y-1>=0&&y+1<n||y==n-1&&vis[x+1][y]==false&&vis[x-1][y]==false&&x-1>=0&&x+1<n){
return;
}
vis[x][y]=true;
dfs(x+1,y,step+1);
dfs(x-1,y,step+1);
dfs(x,y+1,step+1);
dfs(x,y-1,step+1);
vis[x][y]=false;
}
int main(){
cin>>n;
dfs(0,0,0);
cout<<ans;
} Experience : Narrow the scope of , Use it well for debugging
边栏推荐
- Initialize MySQL database when docker container starts
- Selenium element positioning (2)
- leetcode-2. Palindrome judgment
- Kubernetes stateless application expansion and contraction capacity
- Campus second-hand transaction based on wechat applet
- Redis-列表
- Basic operations of databases and tables ----- default constraints
- module ‘tensorflow. contrib. data‘ has no attribute ‘dataset
- 2022年PMP项目管理考试敏捷知识点(8)
- Reasonable and sensible
猜你喜欢

1. Introduction to basic functions of power query

PHP campus movie website system for computer graduation design

Basic operations of databases and tables ----- unique constraints

Social networking website for college students based on computer graduation design PHP

Basic operations of database and table ----- set the fields of the table to be automatically added

Selenium waiting mode

Basic operations of databases and tables ----- non empty constraints

Extracting key information from TrueType font files

UE4 unreal engine, editor basic application, usage skills (IV)

A Cooperative Approach to Particle Swarm Optimization
随机推荐
[understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.
1. Introduction to basic functions of power query
500 lines of code to understand the principle of mecached cache client driver
国家级非遗传承人高清旺《四大美人》皮影数字藏品惊艳亮相!
剑指 Offer 38. 字符串的排列
【SSRF-01】服务器端请求伪造漏洞原理及利用实例
A basic lintcode MySQL database problem
leetcode-2.回文判断
Internship: unfamiliar annotations involved in the project code and their functions
Force buckle 9 palindromes
Accelerating spark data access with alluxio in kubernetes
LeetCode 322. Change exchange (dynamic planning)
module ‘tensorflow. contrib. data‘ has no attribute ‘dataset
[技术发展-28]:信息通信网大全、新的技术形态、信息通信行业高质量发展概览
This time, thoroughly understand the deep copy
Folio. Ink is a free, fast and easy-to-use image sharing tool
Basic operations of databases and tables ----- primary key constraints
Regular expressions: examples (1)
Docker compose configures MySQL and realizes remote connection
剑指 Offer 12. 矩阵中的路径