B题,第二题,求hack,用的dfs 代码简洁明了

B题,第二题,求hack,用的dfs 代码简洁明了

#include<bits/stdc++.h>
#define int long long 
#define ed '\n'
#define IOS ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define all(x) x.begin(), x.end()
#define lb(x) x&-x
using namespace std;
typedef pair<int,int>pa;
const int mod_=1e18;
const int mod=998244353;
const int g=1e3+10;
bool bf[g][g];
int fx[]={0,1,0};
int fy[]={0,0,1};
int sum[g];
int cnt=0;
int n=0,m=0;
void dfs(int x,int y)
{

    if(x==n&&y==m)
    {
        for(int i=1;i<=cnt;i++)
        {
            if(sum[i]==1)cout<<"S";
            else cout<<"D";
        }
        exit(0);
    }
    for(int i=1;i<=2;i++)
    {
        int tx=fx[i]+x;
        int ty=fy[i]+y;
       
        if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&bf[tx][ty]==0)
        {   
            sum[++cnt]=i;
            dfs(tx,ty);
            cnt--;
        }
    }
}
void solve()
{ 

    cin>>n>>m;
   
    int xt=0,y=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            char x;cin>>x;
            if(x=='#')
            {
                xt=i;
                y=j;
            }
        }
    }
    

    bf[xt][y]=1;
   
    dfs(1,1);
  return;
}
signed main() 
{ 
  
  IOS;

 //srand((unsigned long long)time(NULL));
    
//freopen("in.txt","r",stdin);
//freopen("zheng.txt","w",stdout);

  int t=1;
  
  //cin>>t;
  
  while(t--)
  {
    solve();
  }


  return 0;
}

全部评论

相关推荐

在秋招的小白菜很想养修勾:一眼 苍穹外卖+谷粒商城,项目换一换吧,可以找一些付费知识星球博主带带,避免烂大街。多投投大厂,背背八股,你这学历乱杀了,等实习经验到位,到时候大厂闭眼选
投递美团等公司8个岗位
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务