P1958 上学路线
思路:
用深度优先搜索枚举从(1,1)走到(n,m)的方案数。
#include<bits/stdc++.h>
using namespace std;
const int dx[] = {0, 1};
const int dy[] = {1, 0};
int n, m, k, a[45], b[45], f[20][20], ans;
void dfs(int x, int y){
if(x < 1 || x > n || y < 1 || y > m || f[x][y]){
return;
}
if(x == n && y == m){
ans++;
return;
}
f[x][y] = 1;
for(int i = 0; i < 2; i++){
dfs(x + dx[i], y + dy[i]);
}
f[x][y] = 0;
}
int main(){
cin >> n >> m >> k;
for(int i = 1; i <= k; i++){
cin >> a[i] >> b[i];
f[a[i]][b[i]] = 1;
}
dfs(1, 1);
cout << ans;
return 0;
}D3432 【基础】迷宫的第一条出路
错误原因:
方向顺序写错
思路:
使用深度优先搜索,搜出第一条路,并在搜索时用一个数组记录次坐标的上一步,最后倒序输出。
#include<bits/stdc++.h>
using namespace std;
const int dx[] = {0, -1, 0, 1};
const int dy[] = {-1, 0, 1, 0};
struct P{
int x, y;
}pre[25][25];
int n, a[25][25], t;
bool f[25][25];
char s;
void print(int x, int y){
if(x == 1 && y == 1){
cout << "(" << 1 << "," << 1 << ")";
return;
}
print(pre[x][y].x, pre[x][y].y);
cout << "->(" << x << "," << y << ")";
return;
}
void dfs(int x, int y){
if(x == n && y == n){
print(n, n);
return;
}
for(int i = 0; i < 4; i++){
int sx = x + dx[i], sy = y + dy[i];
if(sx >= 1 && sx <= n && sy >= 1 && sy <= n && a[sx][sy] == 0 && f[sx][sy] == 0){
f[sx][sy] = 1;
pre[sx][sy] = {x, y};
dfs(sx, sy);
}
}
}
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cin >> s;
a[i][j] = (s - '0');
}
}
f[1][1] = 1;
dfs(1, 1);
return 0;
}卒的遍历
思路:
使用深度优先搜索,搜出所有路,并在搜索时用一个数组记录次坐标的上一步。每次找完后,倒序输出。
#include<bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0};
const int dy[] = {0, 1};
struct P{
int x, y;
}pre[25][25];
int n, m, t;
bool f[25][25];
char s;
void print(int x, int y){
if(x == 1 && y == 1){
cout << 1 << "," << 1;
return;
}
print(pre[x][y].x, pre[x][y].y);
cout << "->" << x << "," << y;
return;
}
void dfs(int x, int y){
if(x == n && y == m){
t++;
cout << t << ":";
print(n, m);
cout << '\n';
return;
}
for(int i = 0; i < 2; i++){
int sx = x + dx[i], sy = y + dy[i];
if(sx >= 1 && sx <= n && sy >= 1 && sy <= m && f[sx][sy] == 0){
f[sx][sy] = 1;
pre[sx][sy] = {x, y};
dfs(sx, sy);
f[sx][sy] = 0;
}
}
return;
}
int main(){
cin >> n >> m;
f[1][1] = 1;
dfs(1, 1);
return 0;
}P1238 走迷宫
思路:
使用深度优先搜索,搜出所有从起始点到结束点的路,并在搜索时用一个数组记录次坐标的上一步。每次找完后,倒序输出。
#include<bits/stdc++.h>
using namespace std;
const int dx[] = {0, -1, 0, 1};
const int dy[] = {-1, 0, 1, 0};
struct P{
int x, y;
}pre[25][25];
int n, a[25][25], m, xx, yy, l ,r, cnt;
bool f[25][25];
char s;
void print(int x, int y){
if(x == l && y == r){
cout << "(" << l << "," << r << ")";
return;
}
print(pre[x][y].x, pre[x][y].y);
cout << "->(" << x << "," << y << ")";
return;
}
void dfs(int x, int y){
if(x == xx && y == yy){
cnt++;
print(xx, yy);
cout << '\n';
return;
}
for(int i = 0; i < 4; i++){
int sx = x + dx[i], sy = y + dy[i];
if(sx >= 1 && sx <= n && sy >= 1 && sy <= m && a[sx][sy] == 1 && f[sx][sy] == 0){
f[sx][sy] = 1;
pre[sx][sy] = {x, y};
dfs(sx, sy);
f[sx][sy] = 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 >> l >> r >> xx >> yy;
f[l][r] = 1;
dfs(l, r);
if(cnt == 0){
cout << -1;
}
return 0;
}