当前位置:网站首页>[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
边栏推荐
- 插卡4G工业路由器充电桩智能柜专网视频监控4G转以太网转WiFi有线网速测试 软硬件定制
- How to set an alias inside a bash shell script so that is it visible from the outside?
- 【SSRF-01】服务器端请求伪造漏洞原理及利用实例
- 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
- Install redis
- How to improve the level of pinduoduo store? Dianyingtong came to tell you
- Redis-Key的操作
- leetcode3、實現 strStr()
- LeetCode 322. Change exchange (dynamic planning)
- 【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
猜你喜欢

Maya hollowed out modeling

Mongodb problem set
![NLP fourth paradigm: overview of prompt [pre train, prompt, predict] [Liu Pengfei]](/img/11/a01348dbfcae2042ec9f3e40065f3a.png)
NLP fourth paradigm: overview of prompt [pre train, prompt, predict] [Liu Pengfei]

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

A Cooperative Approach to Particle Swarm Optimization

Redis如何实现多可用区?

Redis list

Unity learning notes -- 2D one-way platform production method

Open source | Ctrip ticket BDD UI testing framework flybirds

NiO related knowledge (II)
随机推荐
【全网最全】 |MySQL EXPLAIN 完全解读
Get the relevant information of ID card through PHP, get the zodiac, get the constellation, get the age, and get the gender
[detailed] several ways to quickly realize object mapping
A Cooperative Approach to Particle Swarm Optimization
Blue Bridge Cup embedded_ STM32 learning_ Key_ Explain in detail
GBase 8c数据库升级报错
竞赛题 2022-6-26
Basic operations of databases and tables ----- default constraints
How to upgrade kubernetes in place
Luo Gu P1170 Bugs Bunny and Hunter
[技术发展-28]:信息通信网大全、新的技术形态、信息通信行业高质量发展概览
Thinking about the best practice of dynamics 365 development collaboration
Basic operations of databases and tables ----- non empty constraints
NumPy 数组索引 切片
How does the crystal oscillator vibrate?
PHP campus financial management system for computer graduation design
Leetcode skimming questions_ Verify palindrome string II
正则表达式:示例(1)
Leetcode3. Implement strstr()
C web page open WinForm exe