22B - Bargaining Table - CodeForces Solution


brute force dp *1500

Please click on ads to support us..

Python Code:

n,m=map(int,input().split())
MAP=[input().strip() for i in range(n)]


S=[[0]*(m+5) for i in range(n+5)]

for i in range(n):
    for j in range(m):
        if MAP[i][j]=="1":
            S[i][j]=1

for i in range(1,n):
    for j in range(m):
        S[i][j]+=S[i-1][j]

for i in range(n):
    for j in range(1,m):
        S[i][j]+=S[i][j-1]

ANS=0

for i in range(n):
    for j in range(m):
        for k in range(i,n):
            for l in range(j,m):
                if S[k][l]-S[k][j-1]-S[i-1][l]+S[i-1][j-1]==0:
                    ANS=max(ANS,((k-i)+(l-j)+2)*2)

print(ANS)

C++ Code:

#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <map>
#include <algorithm>
using namespace std;

string str[30];
bool boxHas(int startRow, int endRow, int startCol, int endCol)
{
	for (int i = startRow; i <= endRow; i++)
	{
		for (int j = startCol; j <= endCol; j++)
		{
			if (str[i][j] == '1') return true;
		}
	}
	return false;
}
int main()
{
	int n, m; cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		cin >> str[i];
	}
	int ans = 0;
	for (int startRow = 0; startRow < n; startRow++)
	{
		for (int endRow = startRow; endRow < n; endRow++)
		{
			for (int startCol = 0; startCol < m; startCol++)
			{
				for (int endCol = startCol; endCol < m; endCol++)
				{
					if (boxHas(startRow, endRow, startCol, endCol)) {
						continue;
					}
					ans = max(ans, (endRow - startRow + 1) * 2 + (endCol - startCol + 1) * 2);
				}
			}
		}
	}

	cout << ans << endl;
}
 	 	      	  	 		 	  	 		  	 		


Comments

Submit
0 Comments
More Questions

1271A - Suits
259B - Little Elephant and Magic Square
1389A - LCM Problem
778A - String Game
1382A - Common Subsequence
1512D - Corrupted Array
667B - Coat of Anticubism
284B - Cows and Poker Game
1666D - Deletive Editing
1433D - Districts Connection
2B - The least round way
1324A - Yet Another Tetris Problem
246B - Increase and Decrease
22E - Scheme
1566A - Median Maximization
1278A - Shuffle Hashing
1666F - Fancy Stack
1354A - Alarm Clock
1543B - Customising the Track
1337A - Ichihime and Triangle
1366A - Shovels and Swords
919A - Supermarket
630C - Lucky Numbers
1208B - Uniqueness
1384A - Common Prefixes
371A - K-Periodic Array
1542A - Odd Set
1567B - MEXor Mixup
669A - Little Artem and Presents
691B - s-palindrome