Task: | Abandoned warehouse |
Sender: | aykhann |
Submission time: | 2024-09-09 17:41:15 +0300 |
Language: | C++ (C++11) |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.00 s | details |
#2 | ACCEPTED | 0.00 s | details |
#3 | ACCEPTED | 0.00 s | details |
#4 | ACCEPTED | 0.00 s | details |
#5 | ACCEPTED | 0.00 s | details |
#6 | ACCEPTED | 0.07 s | details |
#7 | ACCEPTED | 0.08 s | details |
#8 | ACCEPTED | 0.08 s | details |
#9 | ACCEPTED | 0.10 s | details |
#10 | ACCEPTED | 0.10 s | details |
#11 | ACCEPTED | 0.00 s | details |
#12 | ACCEPTED | 0.00 s | details |
#13 | ACCEPTED | 0.12 s | details |
#14 | ACCEPTED | 0.00 s | details |
#15 | ACCEPTED | 0.00 s | details |
#16 | ACCEPTED | 0.10 s | details |
Code
#include<bits/stdc++.h> using namespace std; struct Cell { int x, y; }; int n, m; vector<vector<char>> warehouse; vector<vector<bool>> visited; vector<vector<Cell>> parent; vector<vector<char>> direction; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; char dir[] = {'D', 'U', 'R', 'L'}; bool isValid(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m && warehouse[x][y] != '#' && !visited[x][y]; } int bfs(Cell start, Cell end) { queue<Cell> q; q.push(start); visited[start.x][start.y] = true; while (!q.empty()) { Cell cur = q.front(); q.pop(); if (cur.x == end.x && cur.y == end.y) { return 1; } for (int i = 0; i < 4; ++i) { int newX = cur.x + dx[i]; int newY = cur.y + dy[i]; if (isValid(newX, newY)) { visited[newX][newY] = true; parent[newX][newY] = cur; direction[newX][newY] = dir[i]; q.push({newX, newY}); } } } return 0; } void printPath(Cell start, Cell end) { vector<char> path; Cell cur = end; while (cur.x != start.x || cur.y != start.y) { path.push_back(direction[cur.x][cur.y]); cur = parent[cur.x][cur.y]; } reverse(path.begin(), path.end()); cout<<path.size() << endl; for (char step : path) { cout<<step; } cout<<"\n"; } int main() { cin >> n >> m; warehouse.assign(n, vector<char>(m)); visited.assign(n, vector<bool>(m, false)); parent.assign(n, vector<Cell>(m, {-1, -1})); direction.assign(n, vector<char>(m)); Cell start, end; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin>>warehouse[i][j]; if (warehouse[i][j] == 'A'){ start ={i, j}; }else if(warehouse[i][j] == 'B'){ end ={i, j}; } } } if (bfs(start, end)) { cout<<"YES\n"; printPath(start, end); } else { cout<<"NO\n"; } }
Test details
Test 1
Verdict: ACCEPTED
input |
---|
10 10 ##.A###### #.##.##.## #####..### .######### ... |
correct output |
---|
NO |
user output |
---|
NO |
Test 2
Verdict: ACCEPTED
input |
---|
10 10 B#..##.#.. #....A##.. #.....#..# .#......#. ... |
correct output |
---|
NO |
user output |
---|
NO |
Test 3
Verdict: ACCEPTED
input |
---|
10 10 ...#..A.#. ....B...## ...#...... .......... ... |
correct output |
---|
YES 3 LLD |
user output |
---|
YES 3 DLL |
Test 4
Verdict: ACCEPTED
input |
---|
10 10 .#........ .......... .......... ........#. ... |
correct output |
---|
YES 1 R |
user output |
---|
YES 1 R |
Test 5
Verdict: ACCEPTED
input |
---|
10 10 .......... .......... .......... .......... ... |
correct output |
---|
YES 3 RDD |
user output |
---|
YES 3 DDR |
Test 6
Verdict: ACCEPTED
input |
---|
1000 1000 ##.###..######.#########.###.#... |
correct output |
---|
NO |
user output |
---|
NO |
Test 7
Verdict: ACCEPTED
input |
---|
1000 1000 ####.#.###....#.......##.##.#.... |
correct output |
---|
YES 626 LLLDDRDDDDLDLDDLLLLLDDDDLLDLDL... |
user output |
---|
YES 626 RDDDDDLDDDDLDDDDLLLLLLDDLLLDDL... Truncated |
Test 8
Verdict: ACCEPTED
input |
---|
1000 1000 ....#.##......#....#......#...... |
correct output |
---|
YES 364 LULULLULLLULLLLLUULLLLUUULLLLL... |
user output |
---|
YES 364 UUUUUULUUUUUUUUUUULLLUUUULLUUU... Truncated |
Test 9
Verdict: ACCEPTED
input |
---|
1000 1000 .................#......#........ |
correct output |
---|
YES 1003 LLLLLLLLLLLLLLLLLLLLLLLLLDLLLL... |
user output |
---|
YES 1003 DDDDDDDDDDDDDDDDDLDDDDDDDDDDDD... Truncated |
Test 10
Verdict: ACCEPTED
input |
---|
1000 1000 ................................. |
correct output |
---|
YES 947 LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL... |
user output |
---|
YES 947 UUUUUUUUUUUUUUUUUUUUUUUUUUUUUU... Truncated |
Test 11
Verdict: ACCEPTED
input |
---|
1000 3 A#B .#. .#. .#. ... |
correct output |
---|
YES 2000 DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD... |
user output |
---|
YES 2000 DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD... Truncated |
Test 12
Verdict: ACCEPTED
input |
---|
3 1000 A................................ |
correct output |
---|
YES 2000 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR... |
user output |
---|
YES 2000 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR... Truncated |
Test 13
Verdict: ACCEPTED
input |
---|
999 999 A#...#...#...#...#...#...#...#... |
correct output |
---|
YES 499998 DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD... |
user output |
---|
YES 499998 DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD... Truncated |
Test 14
Verdict: ACCEPTED
input |
---|
1 3 A.B |
correct output |
---|
YES 2 RR |
user output |
---|
YES 2 RR |
Test 15
Verdict: ACCEPTED
input |
---|
2 2 ## AB |
correct output |
---|
YES 1 R |
user output |
---|
YES 1 R |
Test 16
Verdict: ACCEPTED
input |
---|
1000 1000 A................................ |
correct output |
---|
YES 1998 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR... |
user output |
---|
YES 1998 DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD... Truncated |