結果

問題 No.2432 Flip and Move
ユーザー momoyuu
提出日時 2023-08-18 22:19:27
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 2,143 bytes
コンパイル時間 2,974 ms
コンパイル使用メモリ 254,208 KB
実行使用メモリ 32,932 KB
最終ジャッジ日時 2024-11-28 07:59:51
合計ジャッジ時間 7,409 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

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


const int mx = 1e6;
bool vis[4][mx+1];
int h,w;
bool ans[mx+1];
inline int tok(int i,int j){
    return i * w + j;
}

ll k;

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin>>h>>w;
    cin>>k;
    ll now = 0;
    int ni = 0;
    int nj = 0;
    int dx = 1;
    int dy = 1;
    stack<pair<int,pair<int,int>>> st;
    st.push(make_pair(0,make_pair(0,0)));
    vis[0][tok(0,0)] = 0;
    bool fn = false;
    while(true){
        if(now==k) break;
        ans[tok(ni,nj)] ^= 1;
        int nni = ni + dx;
        if(nni<0||nni>=h){
            nni = ni;
            dx *= -1;
        }
        int nnj = nj + dy;
        if(nnj<0||nnj>=w){
            nnj = nj;
            dy *= -1;
        }
        ni = nni;
        nj = nnj;
        now++;
        if(fn) continue;
        int mask = 0;
        if(dx==-1) mask += 1;
        if(dy==-1) mask += 2;
        if(vis[mask][tok(nni,nnj)]==0){
            vis[mask][tok(nni,nnj)] = 1;
            st.push(make_pair(mask,make_pair(nni,nnj)));
            continue;
        }
        vis[mask][tok(nni,nnj)] = 1;
        stack<pair<int,int>> nxt;
        ll cnt = 0;
        pair<int,pair<int,int>> want = make_pair(mask,make_pair(nni,nnj));
        while(true){
            if(st.top()==want){
                cnt++;
                nxt.push(make_pair(nni,nnj));
                break;
            }
            int nx = st.top().second.first;
            int ny = st.top().second.second;
            cnt++;
            nxt.push(make_pair(nx,ny));
            st.pop();
            continue;
        }
        fn = true;
        ll res = k - now;
        ll can = res / cnt;
        if(can%2==1){
            while(nxt.size()){
                int ni = nxt.top().first;
                int nj = nxt.top().second;
                ans[tok(ni,nj)] ^= 1;
                nxt.pop();
            }
        }
        now += can * cnt;
    }
    for(int i = 0;i<h;i++){
        for(int j = 0;j<w;j++){
            if(ans[tok(i,j)]==0) cout<<'.';
            else cout<<'#';
        }
        cout<<'\n';
    }
}

0