多校练习赛2 Problem 1008 Palindrome Sub-Array 搜索+矩阵翻转

73 篇文章 21 订阅
56 篇文章 0 订阅

Palindrome Sub-Array

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 608    Accepted Submission(s): 296


Problem Description
  A palindrome sequence is a sequence which is as same as its reversed order. For example, 1 2 3 2 1 is a palindrome sequence, but 1 2 3 2 2 is not. Given a 2-D array of N rows and M columns, your task is to find a maximum sub-array of P rows and P columns, of which each row and each column is a palindrome sequence.
 

Input
  The first line of input contains only one integer, T, the number of test cases. Following T blocks, each block describe one test case.
  There is two integers N, M (1<=N, M<=300) separated by one white space in the first line of each block, representing the size of the 2-D array.
  Then N lines follow, each line contains M integers separated by white spaces, representing the elements of the 2-D array. All the elements in the 2-D array will be larger than 0 and no more than 31415926.
 

Output
  For each test case, output P only, the size of the maximum sub-array that you need to find.
 

Sample Input
  
  
1 5 10 1 2 3 3 2 4 5 6 7 8 1 2 3 3 2 4 5 6 7 8 1 2 3 3 2 4 5 6 7 8 1 2 3 3 2 4 5 6 7 8 1 2 3 9 10 4 5 6 7 8
 

Sample Output
  
  
4
 

Source
 

Recommend
zhuyuanchen520
 


题解
当初以为是动规。。一直不敢做。。  后来知道是搜索之后。。才明白自己有多傻。。
搜索时,就把每个点作为中心点。求最长的长度,并搜索记录答案,搜索时,边分奇数跟偶数。。然后分别跟4个地方的数字进行比较。
看搜索代码就清楚了。

找到对称点后,因为是一个对称的矩形,所以把矩形翻转起来进行比较就好了。。 唉。可惜当时没敢做。。
/*
 * @author ipqhjjybj
 * @date  20130727
 *
 */

#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

#include <iostream>
#include <cmath>
#include <algorithm>

#include <cstring>
#include <vector>
#include <string>
using namespace std;

#define inf 0x3f3f3f3f
#define MAXN 1000
#define clr(x,k) memset((x),(k),sizeof(x))

typedef vector<int> vi;
#define foreach(it,c) for(vi::iterator it = (c).begin();it != (c).end();++it)

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
int a[400][400];
bool search(int x,int y,int step){
    int deep=step;
    if(step&1){
        step--;
        step>>=1;
        for(int i=x-step;i<=x;i++)
            for(int j=y-step;i>0&&j<=y;j++){
                if(a[i][j]!=-1&&a[i][j]==a[2*x-i][j]&&a[i][j]==a[i][2*y-j]&&a[i][j]==a[2*x-i][2*y-j])
                    continue;
                else{
                     return false;
                }
        }
    }else{
        step*=step;
        step/=4;
        step=int(sqrt(step+0.01));
        step-=1;
        for(int i=x-step;i<=x;i++)
            for(int j=y-step;j<=y;j++){
                if(a[i][j]!=-1&&a[i][j]==a[i][2*y-j+1]&&a[i][j]==a[2*x-i+1][j]&&a[i][j]==a[2*x-i+1][2*y-j+1])
                    continue;
                else{
                    return false;
                }
            }
    }
    return true;
}
#define deal(x) ((x)>>1)
#define kill(x) ((x+1)>>1)
int main(){
    //freopen("4618.in","r",stdin);
    int t;
    int n,m;
    scanf("%d",&t);
    while(t--){
        clr(a,-1);
        scanf("%d %d",&n,&m);
        for(int i = 1;i<=n;i++)
            for(int j=1;j<=m;j++)
                scanf("%d",&a[i][j]);
        int ans=1;
        for(int i = 1;i <=n && (n-deal(i))>=deal(ans);i++){
            for(int j=1;j<=m && (m-deal(j))>=deal(ans);j++){
                if(j>=deal(ans)){
                    int temp=0,step=ans+1;
                    bool flag=true;
                    while(deal(step)<=i&&i+deal(step)<=n&&deal(step)<=j&&j+deal(step)<=m&&flag)
                    {
                            flag=false;
                            if(search(i,j,step)){
                                flag=true;
                                temp=max(temp,step);
                                step++;
                            }
                            if(search(i,j,step+1)){
                                flag=true;
                                temp=max(temp,step+1);
                                step+=2;
                            }
                    }
                    ans=max(ans,temp);
                }else{
                    continue;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值