Untitled

mail@pastecode.io avatar
unknown
plain_text
25 days ago
2.5 kB
3
Indexable
Never
#include <iostream>
#include <unordered_map>
#include <cstring>
#include <string>
#include <list>
#define MAX_SIZE 1000

using namespace std;

int n;
int step;
unordered_map<int, list<string>> mp;
int visit[MAX_SIZE][MAX_SIZE];
int mat[MAX_SIZE][MAX_SIZE];

void init(int N, int mPlane[MAX_SIZE][MAX_SIZE])
{
	// Reset
	mp.clear();
	n = N;
	step = 1;
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) {
			mat[i][j] = mPlane[i][j];
		}
	}
	memset(visit, 0, sizeof(visit));

	// Hash map
	for(int i = 0; i < n - 4; i++) {  
		for(int j = 0; j < n - 4; j++) {
			int cnt = 0;
			bool check = false;
			string s0 = "", s90 = "", s180 = "", s270 = "";
			for(int r = i; r < i + 5; r++) {
				for(int c = j; c < j + 5; c++) {
					if(mat[r][c] == 1) {  
						cnt++;
						if(mat[i][c] == 1 || mat[i+4][c] == 1 || mat[r][j] == 1 || mat[r][j+4] == 1) {
							check = true;
						}
					}
				}
			}
			if(check && cnt == 7) {
				for(int r = i; r < i + 5; r++) {
					for(int c = j; c < j + 5; c++) {
						visit[r][c] = step;
						// 4 Huong
						s0 += to_string(mat[r][c]);      
						s180 += to_string(mat[i + 4 - (r - i)][j + 4 - (c - j)]); 
						s90 += to_string(mat[i + 4 - (c - j)][j + (r - i)]);      
						s270 += to_string(mat[i + (c - j)][j + 4 - (r - i)]);     
					}
				}
				//Push hash map
				mp[step].push_back(s0);  
				if(s90 != s0) mp[step].push_back(s90); 
				if(s180 != s90 && s180 != s0) mp[step].push_back(s180);  
				if(s270 != s180 && s270 != s90 && s270 != s0) mp[step].push_back(s270);  
				mp[step].push_back(to_string(i*N+j));
				step++;
			}
		}
	}

	for (const auto &p : mp) {
		std::cout << p.first << ": ";
		for (const auto &s : p.second) {
			std::cout << s << " ";
		}
		std::cout << std::endl;
	}

}

int getCount(int mPiece[5][5])
{
	int cnt = 0;
	string temp = "";
	for(int i = 0; i < 5; i++) {
		for(int j = 0; j < 5; j++) {
			temp += to_string(mPiece[i][j]);
		}
	}
	for(auto it : mp) {
		for(auto v : it.second) {
			if(v == temp) cnt++;
		}
	}
	return cnt;
}

int getPosition(int mRow, int mCol)
{
	int resKey = visit[mRow][mCol];
	for(auto &it:mp[visit[mRow][mCol]]) {
		for(int key = 1; key<step && key != visit[mRow][mCol]; key++) {
			for(auto &item:mp[key]) {
				if(item == it ) resKey = key; 
			}
		}
	}
	
	auto temp = mp[resKey].rbegin();
	int row = stoi(r) / n;
	int col = stoi(r) % n;


	return row * 10000 + col;
}

Leave a Comment