当前位置:网站首页>[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
边栏推荐
- Apicloud openframe realizes the transfer and return of parameters to the previous page - basic improvement
- How to use C to copy files on UNIX- How can I copy a file on Unix using C?
- Numpy array index slice
- leetcode-两数之和
- Leetcode skimming questions_ Invert vowels in a string
- Publish your own toolkit notes using NPM
- 安装Redis
- C web page open WinForm exe
- 【Flask】静态文件与模板渲染
- Leetcode skimming questions_ Verify palindrome string II
猜你喜欢
Mongodb problem set
Computer graduation design PHP college student human resources job recruitment network
Executing two identical SQL statements in the same sqlsession will result in different total numbers
晶振是如何起振的?
Computer graduation design PHP college classroom application management system
Redis list
dried food! Accelerating sparse neural network through hardware and software co design
You are using pip version 21.1.1; however, version 22.0.3 is available. You should consider upgradin
同一个 SqlSession 中执行两条一模一样的SQL语句查询得到的 total 数量不一样
Grabbing and sorting out external articles -- status bar [4]
随机推荐
Campus second-hand transaction based on wechat applet
Pangolin Library: subgraph
NLP fourth paradigm: overview of prompt [pre train, prompt, predict] [Liu Pengfei]
[ssrf-01] principle and utilization examples of server-side Request Forgery vulnerability
Sword finger offer 12 Path in matrix
Code review concerns
Blue Bridge Cup embedded_ STM32_ New project file_ Explain in detail
Using SA token to solve websocket handshake authentication
leetcode3、實現 strStr()
SPI communication protocol
A Cooperative Approach to Particle Swarm Optimization
TrueType字体文件提取关键信息
leetcode-2. Palindrome judgment
PHP error what is an error?
ClickOnce does not support request execution level 'requireAdministrator'
Flowable source code comments (36) process instance migration status job processor, BPMN history cleanup job processor, external worker task completion job processor
Visualstudio2019 compilation configuration lastools-v2.0.0 under win10 system
干货!通过软硬件协同设计加速稀疏神经网络
LeetCode 322. Change exchange (dynamic planning)
Maya hollowed out modeling