【dfs】LETTERS

1212:LETTERS

【题目描述】

给出一个roe×colroe×col的大写字母矩阵,一开始的位置为左上角,你可以向上下左右四个方向移动,并且不能移向曾经经过的字母。问最多可以经过几个字母。

【输入】

第一行,输入字母矩阵行数RR和列数SS,1R,S201≤R,S≤20。

接着输出RR行SS列字母矩阵。

 

【输出】

最多能走过的不同字母的个数。

【输入样例】

3 6
HFDFFB
AJHGDH
DGAGEH

【输出样例】

6

错误原因:

把控制4个方向的数组dx,dy里的值赋值时,把0,0写成了0.0(我也是晕了,查了一个小时的错误)

 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<string>
#include<cstring>
using namespace std;
const int maxn=999999999;
const int minn=-999999999;
const int ll=520;
int dx[4]= {0,0,1,-1};
int dy[4]= {1,-1,0,0};
inline int read() {
    char c = getchar();
    int x = 0, f = 1;
    while(c < '0' || c > '9') {
        if(c == '-') f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int r,s,ans;
char a[ll][ll];
int visit[ll][ll],js[9999];
int da=0;
void dfs(int x,int y,int step) {
    if(da<step) {
        da=step;
    }
    for(int i=0; i<4; ++i) {
        int mx=x+dx[i];
        int my=y+dy[i];
        if((mx<r)&&(my<s)&&(mx>=0)&&(my>=0)&&(visit[mx][my]==0)&&(js[a[mx][my]-'A']==0)) {
            visit[mx][my]=1;
            js[a[mx][my]-'A']=1;
            dfs(mx,my,step+1);
            visit[mx][my]=0;
            js[a[mx][my]-'A']=0;
        }
    }
}
int main() {
    cin>>r>>s;
    for(int i=0; i<r; ++i) {
        for(int j=0; j<s; ++j) {
            cin>>a[i][j];
        }
    }
    visit[0][0]=1;
    js[a[0][0]-'A']=1;
    dfs(0,0,1);
    cout<<da; 
    return 0;
}

 

 

每日美图:

 每日美图

 

 

 

posted @ 2019-04-15 19:59  pyyyyyy  阅读(514)  评论(2编辑  收藏  举报