개발자 톡
연습문제 톡
장애물 인식 프로그램
어디서 문제가 발생했는지 모르겠습니다 ㅠ c언어
- 등록일
- 2023-11-23 17:13:17
- 조회수
- 430
- 작성자
- yimjaeyun
제출 답안
C
#include <stdio.h>
#include <math.h>
int visited[30][30];
int n[30][30];
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
int obstacle[625];
void DFS(int curi, int curj, int obs){
visited[curi][curj]=1;
obstacle[obs-1]+=1;
for (int k=0;k<4;k++){
int nexi=curi+dx[k];
int nexj=curj+dy[k];
if (n[nexi][nexj]==1 && visited[nexi][nexj]==0){
DFS(nexi,nexj,obs);
}
}
}
int main(void)
{
int block_num,pre_num;
scanf("%d",&block_num);
int temp;
int a;
int number_obstacle=0;
for (int i=0;i<block_num;i++){
scanf("%d",&pre_num);
a=block_num;
for (int j=0;j<block_num;j++){
n[i][j]=pre_num/(int)(pow(10,a-1));
pre_num=pre_num-(pre_num/(int)(pow(10,a-1)))*(int)(pow(10,a-1));
a-=1;
}
}
for (int i=0;i<block_num;i++){
for (int j=0;j<block_num;j++){
if (n[i][j]==1 && visited[i][j]==0){
number_obstacle+=1;
DFS(i,j,number_obstacle);
}
}
}
printf("%d\n",number_obstacle);
for (int i=0;i<number_obstacle-1;i++){ % 정렬
for (int j=i+1;j<number_obstacle;j++){
if (obstacle[i]>obstacle[j]){
temp=obstacle[i];
obstacle[i]=obstacle[j];
obstacle[j]=temp;
}
}
}
for (int i=0;i<number_obstacle;i++)
printf("%d\n",obstacle[i]);
}
반례 찾아주시면 감사드리겠습니다.
TC1, TC2, TC5, TC7 빼고는 전부 오답이네요 ㅠ
#장애물_인식_프로그램