Untitled
unknown
plain_text
a year ago
2.6 kB
5
Indexable
#include <iostream> using namespace std; int t, n, m; const int MN = 3000; int a[MN][MN]; struct Queue { int q[MN*MN]; int front, rear; Queue() { front = rear = -1; } void init() { front = rear = -1; } bool isEmpty() { return front == rear; } void push(int num) { q[++rear] = num; } int pop() { return q[++front]; } }; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; Queue mq_x, mq_y; int dist[MN][MN]; bool is_valid(int x, int y) { return 0 <= x && x < n && 0 <= y && y < m; } int special_case(int x, int y) { int mc = 0; for (int i = 0; i < 4; ++i) { int nx = x + dx[i]; int ny = y + dy[i]; if (is_valid(nx, ny)) { if (dist[nx][ny] == -1) { return -1; } mc = max(mc, dist[nx][ny]); } else { return -1; } } return mc; } int cnt_all_one = 0; int cnt1 = 0; void bfs(int sx, int sy) { mq_x.init(); mq_y.init(); mq_x.push(sx); mq_y.push(sy); dist[sx][sy] = 0; while (!mq_x.isEmpty()) { int x = mq_x.pop(); int y = mq_y.pop(); for (int i = 0; i < 4; ++i) { int nx = x + dx[i]; int ny = y + dy[i]; if (is_valid(nx, ny) && a[nx][ny] == 1 && dist[nx][ny] == -1) { mq_x.push(nx); mq_y.push(ny); cnt_all_one++; dist[nx][ny] = 1 + dist[x][y]; cnt1 = max(cnt1, dist[nx][ny]); } } } } int main() { cin >> t; for (int test_case = 1; test_case <= t; ++test_case) { cin >> n >> m; int sx, sy; cin >> sx >> sy; sx--; sy--; int x2, y2; int cnt_normal = 0; cnt_all_one = 0; cnt1 = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> a[i][j]; dist[i][j] = -1; if (a[i][j] == 2) { x2 = i; y2 = j; } if (a[i][j] == 1) { cnt_normal++; } } } bfs(sx, sy); int cnt2 = special_case(x2, y2); cout << "Case #" << test_case << '\n'; if (cnt2 == -1) { cout << -1 << ' ' << -1 << '\n'; } else if (cnt_normal != cnt_all_one + 1) { cout << cnt2 + 1 << ' ' << -1 << '\n'; } else { cout << cnt2 + 1 << ' ' << cnt1 + 1 << '\n'; } } return 0; }
Editor is loading...
Leave a Comment