Advertisement
vfonic

Untitled

Aug 28th, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cctype>
  3. #include <climits>
  4. #include <cmath>
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <cstring>
  8. #include <iostream>
  9. #include <list>
  10. #include <map>
  11. #include <queue>
  12. #include <set>
  13. #include <sstream>
  14. #include <stack>
  15. #include <string>
  16. #include <vector>
  17.  
  18. #define inf 1000000000
  19. #define MAXN 51
  20.  
  21. using namespace std;
  22.  
  23. int sol = 0;
  24. int H, W;
  25. char mat[MAXN][MAXN];
  26.  
  27. int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
  28. int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
  29.  
  30. void dfs(int row, int col, int tmp_sol, char letter) {
  31.     if (tmp_sol > sol) sol = tmp_sol;
  32.  
  33.     for (int i = 0; i < 8; ++i) {
  34.         if (row+dx[i] < 0 || row+dx[i] >= H || col+dy[i] < 0 || col+dy[i] >= W) continue;
  35.  
  36.         if (mat[row+dx[i]][col+dy[i]] == letter) {
  37.             dfs(row+dx[i], col+dy[i], tmp_sol+1, letter+1);
  38.         }
  39.     }
  40. }
  41.  
  42. int main() {
  43.     int _case = 1;
  44.     while (true) {
  45.         scanf("%d%d", &H, &W);
  46.         if (!H && !W) return 0;
  47.  
  48.         sol = 0;
  49.         for (int i = 0; i < H; ++i) {
  50.             scanf("%s", mat[i]);
  51.         }
  52.  
  53.         for (int i = 0; i < H; ++i) {
  54.             for (int j = 0; j < W; ++j) {
  55.                 if (mat[i][j] != 'A') continue;
  56.  
  57.                 dfs(i, j, 1, 'A'+1);
  58.             }
  59.         }
  60.  
  61.         printf("Case %d: %d\n", _case, sol);
  62.     }
  63.    
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement