zoukankan      html  css  js  c++  java
  • [dp]Find Symmetries

    题目描述

    Snuke has two boards, each divided into a grid with N rows and N columns. For both of these boards, the square at the i-th row from the top and the j-th column from the left is called Square (i,j).
    There is a lowercase English letter written in each square on the first board. The letter written in Square (i,j) is Si,j. On the second board, nothing is written yet.
    Snuke will write letters on the second board, as follows:
    First, choose two integers A and B ( 0≤A,B<N ).
    Write one letter in each square on the second board. Specifically, write the letter written in Square (i+A,j+B) on the first board into Square (i,j) on the second board. Here, the k-th row is also represented as the (N+k)-th row, and the k-th column is also represented as the (N+k)-th column.
    After this operation, the second board is called a good board when, for every i and j ( 1≤i,j≤N ), the letter in Square (i,j) and the letter in Square (j,i) are equal.
    Find the number of the ways to choose integers A and B ( 0≤A,B<N ) such that the second board is a good board.

    Constraints
    1≤N≤300
    Si,j ( 1≤i,j≤N ) is a lowercase English letter.

    输入

    Input is given from Standard Input in the following format:
    N
    S1,1S1,2..S1,N
    S2,1S2,2..S2,N
    :
    SN,1SN,2..SN,N

    输出

    Print the number of the ways to choose integers A and B ( 0≤A,B<N ) such that the second board is a good board.

    样例输入

    2
    ab
    ca
    

    样例输出

    2
    

    提示

    For each pair of A and B, the second board will look as shown below:
    ***
    The second board is a good board when (A,B)=(0,1) or (A,B)=(1,0), thus the answer is 2.

    扩展成(2n-1)*(2n-1),计算扩展后的正方形内有多少长度为n的子方形,dp[i][j]表示使以i,j为右下脚坐标的子方形呈对称的最大边长。
    #include <iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    char Map[605][605];
    int dp[605][605];
    int n,A,B;
    
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                scanf(" %c",&Map[i][j]);
                Map[i+n][j]=Map[i][j+n]=Map[i+n][j+n]=Map[i][j];
            }
        }
        int ans=0;
        for(int i=1;i<=2*n-1;i++){
            for(int j=1;j<=2*n-1;j++){
                int cnt=0;
                for(int k=1;k<=min(i-1,j-1);k++){
                    if(Map[i][j-k]==Map[i-k][j]) cnt++;
                    else break;
                }
                dp[i][j]=1+min(dp[i-1][j-1],cnt);
                if(dp[i][j]>=n) ans++;
            }
        }
        printf("%d
    ",ans);
        return 0;
    }
    View Code
     
  • 相关阅读:
    P1162 填涂颜色
    P1238 走迷宫
    U68364 _GC滑迷宫
    P4783 【模板】矩阵求逆
    P2613 有理数取余
    koa1链接mongodb
    koa的跨域访问
    mongodb 降序
    mongodb 分页(limit)
    mongodb 查询数据
  • 原文地址:https://www.cnblogs.com/lllxq/p/10592985.html
Copyright © 2011-2022 走看看