結果

問題 No.86 TVザッピング(2)
ユーザー GOTKAKO
提出日時 2025-06-30 05:22:53
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 4,334 bytes
コンパイル時間 2,183 ms
コンパイル使用メモリ 209,496 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-06-30 05:22:58
合計ジャッジ時間 3,237 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<typename T>
istream &operator>>(istream &is,vector<T> &a){
    for(auto &v : a) cin >> v;
    return is;
}
template<typename T>
ostream &operator<<(ostream &os,const vector<T> &a){
    if(a.size() == 0) return os;
    cout << a.at(0);
    for(int i=1; i<a.size(); i++) cout << " " << a.at(i);
    cout << "\n";
    return os;
}

template<typename T>
struct Point{
    public:
    T x,y;
    Point() : x(0),y(0) {}
    Point(T a,T b) : x(a),y(b) {}
    Point &operator=(const Point &b) = default;
    Point operator+(const Point &b){return Point(x+b.x,y+b.y);}
    Point operator-(const Point &b){return Point(x-b.x,y-b.y);}
    Point operator+=(const Point &b){return *this=*this+b;}
    Point operator-=(const Point &b){return *this=*this-b;}
    bool operator==(const Point &b){return x==b.x && y==b.y;}
    bool operator!=(const Point &b){return x!=b.x || y!=b.y;}
    bool operator<(const Point &b){
        if(x == b.x) return y < b.y;
        else return x < b.x; 
    }
    bool operator<=(const Point &b){return (*this) < b || (*this) == b;}
    bool operator>(const Point &b){return !((*this) <= b);}
    bool operator>=(const Point &b){return !((*this) < b);}

    friend T inner(const Point a,const Point b){return a.x*b.x+a.y*b.y;}
    friend T cross(const Point a,const Point b){return a.x*b.y-a.y*b.x;}
    friend T twodist(const Point a,const Point b){return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);}//2乗で返す.
};
template<typename T>
istream &operator>>(istream &is,Point<T> &a){
    is >> a.x >> a.y;
    return is;
}
template<typename T>
ostream &operator<<(ostream &os,Point<T> &a){
    os << a.x << " " << a.y;
    return os;
}
template<typename T>
vector<Point<T>> Convexhull(vector<Point<T>> &P){
    //(x,y)が最小の点から時計回りに頂点を返す.
    //(xi,yi)と(xi+1,yi+1)は辺になる |P|=1の時注意.
    sort(P.begin(),P.end());
    P.erase(unique(P.begin(),P.end()),P.end());
    if(P.size() == 0) return {};
    if(P.size() == 1) return {P.at(0),P.at(0)};
    vector<Point<T>> A,B;
    for(auto &p1 : P){
        while(A.size() >= 2){
            auto &p2 = A.at(A.size()-1);
            auto &p3 = A.at(A.size()-2);
            T check = cross(p2-p1,p3-p2);
            if(check < 0) A.pop_back();
            else break;
        }
        A.push_back(p1);
    }
    for(auto &p1 : P){
        while(B.size() >= 2){
            auto &p2 = B.at(B.size()-1);
            auto &p3 = B.at(B.size()-2);
            T check = cross(p2-p1,p3-p2);
            if(check > 0) B.pop_back();
            else break;
        }
        B.push_back(p1);
    }
    for(int i=B.size()-2; i>=0; i--) A.push_back(B.at(i));
    return A;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N,M; cin >> N >> M;
    vector<string> S(N);
    for(auto &s : S) cin >> s;
    vector<Point<int>> P;
    for(int i=0; i<N; i++) for(int k=0; k<M; k++) if(S.at(i).at(k) == '.') P.push_back({i,k});

    auto route = Convexhull(P);
    if(route.size() <= 3){cout << "NO\n"; return 0;}
    
    int ok = 0,hekomi = 0;
    for(int i=0; i<route.size()-1; i++){
        auto [x,y] = route.at(i);
        auto [x2,y2] = route.at(i+1);
        if(abs(x-x2)+abs(y-y2) == 1){ok++; continue;}
        if(x == x2 || y == y2 || hekomi == 1){cout << "NO\n"; return 0;}
        hekomi++;
        int dx = clamp(x2-x,-1,1),dy = clamp(y2-y,-1,1);
        if(S.at(x+dx).at(y) == '.'){
            while(x != x2){
                x += dx;
                if(S.at(x).at(y) == '.') ok++;
                else{cout << "NO\n"; return 0;}
            }
            while(y != y2){
                y += dy;
                if(S.at(x).at(y) == '.') ok++;
                else{cout << "NO\n"; return 0;}
            }
            continue;
        }
        if(S.at(x).at(y+dy) == '.'){
            while(y != y2){
                y += dy;
                if(S.at(x).at(y) == '.') ok++;
                else{cout << "NO\n"; return 0;}
            }
            while(x != x2){
                x += dx;
                if(S.at(x).at(y) == '.') ok++;
                else{cout << "NO\n"; return 0;}
            }
            continue;
        }
        cout << "NO\n"; return 0;
    }
    if(ok == P.size()) cout << "YES\n";
    else cout << "NO\n";
}
0