একটি ম্যাট্রিক্সে তারকা (*) এবং অক্ষর (A-Z) আছে। কোন অক্ষরের চারদিকে (৮ দিক) সবচেয়ে বেশি তারকা আছে তা বের করতে হবে। টাই হলে লেক্সিকোগ্রাফিক্যালি ছোট অক্ষরটি নির্বাচন করা হয়।
উদাহরণ ম্যাট্রিক্স:
| B | * | * | * |
| * | * | C | * |
| * | A | * | * |
| * | * | * | D |
A এবং C-এর চারদিকে ৭টি তারকা (সর্বোচ্চ)। A লেক্সিকোগ্রাফিক্যালি ছোট, তাই আউটপুট A।
প্রতি অক্ষরের জন্য তারকা গণনা করে একটি ম্যাপে রাখা হয়। সর্বোচ্চ মান ও লেক্সিকোগ্রাফিক্যালি ছোট অক্ষর বের করা হয়।
#include <iostream>
#include <unordered_map>
#define MAX 4
using namespace std;
int checkStarCount(int mat[][MAX], int i, int j, int n) {
int count = 0;
int move_row[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int move_col[] = {-1, 0, 1, -1, 1, -1, 0, 1};
for (int k = 0; k < 8; k++) {
int x = i + move_row[k];
int y = j + move_col[k];
if (x >= 0 && x < n && y >= 0 && y < n && mat[x][y] == '*')
count++;
}
return count;
}
char charWithMaxStar(int mat[][4], int n) {
unordered_map<char, int> star_count_map;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if ((mat[i][j] - 'A') >= 0 && (mat[i][j] - 'A') < 26) {
int stars = checkStarCount(mat, i, j, n);
star_count_map[mat[i][j]] = stars;
}
}
}
int max = -1;
char result = 'Z' + 1;
for (auto x : star_count_map) {
if (x.second > max || (x.second == max && x.first < result)) {
max = x.second;
result = x.first;
}
}
return result;
}
int main() {
int mat[][4] = {
{'B', '*', '*', '*'},
{'*', '*', 'C', '*'},
{'*', 'A', '*', '*'},
{'*', '*', '*', 'D'}
};
int n = 4;
cout << charWithMaxStar(mat, n) << " has maximum amount of stars around it";
}
A has maximum amount of stars around it