20250729 baekjoon
This commit is contained in:
parent
8e6fa230a3
commit
abbdab565e
62
code_study/Baekjoon/c/21736.c
Normal file
62
code_study/Baekjoon/c/21736.c
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int N, M;
|
||||||
|
scanf("%d %d",&N, &M);
|
||||||
|
|
||||||
|
char** campus = (char**)malloc(sizeof(char*)*N);
|
||||||
|
int** visited = (int**)malloc(sizeof(int*)*N);
|
||||||
|
for(int i=0; i<N; i++) {
|
||||||
|
campus[i] = (char*)malloc(sizeof(char)*(M+1));
|
||||||
|
visited[i] = (int*)malloc(sizeof(int)*M);
|
||||||
|
memset(visited[i], 0, sizeof(int)*M);
|
||||||
|
}
|
||||||
|
|
||||||
|
int* queueX = (int*)malloc(sizeof(int)*(N*M));
|
||||||
|
int* queueY = (int*)malloc(sizeof(int)*(N*M));
|
||||||
|
|
||||||
|
for(int n=0; n<N; n++){
|
||||||
|
scanf("%s",campus[n]);
|
||||||
|
for(int m=0; m<M; m++){
|
||||||
|
if(campus[n][m] == 'I'){
|
||||||
|
queueY[0] = n;
|
||||||
|
queueX[0] = m;
|
||||||
|
visited[n][m] = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int front = 0, rear = 1, friendCount = 0;
|
||||||
|
int dx[4] = {-1,1,0,0};
|
||||||
|
int dy[4] = {0,0,-1,1};
|
||||||
|
|
||||||
|
while(front<rear){
|
||||||
|
int x = queueX[front];
|
||||||
|
int y = queueY[front];
|
||||||
|
front++;
|
||||||
|
for(int i=0; i<4; i++){
|
||||||
|
int nx = x + dx[i];
|
||||||
|
int ny = y + dy[i];
|
||||||
|
if(nx<0 || nx>=M || ny<0 || ny>=N || visited[ny][nx] || campus[ny][nx] == 'X') continue;
|
||||||
|
visited[ny][nx] = 1;
|
||||||
|
queueX[rear] = nx;
|
||||||
|
queueY[rear] = ny;
|
||||||
|
rear++;
|
||||||
|
if(campus[ny][nx] == 'P') friendCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(friendCount) printf("%d\n",friendCount);
|
||||||
|
else printf("TT\n");
|
||||||
|
|
||||||
|
for(int i=0; i<N; i++){
|
||||||
|
free(campus[i]);
|
||||||
|
free(visited[i]);
|
||||||
|
}
|
||||||
|
free(campus);
|
||||||
|
free(visited);
|
||||||
|
free(queueX);
|
||||||
|
free(queueY);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user