解题思路:dfs直接搜,拿一个数组存一下访问过的数组就行啦,变量名不规范见谅

注意事项:

参考代码:

#include<bits/stdc++.h>

using namespace std;

const int MAX_SIZE=20;

char str[MAX_SIZE][MAX_SIZE];

int n,m;

int max_num=0;

bool flag[MAX_SIZE][MAX_SIZE]={false};

char isShow[40];

int isShow_dis;

int dis_x[]={-1,1,0,0};

int dis_y[]={0,0,-1,1};

bool isCan(char c)

{

   for(int i=0;i<isShow_dis;i++)

   {

      if(c==isShow[i])

      {

         return false;

      }

   }

   return true;

}

void dfs(int x,int y,int nums)

{

   if(nums>=max_num)

   {

      max_num=nums;

   }

   for(int i=0;i<4;i++)

   {

      int current_x=x+dis_x[i];

      int current_y=y+dis_y[i];

      if(current_x>=0&&current_x<n&&current_y>=0&&current_y<m&&isCan(str[current_x][current_y]))

      {

         isShow[isShow_dis++]=str[current_x][current_y];

         flag[current_x][current_y]=true;    

         dfs(current_x,current_y,nums+1);

         //回 

         isShow_dis--;

         flag[current_x][current_y]=false;   

         

      }

   }

}

int main()

{

   ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

   cin>>n>>m;

   for(int i=0;i<n;i++)

   {

      for(int j=0;j<m;j++)

      {

         cin>>str[i][j];

      }

   }

   isShow[isShow_dis++] = str[0][0];

   flag[0][0] = true;

   dfs(0,0,1);

   cout << max_num << endl;  

      

}


点赞(0)
 

0.0分

0 人评分

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 0 条评论

暂无评论