您的位置:首页 > 其它

codeforces 426 B. Sereja and Mirroring(递归)

2015-12-22 18:25 246 查看
http://codeforces.com/problemset/problem/426/B

大意:给定一个矩阵,求最小的行数,对应行可以镜像产生新的行,迭代下去直至产生最终的矩阵。

#include <iostream>
#include <cstdio>
using namespace std;
int g[105][105];
int n,m;
bool check(int low,int high,int mid){
if((high-low+1)&1) return 0;
int p1=mid,p2=mid+1;
while(p1>=low&&p2<=high){
for(int j=0;j<m;j++){
if(g[p1][j]!=g[p2][j]) return 0;
}
p1--;
p2++;
}
return 1;
}
int ans=1;
void work(int low,int high){
if(low>=high) {
return ;
}
int mid=(low+high)>>1;
if(!check(low,high,mid)) {
ans=max(ans,high-low+1);
return ;
}
work(low,mid);
//work(mid+1,high); //这一步可以省去
}
int main()
{
//freopen("cin.txt","r",stdin);
while(cin>>n>>m){
ans=1;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&g[i][j]);
}
}
if(n&1){
printf("%d\n",n);
continue;
}
work(0,n-1);
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: