#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
const int maxn = 72;
const int base[] = { 1,5,25,125,625,3125,15625,78125 };
char matrix[maxn][maxn];
int tcnt = 0, n = 0,t;
vector<int> ret;
set<string> mlist;
int IsBlock(int l, int r, int u, int d,int flag) {
for (int i = l; i <= r; i++) {
for (int j = u; j <= d; j++) {
if (matrix[i][j] != flag) return 0;
}
}
return 1;
}
void FillBlock(int l, int r, int u, int d) {
for (int i = l; i <= r; i++) {
for (int j = u; j <= d; j++) {
matrix[i][j] = '*';
}
}
}
int GetTen(string& in) {
if (in.empty()) return 0;
int val = 0;
for (int i = 0; i < in.size(); i++) {
val = val + (in[i] - '0') * base[i];
}
return val;
}
string GetFiv(int num) {
string ret;
while (num) {
ret.push_back(num % 5 + '0');
num /= 5;
}
return ret;
}
void GetTree(string &temp,int l,int r,int u,int d) {
if (l > r || u > d) return ;
if (IsBlock(l, r, u, d, '0')) return;
else if (IsBlock(l, r, u, d, '1')) {
ret.emplace_back(GetTen(temp));
return ;
}
int m1 = (l + r) >> 1,m2 = (u + d) >> 1;
temp += '1';GetTree(temp , l, m1, u, m2); temp.pop_back();
temp += '2';GetTree(temp , l, m1, m2 + 1, d); temp.pop_back();
temp += '3';GetTree(temp ,m1+1, r, u, m2); temp.pop_back();
temp += '4';GetTree(temp ,m1+1, r, m2+1, d); temp.pop_back();
return ;
}
void dfs(string& temp, int l, int r, int u, int d) {
if (l > r || u > d) return;
if (mlist.count(temp)) {
FillBlock(l, r, u, d);
return;
}
else if (l == r && u == d) {
return;
}
int m1 = (l + r) >> 1, m2 = (u + d) >> 1;
temp += '1'; dfs(temp, l, m1, u, m2); temp.pop_back();
temp += '2'; dfs(temp, l, m1, m2 + 1, d); temp.pop_back();
temp += '3'; dfs(temp, m1 + 1, r, u, m2); temp.pop_back();
temp += '4'; dfs(temp, m1 + 1, r, m2 + 1, d); temp.pop_back();
}
int main()
{
string temp;
int id = 0;
while (cin >> n && n) {
cin.ignore();
mlist.clear();
memset(matrix, '.', sizeof(matrix));
ret.clear();
temp = "";
if (n > 0) {
for (int i = 1; i <= n; i++) {
cin >> (&matrix[i][1]);
}
GetTree(temp, 1, n, 1, n);
sort(ret.begin(), ret.end());
if (id) cout << endl;
cout << "Image " << ++id<<endl;
int flag = 0;
for (int i = 0; i < ret.size(); i++) {
if (flag == 0) cout << ret[i];
else cout << " " << ret[i];
flag++;
if (flag == 12) { flag = 0; cout << endl; }
}
if(ret.size() % 12)
cout << endl;
cout << "Total number of black nodes = " << ret.size() << endl;
}
else {
n = -n;
while (cin >> t && t != -1) {
mlist.insert(GetFiv(t));
}
dfs(temp, 1, n, 1, n);
if (id) cout << endl;
cout << "Image " << ++id << endl;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << matrix[i][j];
}
cout << endl;
}
}
}
return 0;
}
习题6-8(uva-806)
最新推荐文章于 2021-05-20 09:09:44 发布