Untitled
unknown
plain_text
a year ago
2.9 kB
10
Indexable
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
int n;
int map[100][100];
vector<char> result;
struct toado {
int x, y;
};
struct block
{
toado top, bot;
};
stack<block> s;
bool check(int r1, int c1, int r2, int c2)
{
int ori = map[r1][c1];
bool flag = true;
for (int i = r1; i <= r2 && flag; i++)
for (int j = c1; j <= c2; j++)
{
if (map[i][j] != ori)
{
flag = false;
break;
}
}
return flag;
}
int encode()
{
result.clear();
s = stack<block>();//contain block
int r1 = 0, r2 = n - 1;
int c1 = 0, c2 = n - 1;
toado top = { r1,c1 };
toado bot = { r2,c2 };
block bigest = { top,bot };
s.push(bigest);
result.push_back('(');
//
while (!s.empty())
{
block currentBlock = s.top();
s.pop();
r1 = currentBlock.top.x;
c1 = currentBlock.top.y;
r2 = currentBlock.bot.x;
c2 = currentBlock.bot.y;
//
bool nigga = check(r1, c1, r2, c2);
//
if (nigga)
{
result.push_back(map[r1][c1]);
//kiem tra so luong
int size = result.size();
int start = size - 1;
bool flagClose = false;
int dem = 0;
while (1)
{
if (result[start] == ')') flagClose = true;
else if (result[start] == '(' && flagClose)
{
dem++;
flagClose = false;
}
else if (result[start] == '(' && !flagClose) break;
else if ((result[start + 1] == '(' || result[start + 1] == ')') && (result[start - 1] == '(' || result[start - 1] == ')')) dem++;
start--;
}
if (dem == 4) result.push_back(')');
}
else
{
//xet 2x2
if (r2 == r1 + 1 && c2 == c1 + 1)
{
result.push_back('(');
result.push_back(map[r1][c1] + '0');
result.push_back(map[r1][c2] + '0');
result.push_back(map[r2][c1] + '0');
result.push_back(map[r2][c2] + '0');
result.push_back(')');
//kiem tra so luong
int size = result.size();
int start = size - 1;
bool flagClose = false;
int dem = 0;
while (1)
{
if (result[start] == ')') flagClose = true;
else if (result[start] == '(' && flagClose)
{
dem++;
flagClose = false;
}
else if (result[start] == '(' && !flagClose) break;
else if ((result[start + 1] == '(' || result[start + 1] == ')') && (result[start - 1] == '(' || result[start - 1] == ')')) dem++;
start--;
}
if (dem == 4) result.push_back(')');
}
else
{
//chia binh thuong
result.push_back('(');
}
}
}
//
result.push_back(')');
return result.size() + pow(n, 2) / 16 + 2;
}
int main()
{
FILE* input;
freopen_s(&input, "input.txt", "r", stdin);
//
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) cin >> map[i][j];
//
int lenght = encode();
cout << endl;
for (int i = 0; i < result.size(); i++) cout << result[i];
//
return 0;
}Editor is loading...
Leave a Comment