C++ easy to understand


  • 0
    class Solution {
    public:
        int maxKilledEnemies(vector<vector<char>>& grid) {
            int m = grid.size();
            if(m==0) return 0;
            int n = grid[0].size();
            if(n==0) return 0;
            int v1[m][n],v2[m][n],v3[m][n],v4[m][n];
            for(int i=0;i<m;i++) {
                for(int j=0;j<n;j++) {
                    v1[i][j]=0;
                    if(j==0) continue;
                    if(grid[i][j-1]=='W') v1[i][j]=0;
                    else if(grid[i][j-1]=='E') v1[i][j] = v1[i][j-1]+1;
                    else v1[i][j] = v1[i][j-1];
                }
                for(int j=n-1;j>=0;j--) {
                    v2[i][j] = 0;
                    if(j==n-1) continue;
                    if(grid[i][j+1]=='W') v2[i][j]=0;
                    else if(grid[i][j+1]=='E') v2[i][j] = v2[i][j+1]+1;
                    else v2[i][j] = v2[i][j+1];
                }
            }
            for(int j=0;j<n;j++) {
                for(int i=0;i<m;i++) {
                    v3[i][j] = 0;
                    if(i==0) continue;
                    if(grid[i-1][j]=='W') v3[i][j] = 0;
                    else if(grid[i-1][j]=='E') v3[i][j] = v3[i-1][j]+1;
                    else v3[i][j] = v3[i-1][j];
                }
                for(int i=m-1;i>=0;i--) {
                    v4[i][j] = 0;
                    if(i==m-1) continue;
                    if(grid[i+1][j]=='W') v4[i][j] = 0;
                    else if(grid[i+1][j]=='E') v4[i][j] = v4[i+1][j]+1;
                    else v4[i][j] = v4[i+1][j];
                }
            }
            int res = 0;
            for(int i=0;i<m;i++) {
                for(int j=0;j<n;j++) {
                    if(grid[i][j]!='0') continue;
                    res = max(res,v1[i][j]+v2[i][j]+v3[i][j]+v4[i][j]);
                }
            }
            return res;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.