17086번 아기 상어2

동도리동·2021년 9월 14일
0

코딩테스트

목록 보기
33/76

break를 2번 사용하였다. 더 효율적으로 쓸 수 있을 것 같은데...굳이ㅎ

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstring>
#include <tuple>
int dx[8] = { -1,-1,-1,0,0,1,1,1 };
int dy[8] = { -1,0,1,-1,1,-1,0,1 };
using namespace std;
int map[51][51];
int main() {
	//freopen("in1.txt", "rt", stdin);
	int ans = -1;
	int n, m;
	int x, y;
	cin >> n >> m;
	vector<pair<int, int>> v;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> map[i][j];
			if (map[i][j] == 1) {
				v.push_back(make_pair(i, j));
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (map[i][j] == 1) continue;
			queue<pair<int, int>> Q;
			int dis[51][51];
			memset(dis, 0, sizeof(dis));
			int maxi=0;
			Q.push(make_pair(i, j));
			while (!Q.empty()) {
				tie(x, y) = Q.front();
				Q.pop();
				for (int k = 0; k < 8; k++) {
					int xx = x + dx[k];
					int yy = y + dy[k];
					if (xx > n || xx <= 0 || yy > m || yy <= 0) continue;
					
					if (dis[xx][yy] == 0) {
						dis[xx][yy] = dis[x][y] + 1;
						Q.push(make_pair(xx, yy));
					}
					if (map[xx][yy] == 1) {
						maxi = dis[xx][yy];
						break;
					}
				}
				if (maxi != 0) break;
			}
			if (maxi > ans) ans = maxi;

		}

	}
	cout << ans << '\n';
	return 0;
}
profile
긍정코딩세상

0개의 댓글

관련 채용 정보