向DA LAO求助,自己调了好久还是全WA,样例过了

P1506 拯救oibh总部

### 不懂就问 说明: 初版: a为输入数组,vis判断有没有走过,flag标记,dx dy方向标,其他的我相信大佬们能看懂 改版: 新增v数组来方便计数,flag抛弃
by a15244412002 @ 2024-02-15 22:07:13


@[a15244412002](/user/991685) ```cpp #include<bits/stdc++.h> using namespace std; int dx[]={0,0,-1,1}; int dy[]={-1,1,0,0}; int n,m,ans=0; bool flag[505][505]; char a[505][505]; void dfs(int x,int y){ if(x==n+1 && y==m+1){ return; } for(int i=0;i<4;i++){ int tx=x+dx[i],ty=y+dy[i]; if(tx>=0 && tx<=n+1 && ty>=0 && ty<=m+1 && a[tx][ty]!='*' && !flag[tx][ty]){ flag[tx][ty]=1; dfs(tx,ty); } } } int main(){ cin>>n>>m; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>a[i][j]; } } dfs(0,0); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(!flag[i][j] && a[i][j]=='0'){ ans++; } } } cout<<ans; return 0; } ```
by wei2013 @ 2024-02-15 22:38:40


@[wei2013](/user/1060672) 麻烦下大佬看看能不能在我的代码基础上改,实在不行就私聊把带注释的发我下呗
by a15244412002 @ 2024-02-16 15:36:01


@[a15244412002](/user/991685) ```cpp #include<bits/stdc++.h> using namespace std; int dx[]={0,0,-1,1}; int dy[]={-1,1,0,0};//方向数组 int n,m,ans=0; bool flag[505][505];//是否淹没 char a[505][505]; void dfs(int x,int y){//dfs板子 if(x==n+1 && y==m+1){//从外面圈一圈 return; } for(int i=0;i<4;i++){ int tx=x+dx[i],ty=y+dy[i]; if(tx>=0 && tx<=n+1 && ty>=0 && ty<=m+1 && a[tx][ty]!='*' && !flag[tx][ty]){// 是否可以通过 flag[tx][ty]=1; dfs(tx,ty);// 递归 } } } int main(){ cin>>n>>m; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>a[i][j]; } } dfs(0,0); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(!flag[i][j] && a[i][j]=='0'){// 扫一遍求未被淹没的0的个数 ans++; } } } cout<<ans; return 0; } ``` ~~验证码bt3k 祭!!!!~~
by wei2013 @ 2024-02-16 15:45:48


谢谢DA LAO已关 @[wei2013](/user/1060672)
by a15244412002 @ 2024-02-16 18:35:34


|