社区讨论
这题有DFS的题解吗
P1002[NOIP 2002 普及组] 过河卒参与者 5已保存回复 15
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 15 条
- 当前快照
- 1 份
- 快照标识符
- @lo1600ul
- 此快照首次捕获于
- 2023/10/22 15:46 2 年前
- 此快照最后确认于
- 2023/11/02 15:21 2 年前
这题用DFS会超时吗,代码不知哪里错了。。。(听取WA声一片)
CPP#include<bits/stdc++.h>
using namespace std;
int n,m,a,b,step;
const int dx[]={0,-2,-1,1,2,2,1,-1,-2};
const int dy[]={0,1,2,2,1,-1,-2,-2,-1};
bool vis[25][25];
const int d1[]={0,1,0};
const int d2[]={0,0,1};
void dfs(int x,int y,int step){
if(x==n&&y==m){
step++;
return;
}
for(int i=1;i<=2;i++){
int tx=x+d1[i];
int ty=y+d2[i];
if(vis[tx][ty]==0&&tx<=n&&ty<=m){
vis[tx][ty]=1;
dfs(tx,ty,step);
vis[tx][ty]=0;
}
}
}
int main(){
cin>>n>>m>>a>>b;
for(int i=1;i<=8;i++){
vis[a+dx[i]][b+dy[i]]=1;
}
dfs(0,0,step);
cout<<step<<endl;
}
每次写DFS都写不对,好崩溃啊,希望有DALAO能帮帮我!
回复
共 15 条回复,欢迎继续交流。
正在加载回复...