#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define INF 100000000000000000
//#define int long long
#define lchild(i) (i*2 + 1)
#define rchild(i) (i*2 + 2)
#define mid(l, u) ((l+u)/2)
#define x(p) p.first
#define y(p) p.second
#define MOD 998244353
#define ordered_set tree<pair<int, int>, null_type,less<pair<int, int>>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
int n, m;
const int N = 3000;
char mat[N][N];
pair<int, int> nodenum[N][N]; //first->horizontal, second -> vertical
bool vis[2*N*N];
set<int> adj[2*N*N];
int currnode = 0;
int solve(int i){
vector<int> lvls;
queue<pair<int, int>> q;
q.push({i, 0});
vis[i] = true;
while(!q.empty()){
pair<int, int> curr = q.front();
q.pop();
if(lvls.size()<=curr.second) lvls.push_back(1);
else lvls[curr.second]++;
for(int j: adj[curr.first]){
if(vis[j]) continue;
vis[j] = true;
q.push({j, curr.second + 1});
}
}
int tr = 1;
vector<int> dp = {lvls[0]};
if(lvls.size()>=2) dp.push_back(lvls[1]);
for(int i = 2;i<lvls.size();i++){
dp.push_back(max(dp[i-1], lvls[i] + dp[i-2]));
}
for(int i = 0;i<lvls.size();i++){
tr = max(tr, dp[i]);
}
return tr;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m;
for(int i = 0;i<n;i++){
string s;
for(int j = 0;j<m;j++) cin>>mat[i][j];
}
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++) nodenum[i][j] = {-1, -1};
}
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
if(mat[i][j]!='R') continue;
if(i+2<n){
if(mat[i+1][j]=='G' && mat[i+2][j]=='W'){
int cnode = currnode++;
//cout<<cnode<<" is ("<<i<<", "<<j<<") vertical"<<endl;
nodenum[i][j].second = cnode;
if(nodenum[i][j].first != -1){
adj[nodenum[i][j].first].insert(cnode);
adj[cnode].insert(nodenum[i][j].first);
}
if(j>0 && nodenum[i+1][j-1].first!=-1){
adj[nodenum[i+1][j-1].first].insert(cnode);
adj[cnode].insert(nodenum[i+1][j-1].first);
}
if(j>1 && nodenum[i+2][j-2].first!=-1){
adj[nodenum[i+2][j-2].first].insert(cnode);
adj[cnode].insert(nodenum[i+2][j-2].first);
}
}
}
if(j+2<m){
if(mat[i][j+1]=='G' && mat[i][j+2]=='W'){
int cnode = currnode++;
//cout<<cnode<<" is ("<<i<<", "<<j<<") horizontal"<<endl;
nodenum[i][j].first = cnode;
if(nodenum[i][j].second != -1){
adj[nodenum[i][j].second].insert(cnode);
adj[cnode].insert(nodenum[i][j].second);
}
if(i>0 && nodenum[i-1][j+1].second!=-1){
adj[nodenum[i-1][j+1].second].insert(cnode);
adj[cnode].insert(nodenum[i-1][j+1].second);
}
if(i>1 && nodenum[i-2][j+2].second!=-1){
adj[nodenum[i-2][j+2].second].insert(cnode);
adj[cnode].insert(nodenum[i-2][j+2].second);
}
}
}
}
}
int ans = 0;
for(int i = 0;i<currnode;i++){
/*cout<<i<<" is connected to: ";
for(int j: adj[i]){
cout<<j<<" ";
}
cout<<endl;
*/
if(!vis[i]){
ans+=solve(i);
}
}
cout<<ans<<endl;
//create a node out of each skewer that you can make
//connect nodes that intersect with each other
//For each connected component,
}
/*
4 4
RGWR
GRRG
WGGW
WWWR
5 5
RGRGW
GRRGW
WGGWR
RWRGW
RGWGW
*/
Compilation message
dango_maker.cpp: In function 'int solve(int)':
dango_maker.cpp:29:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(lvls.size()<=curr.second) lvls.push_back(1);
~~~~~~~~~~~^~~~~~~~~~~~~
dango_maker.cpp:40:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 2;i<lvls.size();i++){
~^~~~~~~~~~~~
dango_maker.cpp:43:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i<lvls.size();i++){
~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
133 ms |
262144 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
133 ms |
262144 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
133 ms |
262144 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |