#arc177c. C - Routing

C - Routing

Score: 500500 points

问题陈述

有一个 NNNN 列的网格。设 (i,j)(i, j) (1iN,1jN)(1 \leq i \leq N, 1 \leq j \leq N) 表示从顶部数第 ii 行,从左边数第 jj 行的单元格。每个单元格最初被涂成红色或蓝色,如果 ci,j=c_{i,j}=R 则单元格 (i,j)(i, j) 是红色的,如果 ci,j=c_{i,j}=B 则单元格是蓝色的。你想要将一些单元格的颜色改为紫色,以便同时满足以下两个条件:

条件 1:你可以通过只经过红色或紫色的单元格,从单元格 (1,1)(1, 1) 移动到单元格 (N,N)(N, N)条件 2:你可以通过只经过蓝色或紫色的单元格,从单元格 (1,N)(1, N) 移动到单元格 (N,1)(N, 1)

在这里,“你可以移动”意味着你可以通过重复移动到目标单元格的水平或垂直相邻单元格来到达目的地,这些单元格具有相关的颜色。

为了满足这些条件,必须将最少多少个单元格改为紫色?

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

There is a grid with NN rows and NN columns. Let (i,j)(i, j) (1iN,1jN)(1 \leq i \leq N, 1 \leq j \leq N) denote the cell at the ii-th row from the top and the jj-th column from the left. Each cell is initially painted red or blue, with cell (i,j)(i, j) being red if ci,j=c_{i,j}=R and blue if ci,j=c_{i,j}=B. You want to change the colors of some cells to purple so that the following two conditions are simultaneously satisfied:

Condition 1: You can move from cell (1,1)(1, 1) to cell (N,N)(N, N) by only passing through cells that are red or purple.
Condition 2: You can move from cell (1,N)(1, N) to cell (N,1)(N, 1) by only passing through cells that are blue or purple.

Here, "You can move" means that you can reach the destination from the starting point by repeatedly moving to a horizontally or vertically adjacent cell of the relevant colors.

What is the minimum number of cells that must be changed to purple to satisfy these conditions?

Constraints

  • 3N5003 \leq N \leq 500
  • Each ci,jc_{i,j} is R or B.
  • c1,1c_{1,1} and cN,Nc_{N,N} are R.
  • c1,Nc_{1,N} and cN,1c_{N,1} are B.
  • NN is an integer.

Input

The input is given from Standard Input in the following format:

NN

c1,1c_{1,1}c1,2c_{1,2}\cdotsc1,Nc_{1,N}

c2,1c_{2,1}c2,2c_{2,2}\cdotsc2,Nc_{2,N}

\vdots

cN,1c_{N,1}cN,2c_{N,2}\cdotscN,Nc_{N,N}

Output

Print the answer.

Sample Input 1

5
RBRBB
RBRRR
RRRBR
RBBRB
BBRBR

Sample Output 1

3

As shown in the figure below, changing cells (1,3),(3,2),(4,5)(1, 3), (3, 2), (4, 5) to purple satisfies the conditions.

Sample Input 2

5
RBBBB
BBBBB
BBBBB
BBBBB
BBBBR

Sample Output 2

7

As shown in the figure below, changing cells $(1, 2), (2, 2), (2, 3), (3, 3), (3, 4), (4, 4), (4, 5)$ to purple satisfies the conditions.

Sample Input 3

10
RRBBBBBBBB
BRRBBBBBBB
BBRRBBBBBB
BBBRRBBBBB
BBBBRRBBBB
BBBBBRRBBB
BBBBBBRRBB
BBBBBBBRRB
BBBBBBBBRR
BBBBBBBBBR

Sample Output 3

2

Sample Input 4

17
RBBRRBRRRRRBBBBBB
BBRBRBRRBRRBRRBBR
BRBRBBBRBBRBBRBBB
RBRRBBBBBBRRBRRRR
RRRRRBRBRRRBBRBBR
RRRRRBRRBRBBRRRBB
BBBRRRBRBRBBRRRBB
BBRRRBRBBBRBRRRBR
RRBBBBBBBBBBBRBRR
RRRBRRBRBRBRBRBBB
RRBRRRRBRBRRBRBBR
RRRBBRBRBBBRBBRBR
BBRBBRRBRRRBBRBBB
BBBRBRRRRRRRBBRBB
RRRRRBRBRBBRRBRRR
BRRRRBBBRRRBRRBBB
BBRRBBRRRBBBRBBBR

Sample Output 4

8
}