Untitled
unknown
plain_text
a year ago
2.9 kB
10
Indexable
#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 hướng
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);
// Lưu vị trí i*N + j vào map
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]; // Lấy key từ visit
if (resKey == 0) return -1; // Không tìm thấy
// Lấy vị trí từ chuỗi cuối cùng của `mp[resKey]`
auto it = mp[resKey].rbegin();
string position = *it;
int idx = stoi(position);
int row = idx / n;
int col = idx % n;
return row * 10000 + col;
}
int main() {
// Test thử hàm init và getPosition
int N = 10;
int mPlane[MAX_SIZE][MAX_SIZE] = {
{0, 0, 0, 0, 0, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 1, 1, 0, 0},
// Thêm các hàng khác nếu cần
};
init(N, mPlane);
cout << getPosition(0, 5) << endl; // Vị trí mẫu, kiểm tra đầu ra
return 0;
}
Editor is loading...
Leave a Comment