[SWEA] 탈주범 검거

Date:

[SWEA] 탈주범 검거

Problem URL : 탈주범 검거

#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>

using namespace std;

int map[50][50];
int visited[50][50];

int TC, n, m, r, c, l;

bool upAvailable(int y, int x) {
	bool check = !visited[y - 1][x] && y - 1 >= 0 && (map[y - 1][x] == 1 || map[y - 1][x] == 2 || map[y - 1][x] == 5 || map[y - 1][x] == 6);
	return check;
}

bool downAvailable(int y, int x) {
	bool check = !visited[y + 1][x] && y + 1 < n && (map[y + 1][x] == 1 || map[y + 1][x] == 2 || map[y + 1][x] == 4 || map[y + 1][x] == 7);
	return check;
}

bool rightAvailable(int y, int x) {
	bool check = !visited[y][x + 1] && x + 1 < m && (map[y][x + 1] == 1 || map[y][x + 1] == 3 || map[y][x + 1] == 6 || map[y][x + 1] == 7);
	return check;
}

bool leftAvailable(int y, int x) {
	bool check = !visited[y][x - 1] && x - 1 >= 0 && (map[y][x - 1] == 1 || map[y][x - 1] == 3 || map[y][x - 1] == 4 || map[y][x - 1] == 5);
	return check;
}

int bfs(int row, int col) {

	queue<pair<pair<int, int>, int>> q;
	q.push(make_pair(make_pair(row, col), 1));
	int ans = 0;
	while (!q.empty()) {

		pair<int, int> p = q.front().first;
		int t = q.front().second;
		int y = p.first;
		int x = p.second;
		q.pop();
		if (visited[y][x]) {
			continue;
		}
		visited[y][x] = 1;
		ans++;

		if (t == l) {
			continue;
		}
		int type = map[y][x];

		if (type == 1) {
			if (downAvailable(y, x)) {
				q.push(make_pair(make_pair(y + 1, x), t + 1));
			}
			if (upAvailable(y, x)) {
				q.push(make_pair(make_pair(y - 1, x), t + 1));
			}
			if (rightAvailable(y, x)) {
				q.push(make_pair(make_pair(y, x + 1), t + 1));
			}
			if (leftAvailable(y, x)) {
				q.push(make_pair(make_pair(y, x - 1), t + 1));
			}
		} else if (type == 2) {
			if (downAvailable(y, x)) {
				q.push(make_pair(make_pair(y + 1, x), t + 1));
			}
			if (upAvailable(y, x)) {
				q.push(make_pair(make_pair(y - 1, x), t + 1));
			}
		} else if (type == 3) {
			if (rightAvailable(y, x)) {
				q.push(make_pair(make_pair(y, x + 1), t + 1));
			}
			if (leftAvailable(y, x)) {
				q.push(make_pair(make_pair(y, x - 1), t + 1));
			}
		} else if (type == 4) {
			if (upAvailable(y, x)) {
				q.push(make_pair(make_pair(y - 1, x), t + 1));
			}
			if (rightAvailable(y, x)) {
				q.push(make_pair(make_pair(y, x + 1), t + 1));
			}
		} else if (type == 5) {
			if (rightAvailable(y, x)) {
				q.push(make_pair(make_pair(y, x + 1), t + 1));
			}
			if (downAvailable(y, x)) {
				q.push(make_pair(make_pair(y + 1, x), t + 1));
			}
		} else if (type == 6) {
			if (leftAvailable(y, x)) {
				q.push(make_pair(make_pair(y, x - 1), t + 1));
			}
			if (downAvailable(y, x)) {
				q.push(make_pair(make_pair(y + 1, x), t + 1));
			}
		} else if (type == 7) {
			if (leftAvailable(y, x)) {
				q.push(make_pair(make_pair(y, x - 1), t + 1));
			}
			if (upAvailable(y, x)) {
				q.push(make_pair(make_pair(y - 1, x), t + 1));
			}
		}
	}

	return ans;
}

int main() {

	scanf("%d", &TC);

	for (int tc = 1; tc <= TC; tc++) {
		scanf("%d %d %d %d %d", &n, &m, &r, &c, &l);
		memset(map, 0, sizeof(map));
		memset(visited, 0, sizeof(visited));
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				scanf("%d", &map[i][j]);
			}
		}
		int answer = bfs(r, c);
		printf("#%d %d\n", tc, answer);
	}

}

댓글