c_cpp https://www.hackerrank.com/contests/worldcodesprint/challenges/two-pluses/copy-from/1301129656

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp https://www.hackerrank.com/contests/worldcodesprint/challenges/two-pluses/copy-from/1301129656相关的知识,希望对你有一定的参考价值。

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

struct cross {
    int x;
    int y;
    int len;
};
vector <cross> results;

bool overlap(cross x, cross y) {
    int xL = x.x-(x.len-1);
    int xR = x.x+(x.len-1);
    int xU = x.y-(x.len-1);
    int xD = x.y+(x.len-1);
    
    int yL = y.x-(y.len-1);
    int yR = y.x+(y.len-1);
    int yU = y.y-(y.len-1);
    int yD = y.y+(y.len-1);
    
    if(xL <= yL && yL <= xR && x.y == y.y) return true;
    if(xL <= yR && yR <= xR && x.y == y.y) return true;
    
    if(xU <= yU && yU <= xD && x.x == y.x) return true;
    if(xU <= yD && yD <= xD && x.x == y.x) return true;
    
    if(xL <= y.x && xR >= y.x ) {
        if(abs(x.y-y.y) <= y.len-1) return true;
    }
    
    if(xU <= y.y && xD >= y.y ) {
        if(abs(x.x-y.x) <= y.len-1) return true;
    }
    
    return false;
    
}

int area(cross x) {
    return (x.len-1)*4 + 1;
}

void pushResults(int i, int j, int len) {
    cross temp;
    temp.y = i; temp.x = j; temp.len = len;
    results.push_back(temp);
}

int main() {
    int grid[15][15];
    int N, M;
    cin>>N>>M;
    
    for(int i=0; i<N; i++) {
        for(int j=0; j<M; j++) {
            char ch;
            cin>>ch;
            if(ch == 'G') grid[i][j] = 1;
            else grid[i][j] = 0;
        }
    }
    
    for(int i=1; i<N-1; i++) {
        for(int j=1; j<M-1; j++) {
            if(!grid[i][j]) continue;
            int len = 1, k;
            for(k=1; i-k>=0 && i+k<N && j+k<M && j-k>=0; k++) {
                if(grid[i+k][j] && grid[i][j+k] && grid[i-k][j] && grid[i][j-k]) {
                    pushResults(i, j, len);
                    len++;
                }
                else {
                    pushResults(i, j, len);
                    break;
                }
            } 
    
            if(k>1) pushResults(i, j, len);
        }
    }
    
    int ans = 0;
    for(int i=0; i<results.size()-1; i++) {
        for(int j=i+1;j<results.size(); j++) {
            if(!overlap(results[i], results[j])) {
                int t = area(results[i])*area(results[j]);
                ans = max(ans, t);
            }
        }
    }
    cout<<ans;
    return 0;
}

以上是关于c_cpp https://www.hackerrank.com/contests/worldcodesprint/challenges/two-pluses/copy-from/1301129656的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 127.单词阶梯

c_cpp MOFSET

c_cpp MOFSET

c_cpp 31.下一个排列

c_cpp string→char *

c_cpp 54.螺旋矩阵