1212:LETTERS

#include<bits/stdc++.h>
using namespace std;
char mp[25][25];
int n,m,ans,vist[4][2]={-1,0,0,-1,0,1,1,0}; 
bool st[30];//0-->A   st[字母-'A']

void dfs(int x,int y,int cnt){
	//隐藏出口 
	if(ans<cnt) ans=cnt;//用ans记录出现过的最长路径 
	
	for(int i=0;i<4;i++)//遍历4个方向
	{
		int tx=x+vist[i][0],ty=y+vist[i][1];
		//tx ty表示由x y可以到达的点
		//没越界、没走过的字母 
		if(tx>=1&&tx<=n&&ty>=1&&ty<=m && st[ mp[tx][ty]-'A' ]==0){
			st[ mp[tx][ty]-'A' ]=1;//进入一个坐标之前,保存现场 
			dfs(tx,ty,cnt+1);
			st[ mp[tx][ty]-'A' ]=0;//回溯:还原现场 
		} 
	 } 
	 //return; 
}

int main()
{
 	cin>>n>>m;
 	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		    cin>>mp[i][j];
    st[mp[1][1]-'A']=1;//标记起点字母
	dfs(1,1,1);//坐标  点的个数 
	cout<<ans<<endl;
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值