P1958 上学路线 分析:统计左下角到右上角的路径数
#include<bits/stdc++.h>
using namespace std;
int a,b,sum;
int n,x1[45],x2[45],a1[20][20];
int dx[4] = {1,0};//右下两个方向
int dy[4] = {0,1};
bool vis[20][20];
void dfs(int x,int y){
if(x == a and y == b){//到了终点方案+1并回溯
sum++;
return ;
}
if(x >= a + 1 or y >= b + 1 or vis[x][y] == 1) return ;//判断能不能走
vis[x][y] = 1;
for(int i = 0;i < 2;i++) dfs(x + dx[i],y + dy[i]);
vis[x][y] = 0;//回溯
}
int main(){
cin >> a >> b;
cin >> n;
for(int i = 1;i <= n;i++){
cin >> x1[i] >> x2[i];
vis[x1[i]][x2[i]] = 1;
}
if(vis[1][1] == 1) {//考虑起点不能走的情况
cout << 0;
return 0;
}
dfs(1,1);
cout << sum;
return 0;
}
**D3432 【基础】迷宫的第一条出路 **
分析:从有下角开始往前找,记录每个上一步的数据,最后倒着输出
#include<bits/stdc++.h>
using namespace std;
int n;
char a[25][25];
bool vis[25][25];
int dx[4] = {0,-1,0,1};//左上右下
int dy[4] = {-1,0,1,0};
struct node{
int x,y;
};
node pre[25][25];
void print(int x,int y){
```cpp
#include<bits/stdc++.h>
using namespace std;
int a,b,sum;
int n,x1[45],x2[45],a1[20][20];
int dx[4] = {1,2,-2,-1};
int dy[4] = {2,1,1,2};
bool vis[20][20];
void dfs(int x,int y){
if(x == a and y == b){
sum++;
return ;
}
if(x > a or y > b or x < 0 or y < 0) return ;
vis[x][y] = 1;
for(int i = 0;i < 4;i++) dfs(x + dx[i],y + dy[i]);
vis[x][y] = 0;
}
int main(){
cin >> a >> b;
dfs(0,0);
cout << sum;
return 0;
}
if(x == 1 and y == 1){
cout << "(1,1)";
return ;
}
print(pre[x][y].x,pre[x][y].y);//递归,不到一不输出,递归到一时倒着输出
cout << "->" << '(' << x <<','<< y << ')';
}
void dfs(int x,int y){
if(x == n and y == n){
print(n,n);//开始输出第一条路径
return ;
}
for(int i = 0 ;i < 4 ;i++){
int xx = x + dx[i],yy = y + dy[i];
if(xx >= 1 and yy >= 1 and yy <= n and xx <= n and vis[xx][yy]==0){
vis[xx][yy] = 1;
pre[xx][yy] = {x,y};//存储上一步数据
dfs(xx,yy);
//由于是第一条路径所以不回溯
}
}
}
int main(){
cin >> n;
for(int i= 1;i <= n;i++){
for(int j = 1;j <= n;j++){
cin >> a[i][j];
}
}
vis[1][1] = 1;
dfs(1,1);
return 0;
}
原代码错因
#include<bits/stdc++.h>
using namespace std;
int n;
char a[25][25];
bool vis[25][25];
int dx[4] = {-1,0,1,0};//顺序错误
int dy[4] = {0,-1,0,1};
struct node{
int x,y;
};
node pre[25][25];
void print(int x,int y){
if(x == 1 and y == 1){
cout << "(1,1)";
return ;
}
print(pre[x][y].x,pre[x][y].y);
cout << "->" << '(' << x <<','<< y << ')';
}
void dfs(int x,int y){
if(x == n and y == n){
cout << n;
return ;
}
for(int i = 0 ;i < 4 ;i++){
int xx = x + dx[i],yy = y + dy[i];
if(xx >= 0 and yy >= 0 and yy <= n and xx <= n){
vis[xx][yy];//没有标记
pre[xx][yy] = {x,y};
dfs(xx,yy);
}
}
}
int main(){
cin >> n;
for(int i= 1;i <= n;i++){
for(int j = 1;j <= n;j++){
cin >> a[i][j];
}
}
dfs(1,1);
return 0;
}
****D3361 【基础】卒的遍历
分析:同样倒着输出,不过需要回溯并记录方案数,同时并没有障碍(可以数学做?)
#include<bits/stdc++.h>
using namespace std;
int n,m,sum = 1;
char a[25][25];
bool vis[25][25];
int dx[2] = {1,0};//只能向下或向右
int dy[2] = {0,1};
struct node{
int x,y;
};
node pre[25][25];
void print(int x,int y){
if(x == 1 and y == 1){
cout << "1,1";
return ;
}
print(pre[x][y].x,pre[x][y].y);
cout << "->" << x <<','<< y;
}
void dfs(int x,int y){
if(x == n and y == m){
cout << sum << ':';
print(n,m);
sum++;//路径数+1
cout << '\n';
return ;
}
for(int i = 0 ;i < 2 ;i++){
int xx = x + dx[i],yy = y + dy[i];
if(xx >= 1 and yy >= 1 and yy <= m and xx <= n && vis[xx][yy]==0){//判断是否能走
vis[xx][yy] = 1;
pre[xx][yy] = {x,y};//记录上一步数据
dfs(xx,yy);
vis[xx][yy] = 0//由于要有多种方案故要回溯
}
}
}
int main(){
cin >> n >> m;
vis[1][1] = 1;
dfs(1,1);
return 0;
}
分析;回溯寻找并倒着输出所有方案,有障碍 **P1238 走迷宫 **
#include<bits/stdc++.h>
using namespace std;
int n,m;
int sx,sy,fx,fy,f = 0;
char a[25][25];
bool vis[25][25];
int dx[4] = {0,-1,0,1};
int dy[4] = {-1,0,1,0};
struct node{
int x,y;
};
node pre[25][25];
void print(int x,int y){
if(x == sx and y == sy){
cout << '(' << sx <<','<< sy << ')';
return ;
}
print(pre[x][y].x,pre[x][y].y);//递归输出
cout << "->" << '(' << x <<','<< y << ')';
}
void dfs(int x,int y){
if(x == fx and y == fy){
print(fx,fy);
cout << '\n';
f=1;
return ;
}
for(int i = 0 ;i < 4 ;i++){
int xx = x + dx[i],yy = y + dy[i];
if(xx >= 1 and yy >= 1 and yy <= m and xx <= n and a[xx][yy] != '0'&&vis[xx][yy]==0){//判断是否能走
vis[xx][yy] = 1;
pre[xx][yy] = {x,y};//记录上一步数据
dfs(xx,yy);
vis[xx][yy] = 0;
}
}
}
int main(){
cin >> n >> m;
for(int i= 1;i <= n;i++){
for(int j = 1;j <= m;j++){
cin >> a[i][j];
}
}
cin >> sx >> sy;//起始位置和终点
cin >> fx >> fy;
if(a[sx][sy] == 0 or a[fx][fy] == 0){//考虑起始位置的情况
cout << -1;
return 0;
}
vis[sx][sy] = 1;
dfs(sx,sy);
if(f == 0) cout << -1;//没路输出-1
return 0;
}
跳马问题 分析:板子题,改运动即可
#include<bits/stdc++.h>
using namespace std;
int a,b,sum;
int n,x1[45],x2[45],a1[20][20];
int dx[4] = {1,2,-2,-1};
int dy[4] = {2,1,1,2};
bool vis[20][20];
void dfs(int x,int y){
if(x == a and y == b){
sum++;
return ;
}
if(x > a or y > b or x < 0 or y < 0) return ;
vis[x][y] = 1;
for(int i = 0;i < 4;i++) dfs(x + dx[i],y + dy[i]);
vis[x][y] = 0;
}
int main(){
cin >> a >> b;
dfs(0,0);
cout << sum;
return 0;
}