This page is still under construction.

Parts of this page are still being built. What you see may change.

Fashion Show

Time limit5sMemory limit512 MB

Summary
Place models on an N by N grid (or upgrade existing ones) so every shared row or column has a plus and every shared diagonal has an x, maximizing style points.
Level

Hard9 of 10

Topics
Greedy, Graph, Binary search, Math
Solved
No attempts yet

Problem

You are hosting a fashion show for three new styles of clothing. The stage is an N×NN \times N grid of cells.

Each cell is either empty, written as ., or holds one model. A model wears one of three styles: +, x, or o. A cell with a + model or an x model adds 1 style point. A cell with an o model adds 2 style points. An empty cell adds nothing.

Two rules govern how the models stand relative to each other.

  • Whenever two models share a row or a column, at least one of the two is a +.
  • Whenever two models share a diagonal, at least one of the two is an x.

A model in row i0i_0 and column j0j_0 and a model in row i1i_1 and column j1j_1 share a row if i0=i1i_0 = i_1, share a column if j0=j1j_0 = j_1, and share a diagonal if i0+j0=i1+j1i_0 + j_0 = i_1 + j_1 or i0−j0=i1−j1i_0 - j_0 = i_1 - j_1.

The grid below breaks both rules.

...
x+o
.+.

The middle row holds the pair x and o, and neither of them is a +. The diagonal that runs from the + in the bottom row up to the o in the middle row holds two models, and neither of them is an x.

The grid below is legal. No row, column, or diagonal breaks a rule.

+.x
+x+
o..

Your advisor has already placed MM models, and that placement follows both rules. You may add any number of models of any styles, including none at all. You may not remove a model, but you may upgrade as many of the existing + and x models into o models as you wish, as long as the rules still hold.

Print the largest total number of style points you can reach.

Input

The first line has the number of test cases TT. Each test case begins with a line holding two integers NN and MM. Then MM lines follow, and the ii-th of them holds one character (+, x, or o, the style of the model) and two integers RiR_i and CiC_i, the row and the column of the model. Rows are numbered 1 to NN from top to bottom, and columns are numbered 1 to NN from left to right.

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 1 and yy is the largest total number of style points.

Constraints

  • 1≤T≤1001 \le T \le 100
  • 1≤N≤1001 \le N \le 100
  • 0≤M≤N20 \le M \le N^2
  • 1≤Ri≤N1 \le R_i \le N and 1≤Ci≤N1 \le C_i \le N for every ii
  • No two pre-placed models sit in the same cell.
  • The pre-placed models follow both rules.

Notes

The first example holds three test cases.

In test case 1 the stage is 2 by 2 and starts empty. The arrangement below earns 4 points.

x.
+o

In test case 2 the only cell already holds an o model, so nothing can be added or changed, and the score is 2.

In test case 3 the stage starts like this.

...
+++
x..

The arrangement below earns 6 points.

.x.
++o
x..

Examples2

  1. Example 1

    Input
    3
    2 0
    1 1
    o 1 1
    3 4
    + 2 3
    + 2 1
    x 3 1
    + 2 2
    
    Expected output
    Case #1: 4
    Case #2: 2
    Case #3: 6
    
  2. Example 2

    Input
    4
    1 0
    2 0
    3 0
    4 0
    
    Expected output
    Case #1: 2
    Case #2: 4
    Case #3: 7
    Case #4: 10