-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathE_ABC_Path.cpp
More file actions
92 lines (71 loc) · 1.9 KB
/
E_ABC_Path.cpp
File metadata and controls
92 lines (71 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <limits>
#include <numeric>
#define int long long
using namespace std;
int h , w;
vector<vector<char>> grid;
vector<vector<int>> dp;
vector<vector<int>> dirs = {{0,-1} , {0,1} , {1,0} , {-1,0} , {1,1} , {-1,1} , {1,-1} , {-1,-1}};
void solve() {
int t = 1;
while(true){
cin >> h >> w;
if(h == 0) break;
grid.assign(h+1 , vector<char>(w+1));
dp.assign(h+1 , vector<int>(w+1 , 1));
vector<pair<char,pair<int,int>>> arr;
for(int i=1 ; i<=h ; i++){
for(int j=1 ; j<=w ; j++){
cin >> grid[i][j];
arr.push_back({grid[i][j] , {i , j}});
}
}
sort(arr.begin() , arr.end());
int ans = 0;
for(int i=arr.size()-1 ; i>=0 ; i--){
int cv = arr[i].first;
int cx = arr[i].second.first;
int cy = arr[i].second.second;
for(int i=0 ; i<8 ; i++){
int nx = cx+dirs[i][0];
int ny = cy+dirs[i][1];
if(nx>=1 && ny>=1 && nx<=h && ny<=w && grid[nx][ny]==grid[cx][cy]-1 && dp[nx][ny]<1+dp[cx][cy]){
dp[nx][ny] = 1+dp[cx][cy];
}
}
}
for(int i=1; i<=h ; i++){
for(int j=1 ; j<=w ; j++){
if(grid[i][j] == 'A') ans = max(ans , dp[i][j]);
}
}
cout << "Case " << t << ": " << ans << endl;
t++;
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int _t = 1;
// cin >> t;
while(_t--){
solve();
}
return 0;
}