fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define endl "\n"
  4. #define vi vector<int>
  5. #define all(v) v.begin(), v.end()
  6. #define SORT(v) sort(all(v))
  7. #define SORTX(v) sort(v.begin(), v.end(), greater<int>())
  8. #define read(a) for (auto &i : a) cin >> i
  9. #define Printv(a) for (auto &i : a) cout << i << " "
  10. #define M_ShahaT ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  11. #define files freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
  12.  
  13.  
  14.  
  15. using namespace std;
  16. /*
  17.   وَأَن لَّيْسَ لِلْإِنسَانِ إِلَّا مَا سَعَى ﴿39﴾
  18.   وَأَنَّ سَعْيَهُ سَوْفَ يُرَى ﴿40﴾
  19.   ثُمَّ يُجْزَاهُ الْجَزَاء الْأَوْفَى ﴿41﴾
  20. */
  21. int dx[] = {1, 0, -1, 0, -1, -1, 1, 1};
  22. int dy[] = {0, -1, 0, 1, -1, 1, -1, 1};
  23. char di[] = {'D', 'L', 'U', 'R'};
  24. const int N=1e5+5;
  25.  
  26.  
  27. int n,m;
  28. vector<int>adj[N];
  29.  
  30. vector<vector<char>>grid;
  31. vector<vector<bool>>vis;
  32. bool valid(int i,int j){
  33. return i>=0 && i<n &&j>=0&&j<m;
  34. }
  35. int ans=0;
  36. void dfs(int i,int j,int cnt=1){
  37. vis[i][j]=true;
  38. ans=max(ans,cnt);
  39. for(int d=0;d<8;d++){
  40. int nx=dx[d]+i;
  41. int ny=dy[d]+j;
  42. if(valid(nx,ny) && !vis[nx][ny] && grid[nx][ny]==grid[i][j]+1) {
  43. dfs(nx,ny,cnt+1);
  44. }
  45. }
  46. vis[i][j]=false;
  47.  
  48.  
  49. }
  50.  
  51. void solve_case() {
  52. // while(true) {
  53. cin >> n >> m;
  54. //if(n==0&&m==0)break;
  55. grid.resize(n, vector<char>(m));
  56. vis.assign(n, vector<bool>(m, false));
  57. for (int i = 0; i < n; i++) {
  58. for (int j = 0; j < m; j++) {
  59. cin >> grid[i][j];
  60.  
  61. }
  62. }
  63. int out = 0;
  64. for (int i = 0; i < n; i++) {
  65. for (int j = 0; j < m; j++) {
  66. if (grid[i][j] == 'A') {
  67. ans = 0;
  68.  
  69. vis.assign(n, vector<bool>(m, false));
  70. dfs(i, j);
  71. out = max(ans, out);
  72. }
  73. }
  74. }
  75.  
  76. cout <<"Case 1: "<< out << endl;
  77. }
  78. //}
  79.  
  80.  
  81. int main() {
  82. M_ShahaT
  83.  
  84.  
  85. int t=1;
  86.  
  87. // cin >> t;
  88. for(int i=1;i<=t;i++){
  89.  
  90. solve_case();
  91.  
  92. }
  93. return 0;
  94. }
Success #stdin #stdout 0.01s 5904KB
stdin
Standard input is empty
stdout
Case 1: 0