Skip to content

Instantly share code, notes, and snippets.

@dada8397
Created February 18, 2017 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dada8397/0b4f736f30c6a575e26a77762ce79fb3 to your computer and use it in GitHub Desktop.
Save dada8397/0b4f736f30c6a575e26a77762ce79fb3 to your computer and use it in GitHub Desktop.
POJ 1154 - Letters
#include <cstdio>
#include <iostream>
#include <stack>
using namespace std;
char map[21][21];
bool visit[30];
int R, C, Max;
void DFS(int x, int y, int cntt) {
cntt ++;
Max = (Max < cnt) ? cnt : Max;
visit[map[x][y] - 'A'] = true;
if(x+1 < R && !visit[map[x+1][y] - 'A']) {
DFS(x+1, y, cnt);
}
if(y+1 < C && !visit[map[x][y+1] - 'A']) {
DFS(x, y+1, cnt);
}
if(x-1 >= 0 && !visit[map[x-1][y] - 'A']) {
DFS(x-1, y, cnt);
}
if(y-1 >= 0 && !visit[map[x][y-1] - 'A']) {
DFS(x, y-1, cnt);
}
visit[map[x][y] - 'A'] = false;
return;
}
int main(void) {
scanf("%d %d\n", &R, &C);
for(int i=0; i<30; i++)
visit[i] = false;
for(int i=0; i<R; i++) {
for(int j=0; j<C; j++) {
scanf("%c", &map[i][j]);
}
getchar();
}
Max = 0;
DFS(0, 0, 0);
printf("%d\n", Max);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment