1111. The letter

Links: https://www.acwing.com/problem/content/1113/

Given a capital letter R × S matrix, the starting position of the upper left corner you, you can be moved in the four directions, but not out of bounds, or to have moved through the alphabet (upper left corner of the letter seen after a letter).

Excuse me, you can go through up to a few letters.

Input Format

The first line contains two integers R and S, the letters represent rows and columns of the matrix.

Next R rows, each row containing a string of uppercase S configuration together constitute a matrix of letters.

Output Format

Output an integer representing the number of letters can pass up.

data range

1≤R,S≤20,1≤R,S≤20

Sample input:

3 6
HFDFFB
AJHGDH
DGAGEH

Sample output:

6
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
using namespace std;
const int N=10010;
const int dx[]={0,0,-1,1};
const int dy[]={1,-1,0,0};//方向打表
int r,s,res;
map<char,bool> ma;//判断这个字母是否走过
char ch[N][N];
void dfs(int x,int y,int c){
    res=max(res,c);//每次对res进行更新就不必考虑停止条件
    for(int i=0;i<4;i++){
        int xi=x+dx[i],yi=y+dy[i];
        if(xi>=0&&yi>=0&&xi<r&&yi<s&&!ma[ch[xi][yi]]){
            ma[ch[xi][yi]]=true;
            dfs(xi,yi,c+1);
            ma[ch[xi][yi]]=false;
        }
    }
}
int main(){
    cin>>r>>s;
    for(int i=0;i<r;i++){
        for(int j=0;j<s;j++){
            cin>>ch[i][j];
        }
    }
    ma[ch[0][0]]=true;//对初始的第一个点标记
    dfs(0,0,1);
    cout<<res;
    return 0;
}

 

Published 32 original articles · won praise 7 · views 796

Guess you like

Origin blog.csdn.net/Young_Naive/article/details/104171414