2018 East Central Regional Contest/ ECNA2018 A. Car Vet (dfs)

Problem Description

Bob Roberts is the self proclaimed “Car Vet” who owns several junk car lots where people can come and search for spare parts for their (ailing) cars. The cars in each lot are parked in a fenced-in m × n grid lot, where each car takes up two grid squares. Each lot also has zero or more grid locations devoted to piles of parts (fenders, 8-track tape players, wheel covers, fuzzy dice, etc.); these locations are effectively blocked. Business has been so good that each of Bob’s lots has only one empty grid space left.

From time to time, Bob or one of his lot attendants drops a part on the ground and it rolls underneath one of the cars. The only way to retrieve it is to move the car out of the way. Depending on the location of the empty grid space, it may be necessary to move several cars in order to achieve this. Cars can only move forward or backward, and car movement is also constrained by the fence, which prevents any car from moving off the lot, and by the blocked grid locations containing piles of parts.
2018 East Central Regional Contest/ ECNA2018 A. Car Vet (dfs)_第1张图片
Figure A.1 shows an example. An ambihelical hexnut has rolled under car number 3 in row 3, column 3 of the grid (shaded dark gray). The space in row 1, column 3 is empty and the space in row 3, column 4 is blocked. The only way to retrieve the part is to move car 8, then car 4, then car 3. Note that if the locations of the empty grid cell and the blocked grid cell were reversed, it would not be possible to retrieve the part.

Figure A.1: Sample lot. Cars 8, 4, and 3 must be moved, in that order, to uncover the grid location in row 3, column 3. This corresponds to Sample Input 1.
The problem here should be obvious: For a given location Bob would like to know how to move cars around to uncover that location, or if it is even possible to uncover it.

Input
Input starts with a line containing two positive integers m n (m, n ≤ 250) indicating number of rows and columns of the junk car lot. Following this are m lines each containing n integers; the jth value on the ith line indicates the contents of the grid square at row i, column j. All values v are in the range −2 ≤ v ≤ 62 500. Each non-negative value indicates that half of a junk car is in that location. Every non-negative value appears exactly twice and these two occurrences are horizontally or vertically adjacent to each other. A value of −1 indicates the empty grid location and a value of −2 indicates a blocked location. There is always exactly one empty location and at least one car, but there may be zero or more blocked locations. Following these m lines is a single line containing two integers r c (1 ≤ r, c ≤ 250) indicating the row and column number of the desired location to be uncovered. This will always correspond to the location of a junk car.

Output
Display, on a single line separated by spaces, the numbers of the cars that must be moved to make the given location empty. They should appear in the order in which they must be moved. If there is more than one sequence of possible moves, display the sequence of shortest length. If there is still a tie, display the sequence that comes lexicographically first according to the car numbers in the sequence. If it is not possible to uncover the desired location, display impossible.

Sample Input

4 4
8 8 -1 9
4 10 10 9
4 3 3 -2
16 16 2 2
3 3

Sample Output

8 4 3

Sample Input2

4 4
8 8 -2 9
4 10 10 9
4 3 3 -1
16 16 2 2
3 3

Sample Output2

impossible

Solution

 补题的时候发现,好无脑的一题。。当时想做居然没做,在搞另一道题
直接从终点开始搜,例如样例: 要想终点腾出来,3必须左移。然后新终点变成4的下面,此时4必须上移。。
直接dfs搜索,若出现重复,或者出现到边界不能移动时,则输出Impossible,否则输出答案。。。


AC Code

#include

using namespace std;
int const maxn = 300;
int board[maxn][maxn];
int visited[250 * 250 + 100];
int emptyI, emptyJ;
int targetI, targetJ;
int dir[4][2] = {{-1, 0},
                 {1,  0},
                 {0,  -1},
                 {0,  1}};
int m, n;

bool inBound(int x, int y) {
    return x >= 0 && y >= 0 && x < m && y < n;
}

bool dfs(int x, int y) {
    if (visited[x * m + y])
        return false;
    visited[x * m + y] = true;
    if (x == emptyI && y == emptyJ)
        return true;
    int id = board[x][y];
    for (auto &d : dir) {
        int xx = x + d[0];
        int yy = y + d[1];
        if (inBound(xx, yy) && board[xx][yy] == id && inBound(xx + d[0], yy + d[1]) &&
            board[xx + d[0]][yy + d[1]] != -2) {
            if (dfs(xx + d[0], yy + d[1])) {
                printf("%d ", board[x][y]);
                return true;
            } else
                return false;
        }
    }
    return false;
}

int main() {
    scanf("%d%d", &m, &n);
    for (int i = 0; i < m; ++i)
        for (int j = 0; j < n; ++j) {
            scanf("%d", &board[i][j]);
            if (board[i][j] == -1)
                emptyI = i, emptyJ = j;
        }
    scanf("%d%d", &targetI, &targetJ);
    if (!dfs(targetI - 1, targetJ - 1))
        puts("impossible");
    return 0;
}

你可能感兴趣的:(ACM,dfs)