当前位置:网站首页>[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
边栏推荐
- Basic operations of databases and tables ----- default constraints
- Unreal browser plug-in
- 【网络攻防实训习题】
- Basic operations of databases and tables ----- non empty constraints
- [flask] static file and template rendering
- Basic operations of databases and tables ----- unique constraints
- 晶振是如何起振的?
- Computer graduation design PHP college classroom application management system
- Redis key operation
- Bidding promotion process
猜你喜欢

Leetcode skimming questions_ Sum of squares

Redis string type

SPI communication protocol

Publish your own toolkit notes using NPM
![[detailed] several ways to quickly realize object mapping](/img/e5/70c7f8fee4556d14f969fe33938971.gif)
[detailed] several ways to quickly realize object mapping

Executing two identical SQL statements in the same sqlsession will result in different total numbers

3D视觉——4.手势识别(Gesture Recognition)入门——使用MediaPipe含单帧(Singel Frame)和实时视频(Real-Time Video)

It's wrong to install PHP zbarcode extension. I don't know if any God can help me solve it. 7.3 for PHP environment

leetcode-两数之和

Basic operations of database and table ----- delete data table
随机推荐
National intangible cultural heritage inheritor HD Wang's shadow digital collection of "Four Beauties" made an amazing debut!
Redis守护进程无法停止解决方案
PHP campus movie website system for computer graduation design
阿里测开面试题
Accelerating spark data access with alluxio in kubernetes
[solved] how to generate a beautiful static document description page
Computer graduation design PHP college student human resources job recruitment network
Docker compose configures MySQL and realizes remote connection
02. Go language development environment configuration
How to use C to copy files on UNIX- How can I copy a file on Unix using C?
【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
How does the crystal oscillator vibrate?
Folio. Ink is a free, fast and easy-to-use image sharing tool
PHP error what is an error?
[flask] official tutorial -part2: Blueprint - view, template, static file
Leetcode3. Implement strstr()
Basic operations of database and table ----- delete data table
Basic operations of databases and tables ----- unique constraints
How to set an alias inside a bash shell script so that is it visible from the outside?
leetcode3、实现 strStr()