Shubham and Grid
Practice
4.8 (4 votes)
Medium
Maximum flow
Problem
87% Success 2188 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code
You are given a grid of n rows and m columns consisting of lowercase English letters a, b, c, and d.
We say 4 cells form a "nice-quadruple" if and only if
- The letters on these cells form a permutation of the set {\(a,b,c,d\)}.
- The cell marked b is directly reachable from cell marked a.
- The cell marked c is directly reachable from the cell marked b.
- The cell marked d is directly reachable from the cell marked c.
A cell \((x2,y2)\) is said to be directly reachable from cell \((x1,y1)\) if and only if \((x2,y2)\) is one of these 4 cells { \((x1-1,y1)\), \((x1+1,y1)\), \((x1,y1-1)\) and \((x1,y1+1)\)}).
Given the constraint that each cell can be part of at most 1 "nice-quadruple", what's the maximum number of "nice-quadruples" you can select?
Input format
- First line: Two space-separated integers n and m
- Next n lines: Each contains m space separated lowercase letters from the set {\(a,b,c,d\)} denoting the grid cells.
Output format
Output the maximum number of "nice-quadruple" you can select.
Constraints
\(1 \le n,m \le 20\)
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
3 votes
Tags:
AlgorithmsFlowsGraphsMaximum flowMedium
2.Replace
Points:50
28 votes
Tags:
Dinic's algorithmGraphsHash MapsMediumTrees
Points:50
3 votes
Tags:
AlgorithmsGraphsMax flowMaximum Bipartite MatchingMaximum flowMediummaxflow
Editorial
Login to unlock the editorial