Untitled
unknown
plain_text
a year ago
1.1 kB
8
Indexable
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main() {
int N = 4; // Kích thước bản đồ N x N
int map[4][4] = {
{1, 2, 3, 4},
{2, 1, 4, 3},
{1, 3, 2, 4},
{4, 2, 1, 3}
};
map<int, vector<int>> mp;
// Duyệt qua các phần tử của bản đồ
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
int key = map[i][j];
// Lưu vị trí row * N + col vào vector của key
mp[key].push_back(i * N + j);
}
}
// In ra số lần xuất hiện của mỗi key và các vị trí tương ứng
for (auto it : mp) {
int key = it.first;
vector<int> positions = it.second;
cout << "Key " << key << " xuất hiện " << positions.size() << " lần tại các vị trí: ";
for (int pos : positions) {
int row = pos / N;
int col = pos % N;
cout << "(" << row << ", " << col << ") ";
}
cout << endl;
}
return 0;
}
Editor is loading...
Leave a Comment