个人感觉DFS没有递归那么烧脑
简单迷宫
如何接受二维数组
先构建A[MAXN][MAXN],人后二重循环
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <cstring>
using namespace std;
const int N=13,maxn=10;
int n=2,m=2;
int wi[N]={1,2},C[maxn][maxn],D[maxn][maxn]={0};
int ans=0;
void dfs(int r,int c)
{//printf("(%d,%d)",r,c);
if(r==n-1 and c==m-1)
{
ans++;return;
}
else
{
if(r+1<n){if(!C[r+1][c] && !D[r+1][c]) {D[r+1][c]=1;dfs(r+1,c);D[r+1][c]=0;}}
if(r-1>=0){if(!C[r-1][c] && !D[r-1][c]) {D[r-1][c]=1;dfs(r-1,c);D[r-1][c]=0;}}
if(c+1<m){if(!C[r][c+1] && !D[r][c+1]) {D[r][c+1]=1;dfs(r,c+1);D[r][c+1]=0;}}
if(c-1>=0){if(!C[r][c-1] && !D[r][c-1]) {D[r][c-1]=1;dfs(r,c-1);D[r][c-1]=0;}}
return;
}
}
int main()
{D[0][0]=1;
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{scanf("%d",&C[i][j]);}
//printf("?");
dfs(0,0);printf("%d",ans);
//for(int i=0;i<T.size();i++)printf("@%d@",T[i]);
}
参考答案:
1.把方案放入列表
2.dfs执行立刻置dfs(i,j)的visit1
#include <cstdio>
const int MAXN = 5;
int n, m, maze[MAXN][MAXN];
bool visited[MAXN][MAXN] = {false};
int counter = 0;
const int MAXD = 4;
int dx[MAXD] = {0, 0, 1, -1};
int dy[MAXD] = {1, -1, 0, 0};
bool isValid(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m && maze[x][y] == 0 && !visited[x][y];
}
void DFS(int x, int y) {
if (x == n - 1 && y == m - 1) {
counter++;
return;
}
visited[x][y] = true;
for (int i = 0; i < MAXD; i++) {
int nextX = x + dx[i];
int nextY = y + dy[i];
if (isValid(nextX, nextY)) {
DFS(nextX, nextY);
}
}
visited[x][y] = false;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &maze[i][j]);
}
}
DFS(0, 0);
printf("%d", counter);
return 0;
}
矩阵权值最大路径
写死我了,四个方向,每个方向都写一遍
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <cstring>
using namespace std;
const int N=13,maxn=10;
int n=2,m=2,k;
int wi[N]={1,2},C[maxn][maxn]={0},D[maxn][maxn]={0},W[maxn][maxn];
int step=0;
int ans=-101;
int w0;
vector<int> row,col,ans_R,ans_C;
void dfs(int r,int c)
{//printf("(%d,%d)",r,c);
//printf("?");
if(r==n-1 and c==m-1)
{
if(ans<w0)
{ans=w0,ans_R=row;ans_C=col;
}return;
}
else
{
if(r+1<n){if(!C[r+1][c] && !D[r+1][c]) {//printf("1");
D[r+1][c]=1;w0+=W[r+1][c];row.push_back(r+1);col.push_back(c);
dfs(r+1,c);
row.pop_back();col.pop_back();w0-=W[r+1][c];D[r+1][c]=0;}}
if(r-1>=0){if(!C[r-1][c] && !D[r-1][c]) {//printf("2");
D[r-1][c]=1;w0+=W[r-1][c];row.push_back(r-1);col.push_back(c);
dfs(r-1,c);
row.pop_back();col.pop_back();w0-=W[r-1][c];D[r-1][c]=0;}}
if(c+1<m){if(!C[r][c+1] && !D[r][c+1]) {//printf("3");
D[r][c+1]=1;w0+=W[r][c+1];row.push_back(r);col.push_back(c+1);
dfs(r,c+1);
row.pop_back();col.pop_back();w0-=W[r][c+1];D[r][c+1]=0;}}
if(c-1>=0){if(!C[r][c-1] && !D[r][c-1]) {//printf("4");
D[r][c-1]=1;w0+=W[r][c-1];row.push_back(r);col.push_back(c-1);
dfs(r,c-1);
row.pop_back();col.pop_back();w0-=W[r][c-1];D[r][c-1]=0;}}
return;
}
}
int main()
{D[0][0]=1;
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{scanf("%d",&W[i][j]);}
w0=W[0][0];
//printf("?");
dfs(0,0);
printf("1 1\n");
//printf("%d",ans);
//for(vector<int>::iterator it=ans_R.begin();it!=ans_R.end();it++)
for(int i=0;i<ans_R.size();i++)
{printf("%d %d",ans_R[i]+1,ans_C[i]+1);if(i<ans_R.size()-1) printf("\n");
}
}
答案用了循环写的
#include <cstdio>
#include <vector>
#include <utility>
using namespace std;
typedef pair<int, int> Position;
const int MAXN = 5;
const int INF = 0x3f;
int n, m, maze[MAXN][MAXN];
bool visited[MAXN][MAXN] = {false};
int maxValue = -INF;
vector<Position> tempPath, optPath;
const int MAXD = 4;
int dx[MAXD] = {0, 0, 1, -1};
int dy[MAXD] = {1, -1, 0, 0};
bool isValid(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m && !visited[x][y];
}
void DFS(int x, int y, int nowValue) {
if (x == n - 1 && y == m - 1) {
if (nowValue > maxValue) {
maxValue = nowValue;
optPath = tempPath;
}
return;
}
visited[x][y] = true;
for (int i = 0; i < MAXD; i++) {
int nextX = x + dx[i];
int nextY = y + dy[i];
if (isValid(nextX, nextY)) {
int nextValue = nowValue + maze[nextX][nextY];
tempPath.push_back(Position(nextX, nextY));
DFS(nextX, nextY, nextValue);
tempPath.pop_back();
}
}
visited[x][y] = false;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &maze[i][j]);
}
}
tempPath.push_back(Position(0, 0));
DFS(0, 0, maze[0][0]);
for (int i = 0; i < optPath.size(); i++) {
printf("%d %d\n", optPath[i].first + 1, optPath[i].second + 1);
}
return 0;
}