Shubham and Grid
Practice
4.8 (4 votes)
Medium
Maximum flow
Problem
87% Success 2187 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

  1. The letters on these cells form a permutation of the set {\(a,b,c,d\)}.
  2. The cell marked b is directly reachable from cell marked a.
  3. The cell marked c is directly reachable from the cell marked b.
  4. 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

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
3 votes
Tags:
AlgorithmsGraphsMax flowMaximum Bipartite MatchingMaximum flowMediummaxflow
Points:50
28 votes
Tags:
Dinic's algorithmGraphsHash MapsMediumTrees
Points:50
3 votes
Tags:
AlgorithmsFlowsGraphsMaximum flowMedium