fork(3) download
  1. #include<bits/stdc++.h>
  2. #define ff first
  3. #define ss second
  4. #define mp make_pair
  5. #define mxN 1000000+100
  6. using namespace std;
  7. char a[55][55];
  8. int dis[55][55];
  9. int n,m;
  10. bool vis[55][55];
  11. int fx[] = {1,1,1,-1,-1,-1,0,0};
  12. int fy[] = {1,-1,0,1,-1,0,1,-1};
  13. int bfs(int x,int y){
  14. memset(vis,0,sizeof(vis));
  15. queue<pair<int,int>>q;
  16. q.push(mp(x,y));
  17. char c = 'A';
  18. int mx = 0;
  19. dis[x][y] = 1;
  20. while(!q.empty()){
  21. int u = q.front().ff;
  22. int v = q.front().ss;
  23. q.pop();
  24. c++;
  25. for(int i = 0; i < 8; i++){
  26. int xx = u + fx[i];
  27. int yy = v + fy[i];
  28. if(a[xx][yy] == (a[u][v]+1) && xx >= 0 && yy >= 0 && xx < n && yy < m && vis[xx][yy] == 0){
  29. q.push(mp(xx,yy));
  30. vis[xx][yy] = 1;
  31. dis[xx][yy] = dis[u][v] + 1;
  32. c = a[u][v] + 1;
  33. mx = max(mx,dis[xx][yy]);
  34. }
  35. }
  36. }
  37. return mx;
  38. }
  39.  
  40. void solve()
  41. {
  42. int cas = 0;
  43. while(cin >> n >> m){
  44. if(n == 0 || m == 0)break;
  45. printf("Case %d: ", ++cas);
  46. int ans = 0;
  47. for(int i = 0; i < n; i++){
  48. for(int j = 0; j < m; ++j){
  49. cin >> a[i][j];
  50. }
  51. }
  52. for(int i = 0; i < n; i++){
  53. for(int j = 0; j < m; ++j){
  54. if(a[i][j] == 'A'){
  55. ans = max(ans,bfs(i,j));
  56. }
  57. }
  58. }
  59. printf("%d\n", ans);
  60. }
  61. }
  62. int main()
  63. {
  64. //freopen("inp.txt","r",stdin);
  65. //freopen("out.txt","w",stdout);
  66. int t = 1;
  67. //scanf("%d",&t);
  68. while(t--)solve();
  69.  
  70. return 0;
  71. }
Success #stdin #stdout 0s 4284KB
stdin
Standard input is empty
stdout
Standard output is empty