초기 네이버 기록/알고리즘(C)

백준 2178 BFS미로탐색(C언어)

뜨거운 개발자 2023. 1. 4. 00:05
#include<stdio.h>

void ctoi(char* arr, int* ar,int m)
{
	for (int i = 0; i < m; i++)
	{
		ar[i] = arr[i] - 48;
		//printf("%d\n", ar[i + 1]);
	}
}
int cue[40000];
int front = 0;
int rear = 0;

void bfs(int y, int x,int (*map)[200],int xmax,int ymax)//bfs는 방향벡터 순서와도 관련이 있는가?
{
	int popx = x;
	int popy = y;
	while (front <= rear)
	{
		
		
		//printf("%d,%d :%d\n",popy,popx, map[popy][popx]);//x의 최대값과 y의 최대값을 넘어가는경우 비교가 제대로 이루어지지 않는다.
		
		if (popy<ymax-1&&map[popy + 1][popx] == 1)
		{
			map[popy + 1][popx] += map[popy][popx] ;
			cue[rear++] = (popy + 1) * xmax + popx;
		}
		if (0<popy &&map[popy - 1][popx] == 1)
		{
			map[popy - 1][popx] += map[popy][popx];
			cue[rear++] = (popy - 1) * xmax + popx;
		}
		if (popx<xmax-1&&map[popy][popx + 1] == 1)
		{
			map[popy][popx + 1] += map[popy][popx];
			cue[rear++] = (popy)*xmax + popx + 1;
		}
		if (0<popx&&map[popy][popx - 1] == 1)
		{
			map[popy][popx - 1] += map[popy][popx];
			cue[rear++] = (popy)*xmax + popx - 1;
		}
		popy = cue[front] / xmax;
		popx = cue[front] % xmax;
		//if (cue[front] % xmax == 0)
	//	{
		//	popx = xmax;
		//}
		front++;
	}
	//printf("%d")
}

int main()
{
	int n, m;
	scanf("%d %d", &n, &m);
	char arr[200];
	int map[200][200] = { 0 };
	for (int i = 0; i < n; i++)
	{
		scanf("%s", arr);
		ctoi(arr, map[i],m);
	}
	bfs(0, 0, map, m,n);
	printf("%d", map[n-1][m-1]);

}

개빡세다,,, 이거 1,1로 하다가 너무 뻘짓해서 그냥 0, 0 으로 구현하니까 쉽게 풀렸다,,, 내 3시간... 화나지만 괜찮다..

bfs도 결국 rear,front,pop아이디어만 잘 접목시키면 쉽게 풀 수있는 문제다.. 최대한 큐를 사용하고 싶지만 내가 큐 라이브러리를 쓸줄 모르기에 여기까지,,

 

이 글은 코딩 꼬꼬마 시절에 푼 문제를 보관한 글로 네이버에 저장해둔 글을 옮긴 글입니다.
혹시나 참고하시는 부분에 이상한 부분이나 질문이 생긴다면 남겨주시면 친절히 답변 드리겠습니다.

728x90