Stay Hungry,Stay Foolish!

C - Counting Squares -- ATCODER

C - Counting Squares

https://atcoder.jp/contests/abc275/tasks/abc275_c

参考: https://atcoder.jp/contests/abc275/submissions/36103954

 

思路

首先不能使用暴力穷举法,任意四个点来判断,是否被标注,是否为正方形。

81*80*79*78/(4*3*2) 复杂度太大。

 

按边计数法:

对于任何正方形,可以扣住左边或者左下边的边来统计个数,防止重复计算。

 

 

 

对于这种边, 可以方便地计算另外连个点:

T1 T2

 

 

 

Code

https://atcoder.jp/contests/abc275/submissions/36110355

 

 
map<int, map<int, bool>> pawns;
vector<pair<int, int>> points;
 
int main()
{
    for(int i=1; i<=9; i++){
        string temp;
        cin >> temp;
        
        for(int j=1; j<=9; j++){
            char one = temp[j-1];
            if (one == '#'){
                pawns[j][i] = true;
                points.push_back(make_pair(j, i));
            }
        }
    }
 
    int sum = 0;
    
    int size = points.size();
    for(int i=0; i<size; i++){
        pair<int, int> one = points[i];
        
        for (int j=i+1; j<size; j++){
            pair<int, int> two = points[j];
 
            if(one.second<two.second
            && one.first<=two.first){
                // for every point,
                // first means row
                // second means column
//                cout << "one point = " << one.first << " " << one.second << endl;
//                cout << "two point = " << two.first << " " << two.second << endl;
 
                // first group of points
                // right down side
                // one -> two
                int dx = two.first - one.first;
                int dy = two.second - one.second;
                int t1x = two.first + dy;
                int t1y = two.second - dx;
 
                int t2x = t1x - dx;
                int t2y = t1y - dy;
 
                if(pawns[t1x][t1y] && pawns[t2x][t2y]){
//                    cout << "t1:" << t1x << " " << t1y << endl;
//                    cout << "t2:" << t2x << " " << t2y << endl;
                    sum++;
                }
            }
        }
    }
 
    cout << sum << endl;
 
    return 0;
}

 

posted @ 2022-10-31 12:20  lightsong  阅读(108)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel