F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Game on Chessboard

Time Limit: 12000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 350    Accepted Submission(s): 37


Problem Description
Given a chessboard of size n ¡Á n. The rows are numbered 1, 2,... , n from top to bottom while the columns are numbered 1, 2, ... , n from left to right, which means that (1, 1) is the top-left corner and that (n, n) is the bottom-right corner.
There are some chesses in the chessboard. Each chess has a color, white or black, and a cost value w. The number of chesses in one cell does not exceed 1.
You want to remove these chesses. In each time, you can choose one white chess u and one black chess v and then remove them with cost |wu u wv|. Moreover, one chess can be chosen in this operation if and only if there are no other chesses in its bottom-left area. Formally, one chess positioned at (x, y)
can be chosen if and only if there are no other chesses (x1, y1) that x1 ¡Ý x and y1 ¡Ü y.
However, you may be not able to choose two chesses holding the restriction sometimes. So you can also choose exactly one chess x arbitrarily and remove it with cost wx. Moreover, you can do this operation in any situation as long as there is at least one chess on chessboard.
Now you want to remove all the chesses with the minimum total cost. Print the mimimum cost in one line.
 

Input
The first line contains one positive integers n (1 ¡Ü n ¡Ü 12), denoting the size of the chessboard.
Next n lines each contains a string of length n containing only ¡° . ¡±, ¡°W¡± or ¡°B¡±, denoting the initial chessboard, where cell (i, j) is blank if the j-th character in i-th string is ¡° . ¡±, while ¡°W¡± represents a white chess and ¡°B¡± represents a black chess.
Next n lines each contains n non-negative integers wi,j (0 ¡Ü wi,j ¡Ü 106), denoting the cost values, where the j-th number in i-th line represents the cost value of the chess positioned at (i, j).
It is guaranteed that wi,j is positive if and only if there exists a chess at (i, j).
 

Output
Output a single line containing a non-negative integer, denoting the mimimum cost.
 

Sample Input
4 WBB. BWBW WBWW BBBW 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1
 

Sample Output
3
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-05-12 16:51:43, Gzip enabled