본문 바로가기

백준 알고리즘/BFS47

백준 2178번 C++ #include #include #include using namespace std; const int MAX=101;int n, m;int arr[MAX][MAX];queue way;int visited[MAX][MAX]; typedef struct {int x, y;}Move;Move mv[4] = { {0,-1},{0,1},{1,0},{-1,0} }; int bfs() {int answer = 0;way.push(make_pair(1,make_pair(1,1)));visited[1][1] = 1; while (!way.empty()) {int x = way.front().first;int y = way.front().second.first;int count = way.front().second.se.. 2019. 2. 19.
백준 2589번 C++ #include #include #include using namespace std; const int MAX = 51;int n,m;char way[MAX][MAX];char shortestway[MAX][MAX]; typedef struct {int x, y;}Move;Move moves[4] = { {1,0},{-1,0},{0,1},{0,-1} }; int bfs(int i,int j) {int longestway=0;memset(shortestway, 0, sizeof(shortestway));queue q;q.push(make_pair(i, j)); while (!q.empty()) {int x = q.front().first;int y = q.front().second;q.pop(); for .. 2019. 1. 14.
백준 10026번 C++ #include #include using namespace std; const int MAX = 101;int n;char color[MAX][MAX];char visited[MAX][MAX];int colornum = 0;typedef struct {int x, y;}Move;Move m[4] = { {1,0},{-1,0},{0,1},{0,-1} }; void dfs(int i,int j) {visited[i][j] = 1; for (int k = 0; k = 0 && ma = 0 && mb < n) {if (color[ma][mb] == color[i][j] && visit.. 2019. 1. 13.
백준 1389번 C++ #include #include using namespace std; const int MAX = 987654321;const int arr = 100;int n, m;int graph[arr][arr]; void floyd() { for (int k = 1; k n >> m; for (int i = 1; i x >> y;graph[x][y] = 1;graph[y][x] = 1;} floyd(); int ans;int shortest = 987654321;for (int i = 1; i 2019. 1. 13.