#include <stdio.h>
#include <string.h>
int main() {
int n; // size of board
int i, j, k, l; // counters
char type[10][10][100];
float weight[10][10];
float totalWeight[10][10];
// Input
printf("Enter the size ofboard: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
printf("Enter the type of cell (%d, %d) (H, P, S, W): ", i, j);
scanf("%s", type[i][j]);
totalWeight[i][j] = -1;
}
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
printf("Enter the weight of cheese in cell (%d, %d): ", i, j);
scanf("%f", &weight[i][j]);
}
}
// Dijkstra typealgorithm
if (strcmp(type[1][1], "W") != 0) {
totalWeight[1][1] = weight[1][1];
for (k = 2; k < 2 * n; k++) {
if (k <= n + 1) {
l = 1;
} else {
l = k - n;
}
for (i = l; i <= k - l; i++) {
j = k - i;
if (totalWeight[i][j] > 0) {
if ((i + 1 <= n) && (strcmp(type[i + 1][j], "W") != 0)) {
if ((strcmp(type[i][j], type[i + 1][j]) == 0) || (strcmp(type[i][j], "S") == 0) || (strcmp(type[i + 1][j], "S") == 0)) {
if (weight[i + 1][j] + totalWeight[i][j] > totalWeight[i + 1][j]) {
totalWeight[i + 1][j] = totalWeight[i][j] + weight[i + 1][j];
}
}
}
if ((j + 1 <= n) && (strcmp(type[i][j + 1], "W") != 0)) {
if ((strcmp(type[i][j], type[i][j + 1]) == 0) || (strcmp(type[i][j], "S") == 0) || (strcmp(type[i][j + 1], "S") == 0)) {
if (weight[i][j + 1] + totalWeight[i][j] > totalWeight[i][j + 1]) {
totalWeight[i][j + 1] = totalWeight[i][j] + weight[i][j + 1];
}
}
}
}
}
}
if (totalWeight[n][n] > 0) {
printf("Maximal weight of cheese %f", totalWeight[n][n]);
} else {
printf("It is impossible to reach bottom right cell!");
}
} else {
printf("It is impossible to reach bottom right cell!");
}
return 0;
}
Comments
Leave a comment