AcWing 842. 排列数字
深搜的一道水题
https://www.acwing.com/problem/content/844/ 正确输入输出
#include<bits/stdc++.h>
using namespace std;
const int N=;
int path[N];
bool vis[N];
int n;
void dfs(int u) {
if(u==n) {
for(int i=; i<n; i++) {
cout<<path[i]<<" ";
}
cout<<endl;
return ; //回溯
}
for(int i=; i<=n; i++) {
if(!vis[i]) { //如果访问过
vis[i]=; //标记
path[u]=i; //记录此时的数字
dfs(u+); //下一步
vis[i]=; //清理现场
path[u]=;
}
}
}
int main() {
cin>>n;
dfs();
return ;
}
#include<bits/stdc++.h>
using namespace std;
const int N=;
int n,m;
int g[N][N]; //地图
int d[N][N]; //到起点的距离
typedef pair<int,int>PII;
PII q[N*N];//pair
PII Prev[N][N]; //用来记录x,y 这个点从哪个点过来,并输出路径
int bfs() {
int hh=,tt=; //hh 队头 ,tt 队尾
q[]= {,};
memset(d,-,sizeof d); //初始化
d[][]=; //起点到起点的距离为0
int dx[]= {-,,,}, dy[] = {,,,-};
while(hh<=tt) {
auto t = q[hh++]; //取出队头
for(int i=; i<; i++) {
int x=t.first+dx[i],y=t.second+dy[i];
if(x>=&&x<n&&y>=&&y<m&&g[x][y]==&&d[x][y]==-) { //为空地不是障碍物,而且没有走过,
Prev[x][y]=t; //记录从哪个个点过来
d[x][y]=d[t.first][t.second]+; //标记距离
q[++tt]= {x,y}; //在放进去
}
}
}
int x=n-,y=m-;
while(x||y) {
cout<<x<<" "<<y<<endl;
auto t=Prev[x][y];
x=t.first,y=t.second; //输出路径
}
return d[n-][m-];
}
int main() {
cin>>n>>m;
for(int i=; i<n; i++)
for(int j=; j<m; j++)
cin>>g[i][j];
cout<<bfs()<<endl;
return ;
}
AcWing 842. 排列数字的更多相关文章
- [AcWing 823] 排列
点击查看代码 #include<iostream> using namespace std; const int N = 10; int n; void dfs(int u, int nu ...
- HDOJ 1755 - A Number Puzzle 排列数字凑同余,状态压缩DP
dp [ x ] [ y ] [ z ] 表示二进制y所表示的组合对应的之和mod x余数为z的最小数... 如可用的数字为 1 2 3 4...那么 dp [ 7 ] [ 15 ] [ 2 ] = ...
- AcWing 230. 排列计数 水题(组合数+错排)打卡
题目:https://www.acwing.com/problem/content/232/ #include<bits/stdc++.h> #define ll long long #d ...
- AcWing 339 .圆形数字
大型补档计划 题目链接 设 \(f[i][j]\) 表示二进制下,数字有 \(i\) 位, \(0\) 的个数 - \(1\) 的个数 \(=\) \(j\) 的方案数 \(f[0][0] = 1;\ ...
- 《挑战程序设计竞赛》——DFS
DFS(深度优先搜索) 简介 深度优先搜索(DFS,Depth-First Search)是搜索的手段之一.它从某个状态开始,不断的转移状态直到无法转移.然后退回到前一步的状态,继续转移到其他状态,如 ...
- 9月19日下午JavaScript数组冒泡排列和二分法
数组 一.冒泡排列 对数组attr = [1,8,6,4,5,3,7,2,9]进行由大到小排列,用冒泡排列的方法排列时,会对数组进行比较互换.如果前一个数字较大,这2个元素排列方式不变,如果后一个元素 ...
- JS数组2(冒泡排列、数组里面查找数据)
数组 一.冒泡排列 对数组attr = [1,8,6,4,5,3,7,2,9]进行由大到小排列,用冒泡排列的方法排列时,会对数组进行比较互换.如果前一个数字较大,这2个元素排列方式不变,如果后一个元素 ...
- js部分总结
1 currentStyle 可以获取行间样式,但是不兼容 其他浏览器用getComputedStyle(div,null)这个ie低级版本不兼容; if(div.currentStyle){ } e ...
- codeforces B. Color the Fence 解题报告
题目链接:http://codeforces.com/problemset/problem/349/B 题目意思:给定v升的颜料和9个需要花费ad 升的颜料,花费ad 升的颜料意味着得到第d个数字,现 ...
随机推荐
- npm常用模块汇总
npm常用模块汇总: 点击插件名字,查看使用文档 npm常用模块汇总 node常用模块汇总 gulp常用插件汇总 npx 使用教程:npx使用教程 bable:bable这是JavaScript编译器 ...
- 问题 I: 排名
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> ...
- Linux C++ 单链表添加,删除,输出,逆序操作
/*单链表操作*/#include <iostream>using namespace std; class Node{ public: Node(){ next=0; } Node(in ...
- POI导出PPT
1.null <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> <g ...
- Linux安装U盘启动报错Failed to load ldlinux.c32
报错信息 使用U盘安装linux无法正常启动 Start booting from USB device... SYSLINUX 5.10 EDD 2013-06-04 Copyright (C) 1 ...
- ECMAScript基本语法——⑤运算符 逻辑运算符
&&与,会短路:左边为false右边就不参与运算||或,会短路:左边为true右边就不参与运算!非, 注意:在JavaScript中,如果运算数不是运算符要求的类型,那么JavaScr ...
- 【巨杉数据库SequoiaDB】企业级和开源领域“两开花”,巨杉引领国产数据库创新
2019年12月15日,OSC 源创会·年终盛典在深圳圆满举行.巨杉数据库作为业界领先的金融级分布式数据库厂商, 获得 “2019年开源数据库先锋企业” 及 “2019 GVP-Gitee最有价值开源 ...
- Longest Ordered Subsequence POJ - 2533 dp 最长上升/不下降 子序列
#include<iostream> using namespace std ; ; int f[N]; int a[N]; int n; int main() { cin>> ...
- 机器学习作业(八)异常检测与推荐系统——Matlab实现
题目下载[传送门] 第1题 简述:对于一组网络数据进行异常检测. 第1步:读取数据文件,使用高斯分布计算 μ 和 σ²: % The following command loads the datas ...
- code码说明
https://www.cnblogs.com/wanglaowu/p/6229843.html