Codeforces Round #576 (Div. 1) D. Rectangle Painting 1

D. Rectangle Painting 1

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

There is a square grid of size n×n. Some cells are colored in black, all others are colored in white. In one operation you can select some rectangle and color all its cells in white. It costs max(h,w) to color a rectangle of size h×w. You are to make all cells white for minimum total cost.

Input

The first line contains a single integer n (1≤n≤50) — the size of the square grid.
Each of the next n lines contains a string of length n, consisting of characters ‘.’ and ‘#’. The j-th character of the i-th line is ‘#’ if the cell with coordinates (i,j) is black, otherwise it is white.

Output

Print a single integer — the minimum total cost to paint all cells in white.

Examples

input

3
###
#.#
###

output

3

input

3
...
...
...

output

0

inputCopy

4
#...
....
....
#...

output

2

input

5
#...#
.#.#.
.....
.#...
#....

output

5

题意为有一个方形的区域,有白块和黑块,一次可以将一个矩形区域内的所有方块变成白色,代价为max(m,n),即矩形的长边。求最小代价。
这个问题可以使用dp解决。主要就是将这个大问题分解成小问题。f[x][y][z][w]表示使(x,y),(z,w)两点之间的区域全变成白色所需要的最低代价。
这里将整个问题不断分解成两个小问题,试探所有可能,来求区域最小。递归的方法比较节省代码,同时更容易理解。

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;
 
public class Main {
    static StreamTokenizer st = new StreamTokenizer(new BufferedInputStream(System.in));
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter pr = new PrintWriter(new BufferedOutputStream(System.out));
    static Scanner sc = new Scanner(System.in);
 
//        long tic = System.currentTimeMillis();
//        long toc = System.currentTimeMillis();
//        System.out.println("Elapsed time: " + (toc - tic) + " ms");
    static int f[][][][] = new int[55][55][55][55];
    static String s[];
 
    public static void main(String[] args) {
        int n = sc.nextInt();
        s = new String[n+3];
        for (int i = 1; i <= n; i++) &#123;
            s[i] = " "+sc.next();
        &#125;
        for (int i = 0; i < 55; i++) &#123;
            for (int j = 0; j < 55; j++) &#123;
                for (int k = 0; k < 55; k++) &#123;
                    for (int p = 0; p < 55; p++) &#123;
                        f[i][j][k][p] = -1;
                    &#125;
                &#125;
            &#125;
        &#125;
        System.out.println(dfs(1, 1, n, n));
 
    &#125;
 
    static int dfs(int x, int y, int z, int w) &#123;
        if (f[x][y][z][w] != -1)
            return f[x][y][z][w];
        if (x == z && y == w)
            return f[x][y][z][w] = (s[x].charAt(y) == '#' ? 1 : 0);
        int ret = Math.max(z - x + 1, w - y + 1);
        for (int i = x; i < z; i++)
            ret = Math.min(dfs(x, y, i, w) + dfs(i + 1, y, z, w), ret);
        for (int i = y; i < w; i++)
            ret = Math.min(dfs(x, y, z, i) + dfs(x, i + 1, z, w), ret);
        return f[x][y][z][w] = ret;
    &#125;
 
    static int nextNum() &#123;
        try &#123;
            st.nextToken();
        &#125; catch (IOException e) &#123;
            e.printStackTrace();
        &#125;
        return (int) st.nval;
    &#125;
 
    static int log2(long x) &#123;
        int flag = 1;
        int log = 0;
        while (x > flag) &#123;
            flag <<= 1;
            log++;
        &#125;
        return log;
    &#125;
 
    static long phi(int x) &#123;
        long res = x;
        for (long i = 2; i <= Math.sqrt(x); i++) &#123;
            if (x % i == 0) &#123;
                while (x % i == 0)
                    x /= i;
                res = res / i * (i - 1);
            &#125;
        &#125;
        if (x > 1)
            res = res / x * (x - 1);
        return res;
    &#125;
 
    static long pow(long a, long b, long mod) &#123;
        if (b == 0)
            return 1;
        if (b == 1)
            return a;
        if ((b & 1) == 0)
            return pow(a * a % mod, b >> 1, mod);
        else
            return a * pow(a * a % mod, b >> 1, mod) % mod;
    &#125;
 
    static long gcd(long a, long b) &#123;
        if (b == 0) &#123;
            return a;
        &#125; else &#123;
            return gcd(b, a % b);
        &#125;
    &#125;
 
    static int nextInt() &#123;
        try &#123;
            st.nextToken();
        &#125; catch (IOException e) &#123;
            e.printStackTrace();
        &#125;
        return (int) st.nval;
    &#125;
 
    static double nextDouble() &#123;
        try &#123;
            st.nextToken();
        &#125; catch (IOException e) &#123;
            e.printStackTrace();
        &#125;
        return st.nval;
    &#125;
 
    static String next() &#123;
        try &#123;
            st.nextToken();
        &#125; catch (IOException e) &#123;
            e.printStackTrace();
        &#125;
        return st.sval;
    &#125;
 
    static long nextLong() &#123;
        try &#123;
            st.nextToken();
        &#125; catch (IOException e) &#123;
            e.printStackTrace();
        &#125;
        return (long) st.nval;
    &#125;
&#125;
 
class Node &#123;
    int a;
    int b;
    int c;
 
    public Node() &#123;
    &#125;
 
    public Node(int a, int b, int c) &#123;
        super();
        this.a = a;
        this.b = b;
        this.c = c;
    &#125;
 
    @Override
    public String toString() &#123;
        return "Node [a=" + a + ", b=" + b + "]";
    &#125;
&#125;

   转载规则


《Codeforces Round #576 (Div. 1) D. Rectangle Painting 1》 xfx 采用 知识共享署名 4.0 国际许可协议 进行许可。
 上一篇
The Preliminary Contest for ICPC Asia Nanjing 2019 B_super_log The Preliminary Contest for ICPC Asia Nanjing 2019 B_super_log
super_log题意是有个一函数log a*(x) ,x<1时为-1,x>1为1+log a*(log a(x))给你a,b,m让求最小使不等式(log a*(x)>=b)成立的最小x%m这题可以转换为求a的a次方的a次
2019-09-04
下一篇 
Educational Codeforces Round 68 D.1-2-K Game Educational Codeforces Round 68 D.1-2-K Game
D. 1-2-K Game time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAlic
2019-07-15
  目录