Created
June 24, 2022 04:52
-
-
Save AmirHosein-Gharaati/98c150300f3422602e44b4ba0f92b347 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#define maxn 15 | |
int n, m, k; | |
char G[maxn][maxn]; | |
int fire[maxn][maxn] ; | |
struct point { | |
int x, y; | |
} ; | |
struct Node{ | |
struct point data; | |
struct Node *next; | |
}; | |
void insertStart(struct Node** head, struct point data){ | |
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node)); | |
newNode->data = data; | |
newNode->next = *head; | |
*head = newNode; | |
} | |
void insertLast(struct Node** head, struct point data){ | |
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node)); | |
newNode->data = data; | |
newNode->next = NULL; | |
if(*head==NULL){ | |
*head = newNode; | |
return; | |
} | |
struct Node* temp = *head; | |
while(temp->next!=NULL) | |
temp = temp->next; | |
temp->next = newNode; | |
} | |
struct Node* head = NULL; | |
void init() { | |
int dx[9] = { -1, -1, -1, 0, 0, 1, 1, 1 }; | |
int dy[9] = { -1, 0, 1, -1, 1, -1, 0, 1 }; | |
while (head!=NULL) { | |
struct point now = head -> data; | |
head = head->next; | |
for (int i = 0; i < 8; i++) { | |
struct point next ; | |
next.x = now.x + dx[i]; | |
next.y = now.y + dy[i]; | |
if (next.x > 0 && next.x <= n && next.y > 0 && next.y <= m) { | |
if (fire[next.x][next.y] > fire[now.x][now.y] + k) { | |
fire[next.x][next.y] = fire[now.x][now.y] + k; | |
//q.push(next); | |
insertLast(&head,next); | |
} | |
} | |
} | |
} | |
} | |
int main() { | |
scanf("%d%d%d", &n, &m, &k); | |
memset(fire,maxn,maxn*maxn *sizeof (int)); | |
for (int i = 1; i <= n; i++) { | |
scanf("%s", G[i] + 1); | |
} | |
int x , y; | |
scanf("%d %d" , &x , &y); | |
struct point temp; | |
for (int i = 1; i <= n; i++) { | |
for (int j = 1; j <= m; j++) { | |
if (G[i][j] == 'f') { | |
fire[i][j] = 0; | |
temp.x=i; | |
temp.y=j; | |
insertLast(&head,temp); | |
} | |
} | |
} | |
init(); | |
printf("%d" , fire[n-y][x+1]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment