习题6-8(uva-806)

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值