#include <stdio.h>

struct node { int x, y; };

int r, c, v[30][30], alpha[30], cnt=0;
int dx[4]={1, -1, 0, 0}, dy[4]={0, 0, 1, -1};
char G[30][30];

void dfs(int x, int y) {
    printf("%c ", G[x][y]);
    //v[x][y]=alpha[G[x][y]-'A']=1;
    cnt++;

    for(int i=0; i<4; i++) {
        int nx=x+dx[i], ny=y+dy[i];
        if(G[nx][ny]&&!v[nx][ny]&&!alpha[G[nx][ny]-'A']) {
            v[x][y]=alpha[G[x][y]-'A']=1;
            dfs(nx, ny);
            v[x][y]=alpha[G[x][y]-'A']=0;
        }
    }

}

int main() {
    scanf("%d %d", &r, &c);
    for(int i=1; i<=r; i++) {
        for(int j=1; j<=c; j++) {
            scanf(" %c", &G[i][j]);
        }
    }

    dfs(1, 1);
    printf("%d", cnt);
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: