hdu4328 最大子矩陣問題O(n*m)掃描思想
http://acm.hdu.edu.cn/showproblem.php?pid=4328
Problem Description
Mark bought a huge cake, because his friend ray_sun’s birthday is coming. Mark is worried about how to divide the cake since it’s so huge and ray_sun is so strange. Ray_sun is a nut, you can never imagine how strange he was, is, and going to be. He does not
eat rice, moves like a cat, sleeps during work and plays games when the rest of the world are sleeping……It is not a surprise when he has some special requirements for the cake. A considering guy as Mark is, he will never let ray_sun down. However, he does
have trouble fulfilling ray_sun’s wish this time; could you please give him a hand by solving the following problem for him?
The cake can be divided into n*m blocks. Each block is colored either in blue or red. Ray_sun will only eat a piece (consisting of several blocks) with special shape and color. First, the shape of the piece should be a rectangle. Second, the color of blocks in the piece should be the same or red-and-blue crisscross. The so called ‘red-and-blue crisscross’ is demonstrated in the following picture. Could you please help Mark to find out the piece with maximum perimeter that satisfies ray_sun’s requirements?
The cake can be divided into n*m blocks. Each block is colored either in blue or red. Ray_sun will only eat a piece (consisting of several blocks) with special shape and color. First, the shape of the piece should be a rectangle. Second, the color of blocks in the piece should be the same or red-and-blue crisscross. The so called ‘red-and-blue crisscross’ is demonstrated in the following picture. Could you please help Mark to find out the piece with maximum perimeter that satisfies ray_sun’s requirements?
Input
The first line contains a single integer T (T <= 20), the number of test cases.
For each case, there are two given integers, n, m, (1 <= n, m <= 1000) denoting the dimension of the cake. Following the two integers, there is a n*m matrix where character B stands for blue, R red.
For each case, there are two given integers, n, m, (1 <= n, m <= 1000) denoting the dimension of the cake. Following the two integers, there is a n*m matrix where character B stands for blue, R red.
Output
For each test case, output the cased number in a format stated below, followed by the maximum perimeter you can find.
Sample Input
2
1 1
B
3 3
BBR
RBB
BBB
Sample Output
Case #1: 4
Case #2: 8
/**
hdu4328 最大子矩陣問題O(n*m)
題目大意:給定一個n*m的棋盤有紅黑兩色,讓擷取一個周長最大矩形,該矩形要麼全是黑色,要麼全是紅色,要麼黑色和紅色交替
見題目附圖。
解題思路:如果遍歷每一個矩陣是不可能的,時間O(n*n*n),因此我們採用掃描的方法(白書P51)。對於交替的矩形我們把(i+j)
為奇數的格子翻轉顏色,那樣就可以轉換成全是一樣顏色了,然後邊成把(i+j)為偶數的翻轉,四種情況取最大即可。複雜度O(n*m)
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=1005;
int mat[maxn][maxn],mat1[maxn][maxn],up[maxn][maxn],lef[maxn][maxn],rig[maxn][maxn];
char a[maxn][maxn];
int n,m;
int get()
{
int ans=0;
for(int i=0; i<m; i++)
{
int lo=-1,ro=n;
for(int j=0; j<n; j++)
{
if(mat[i][j]==1)
{
up[i][j]=lef[i][j]=0;
lo=j;
}
else
{
up[i][j]=i==0?1:up[i-1][j]+1;
lef[i][j]=i==0?lo+1:max(lef[i-1][j],lo+1);
}
}
for(int j=n-1; j>=0; j--)
{
if(mat[i][j]==1)
{
rig[i][j]=n;
ro=j;
}
else
{
rig[i][j]=i==0?ro-1:min(rig[i-1][j],ro-1);
ans=max(ans,up[i][j]*2+2*(rig[i][j]-lef[i][j]+1));
}
}
}
return ans;
}
int main()
{
int tt=0,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
for(int i=0; i<m; i++)
{
scanf("%s",a[i]);
}
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
if(a[i][j]=='R')
mat1[i][j]=mat[i][j]=1;
else
mat1[i][j]=mat[i][j]=0;
}
}
int ans=-1;
///1
ans=max(ans,get());
///2
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
if(mat1[i][j]==1)
mat[i][j]=0;
else
mat[i][j]=1;
}
}
ans=max(ans,get());
///3
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
if((i+j)%2==1)
{
if(mat1[i][j]==1)
mat[i][j]=0;
else
mat[i][j]=1;
}
else
mat[i][j]=mat1[i][j];
}
}
/* for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
printf("%d",mat[i][j]);
}
printf("\n");
}
printf("\n");*/
ans=max(ans,get());
///4
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
if((i+j)%2==0)
{
if(mat1[i][j]==1)
mat[i][j]=0;
else
mat[i][j]=1;
}
else
mat[i][j]=mat1[i][j];
}
}
/*for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
printf("%d",mat[i][j]);
}
printf("\n");
}
printf("\n");*/
ans=max(ans,get());
printf("Case #%d: %d\n",++tt,ans);
}
return 0;
}
/**
100
3 3
BBR
RBR
BBB
2 2
BR
RB
2 2
BB
RR
2 2
BB
BB
*/
相關文章
- 最大子段和 | 最大子矩陣 | 最大M子段和矩陣
- 最大子矩陣和矩陣
- C51矩陣鍵盤掃描程式矩陣
- CF22B 最大子矩陣問題簡單版矩陣
- #1502 : 最大子矩陣矩陣
- 51微控制器非常簡潔的矩陣掃描程式矩陣
- poj1050 最大子矩陣和矩陣
- 最大子陣列問題(Maximum subarray problem)陣列
- 最大子陣列和問題的解陣列
- 矩陣連乘問題矩陣
- POJ 1050-To the Max(最大子矩陣和)矩陣
- 生成一個掃雷矩陣矩陣
- 優化Oracle with全表掃描的問題優化Oracle
- Fotify掃描問題Dynamic Code Evaluation:Code Injection
- 演算法題:矩陣鏈乘問題演算法矩陣
- awvs -網站掃描問題求指導網站
- 優化Oracle with全表掃描的問題(二)優化Oracle
- 【原創】生成n*n蛇形矩陣的演算法矩陣演算法
- 演算法題系列:矩陣鏈乘問題演算法矩陣
- 掃描技術和掃描工具
- openjudge1768 最大子矩陣[二維字首和or遞推|DP]矩陣
- 動態規劃-最大子矩陣和(ZOJ 1074 TO THE MAX )動態規劃矩陣
- LeetCode 周賽上分之旅 #45 精妙的 O(lgn) 掃描演算法與樹上 DP 問題LeetCode演算法
- python之矩陣相加:提示使用者輸入矩陣的行數n,再提示使用者輸入矩陣的列數m,接下來,提示使用者輸入 2*n*m 個數字(每次輸入 一個數字)。輸出 C=A+B。Python矩陣
- 關於分割槽表中的全partition掃描問題
- bzoj1084: [SCOI2005]最大子矩陣(Dp)矩陣
- PHP掃描圖片轉點陣 二維碼轉點陣PHP
- AWVS掃描器掃描web漏洞操作Web
- 利用瓊斯矩陣求解一般偏振問題矩陣
- zt:東軟醫保動態庫全表掃描問題
- 使用全表掃描快取大表的相關問題快取
- win10系統掃描器提示掃描不到掃描器如何解決Win10
- 掃描王 for Mac專業圖片掃描工具Mac
- Nmap掃描教程之基礎掃描詳解
- 【工具篇】最流行的Web漏洞掃描工具推薦!Web
- 線性時間選擇(含平均情況O(n)和最壞情況O(n)演算法)演算法
- 矩陣連乘問題 Python 動態規劃矩陣Python動態規劃
- 第四章:多維陣列和矩陣 ------------- 4.4 找出邊界為1的最大子方陣陣列矩陣