当前位置:网站首页>[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
边栏推荐
- Selenium waiting mode
- Social networking website for college students based on computer graduation design PHP
- MCU lightweight system core
- 【Flask】响应、session与Message Flashing
- Force buckle 9 palindromes
- 干货!通过软硬件协同设计加速稀疏神经网络
- You are using pip version 21.1.1; however, version 22.0.3 is available. You should consider upgradin
- Get the relevant information of ID card through PHP, get the zodiac, get the constellation, get the age, and get the gender
- 500 lines of code to understand the principle of mecached cache client driver
- 插卡4G工业路由器充电桩智能柜专网视频监控4G转以太网转WiFi有线网速测试 软硬件定制
猜你喜欢
Leetcode3, implémenter strstr ()
[flask] official tutorial -part1: project layout, application settings, definition and database access
干货!通过软硬件协同设计加速稀疏神经网络
2022 PMP project management examination agile knowledge points (8)
【已解决】如何生成漂亮的静态文档说明页
02.Go语言开发环境配置
Force buckle 1020 Number of enclaves
500 lines of code to understand the principle of mecached cache client driver
【SSRF-01】服务器端请求伪造漏洞原理及利用实例
NLP第四范式:Prompt概述【Pre-train,Prompt(提示),Predict】【刘鹏飞】
随机推荐
Competition question 2022-6-26
genius-storage使用文档,一个浏览器缓存工具
A Cooperative Approach to Particle Swarm Optimization
Install redis
剑指 Offer 38. 字符串的排列
Win10 add file extension
Redis list
Sword finger offer 12 Path in matrix
插卡4G工业路由器充电桩智能柜专网视频监控4G转以太网转WiFi有线网速测试 软硬件定制
Tensorflow customize the whole training process
Open source | Ctrip ticket BDD UI testing framework flybirds
PHP error what is an error?
【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
leetcode-2.回文判断
Redis daemon cannot stop the solution
500 lines of code to understand the principle of mecached cache client driver
500 lines of code to understand the principle of mecached cache client driver
Docker compose configures MySQL and realizes remote connection
【详细】快速实现对象映射的几种方式
正则表达式:示例(1)