阿里巴巴4.19笔试

太菜了,最后,90,55,10

第二题感觉没问题,但是一直错,大佬帮看看

#include<bits/stdc++.h>
using namespace std;

int n, m;
vector<vector<pair<char, int>>> board;
vector<vector<bool>> visited;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

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

int dfs(int x, int y, char color){
    if(!isValid(x, y) || visited[x][y] || board[x][y].first != color) return 0;
    visited[x][y] = true;
    int score = board[x][y].second;
//     cout << "score" << endl;
    for(int i = 0; i < 4; ++i){
        int newX = x + dx[i];
        int newY = y + dy[i];
        score += dfs(newX, newY, color);
    }
    return score;
}

void drop(){
    for(int j = 0; j < m; j++){
        int empty = n - 1;
        for(int i = n - 1; i >= 0; i--){
//             if(visited[i][j]) continue;
            if(board[i][j].first == ' ' || visited[i][j]) continue;
            board[empty][j] = board[i][j];
            visited[empty][j] = visited[i][j];
            empty--;
        }
        for(; empty >= 0; empty--){
            board[empty][j] = make_pair(' ', 0); // ' '
            visited[empty][j] = false;
        }
    }
}

int main(){
    cin >> n >> m;
    board.resize(n, vector<pair<char, int>>(m));
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; ++j){
            string cell;
            cin >> cell;
            board[i][j] = make_pair(cell[0], cell[1] - '0');
        }
    }
    int q;
    cin >> q;
    for(int i = 0; i < q; ++i){
        int x, y;
        cin >> x >> y;
        x--;
        y--;
//         cout << x << "////" <<  y << endl;
        visited.assign(n, vector<bool>(m, false));
        int score = dfs(x, y, board[x][y].first);
        if(score > 0) drop();
        cout << score << endl;
    }
    return 0;
}

全部评论
drop最后一行 empty不能置为false吧
点赞 回复 分享
发布于 2023-04-19 20:54 北京
第一题咋写的,双指针寄了。我第二题过了
点赞 回复 分享
发布于 2023-04-19 20:52 山东
可以分享一下第一题思路吗,怎么过90%的呀
点赞 回复 分享
发布于 2023-04-19 20:50 北京

相关推荐

04-11 21:31
四川大学 Java
野猪不是猪🐗:(ja)va学弟这招太狠了
点赞 评论 收藏
分享
评论
2
4
分享

创作者周榜

更多
牛客网
牛客企业服务