# hugodaovang2

user_9278292
plain_text
7 months ago
2.1 kB
19
Indexable
Never
```#include <iostream>

using namespace std;

int T;
int N, G;
int goldX[5], goldY[5];
int map[25][25];
int distanceGold[5];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int visit[25][25] = {0};
int numSpace;
int costt;
int minn;

struct Point {
int x, y, dist;
Point() {}
Point(int x, int y, int dist) {
this -> x = x;
this -> y = y;
this -> dist = dist;
}
};

Point queuee[4000];
int front, rear;

void init() {
front = rear = 0;
}

void enqueue(Point p) {
queuee[rear++] = p;
}

Point dequeue() {
return queuee[front++];
}

void resetVisit() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) visit[i][j] = 0;
}
}

void findPath(int x, int y) {
init();
resetVisit();
visit[x][y] = 1;
enqueue(Point(x, y, 0));
while (front != rear) {
Point p = dequeue();
for (int i = 0; i < 4; i++) {
int px = p.x + dx[i];
int py = p.y + dy[i];
if (px >= 0 && py >= 0 && px < N && py < N && map[px][py] != 0 && visit[px][py] == 0) {
visit[px][py] = 1;
for (int g = 0; g < G; g++) {
if (px == goldX[g] - 1 && py == goldY[g] - 1) distanceGold[g] = p.dist + 1;
}
enqueue(Point(px, py, p.dist + 1));
}
}
}
visit[x][y] = 0;
}

int findMaxOfArr(int arr[], int n) {
int res = -1;
for (int i = 0; i < n; i++) {
if (res < arr[i]) res = arr[i];
}
return res;
}

int main() {
cin >> T;
for (int t = 1; t <= T; t++) {
cin >> N >> G;
for (int i = 0; i < G; i++) cin >> goldX[i] >> goldY[i];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> map[i][j];
}
}
for (int i = 0; i < G; i++) {
map[goldX[i] - 1][goldY[i] - 1] = 3;
}
int arrMax[25];
for (int i = 0; i < 25; i++) arrMax[i] = 0;
int indexOfArrMax = 0;
minn = 999999;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (map[i][j] == 1) {
findPath(i, j);
if (minn > findMaxOfArr(distanceGold, G)) minn = findMaxOfArr(distanceGold, G);
}
}
}
cout << "Case #" << t << endl << minn << endl;
}
return 0;
}```
Leave a Comment