Untitled
unknown
plain_text
a year ago
2.8 kB
10
Indexable
#define MAX_SIZE 1000
#include <unordered_map>
#define rint register int
using namespace std;
int planeSize;
unordered_map<int, vector<pair<int, int>>> hashMap;
int hashPlane[1009][1009];
unsigned long long hashFunc(int arr[][5]) {
unsigned long long hash = 0;
for (rint i = 0; i < 5; ++i) {
for (rint j = 0; j < 5; ++j) {
hash = (hash * 31 + arr[i][j]) % 1000000007;
}
}
return hash;
}
void rotateMatrix(int(*tile)[5]) {
for (rint i = 0; i < 5 / 2; i++) {
for (rint j = i; j < 5 - i - 1; j++) {
int tmp = tile[i][j];
tile[i][j] = tile[4 - j][i];
tile[4 - j][i] = tile[4 - i][4 - j];
tile[4 - i][4 - j] = tile[j][4 - i];
tile[j][4 - i] = tmp;
}
}
}
void init(int N, int mPlane[MAX_SIZE][MAX_SIZE])
{
hashMap.clear();
memset(hashPlane, 0, sizeof(hashPlane));
planeSize = N;
int tile[5][5];
for(rint i = 0; i < planeSize - 4 ; i++)
{
for(rint j = 0; j < planeSize - 4 ; j++)
{
if(!hashPlane[i][j])
{
int count = 0;
for (rint x = 0; x < 5; ++x){
for (rint y = 0; y < 5; ++y){
tile[x][y] = mPlane[i + x][j + y];
if (tile[x][y]) count++;
}
}
if (count == 7)
{
size_t hash_value = 0;
for (rint w = 0; w < 4; w++){
hash_value = hashFunc(tile);
rotateMatrix(tile);
if (hashMap[hash_value].size()) break;
}
if (hash_value != 0)
{
hashMap[hash_value].push_back(make_pair(i, j));
for (rint x = 0; x < 5; ++x) {
for (rint y = 0; y < 5; ++y) {
hashPlane[i + x][j + y] = hash_value;
}
}
}
}
}
}
}
}
int getCount(int mPiece[5][5])
{
size_t hash_value = 0;
for (rint w = 0; w < 4; w++){
hash_value = hashFunc(mPiece);
rotateMatrix(mPiece);
if (hashMap[hash_value].size()) break;
}
if (hash_value == 0) return 0;
return hashMap[hash_value].size();
}
int getPosition(int mRow, int mCol)
{
size_t hash_value = hashPlane[mRow][mCol];
int row = hashMap[hash_value].front().first + 2;
int col = hashMap[hash_value].front().second + 2;
return row * 10000 + col;
}Editor is loading...
Leave a Comment