Submission #24453782


Source Code Expand

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll>l_l;

typedef unsigned long long ull;
ull red;
set<ull>used;//調査済みの盤面
vector<string>s;
ll answer=0;
ll n;
vector<ll> dx={1,0,-1,0};
vector<ll> dy={0,1,0,-1};

bool valid(ll x,ll y){
    return 0<=x && x<=n-1 && 0<=y && y<=n-1;//(x,y)が盤面内に含まれているかどうか
}
void change(l_l P){
    //(P.first, P.second)が白マスなら赤マスに、赤マスなら白マスに塗り替える
    ll num=P.first*n+P.second;
    if(s[P.first][P.second]=='@'){
        red-=ull(1)<<num;
        s[P.first][P.second]='.';
    }else{
        red+=ull(1)<<num;
        s[P.first][P.second]='@';
    }
}

void dfs(ll num){
    //num:あと何個赤マスを置くか
    
    if(used.find(red)!=used.end()){
        return;//既に調査済みなので、探索せずに終了する
    }
    used.insert(red);//調査済みの盤面に、現在の盤面を加える

    if(num==0){
        answer++;//現在の盤面は条件を満たす塗り方なので、answerに1加える
        return;  //終了する
    }
    
    vector<l_l>next;//次に赤マスを塗る場所の候補
    
    for(int i=0;i<=n-1;i++){
        for(int j=0;j<=n-1;j++){
            //(i,j)を次の赤マスにできる条件は、
            //s[i][j]が白マス('.')であり、かつ、s[i][j]の隣に赤マス('@')があること
            
            if(s[i][j]=='.'){
                bool flag=false;
                for(int z=0;z<=3;z++){
                    ll nxt_i=i+dx[z];
                    ll nxt_j=j+dy[z];
                    if(valid(nxt_i,nxt_j) && s[nxt_i][nxt_j]=='@'){
                        flag=true;//(i,j)の隣に赤マスがあれば、flagをtrueにする
                    }
                }
                if(flag){
                    next.push_back({i,j});//次に赤マスを塗る場所の候補に、(i,j)を追加する
                }
            }
        }
    }
    for(l_l pos:next){
        change(pos);//赤く塗る
        dfs(num-1); //dfsを進める
        change(pos);//元に戻す
    }
}

signed main(){
    
    ll k;cin>>n>>k;
    s.resize(n);
    for(int i=0;i<=n-1;i++)cin>>s[i];
    
    for(int i=0;i<=n-1;i++){
        for(int j=0;j<=n-1;j++){
            //最初に赤く塗るマスを(i,j)とする
            if(s[i][j]=='.'){
                change({i,j});//赤く塗る
                dfs(k-1);     //dfsを始める
                change({i,j});//元に戻す
            }
        }
    }
    cout<<answer<<endl;
    return 0;
}

Submission Info

Submission Time
Task E - Red Polyomino
User sugarrr
Language C++ (GCC 9.2.1)
Score 500
Code Size 2693 Byte
Status AC
Exec Time 85 ms
Memory 7900 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 32
Set Name Test Cases
Sample example0.txt, example1.txt, example2.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, example0.txt, example1.txt, example2.txt
Case Name Status Exec Time Memory
000.txt AC 7 ms 3568 KiB
001.txt AC 2 ms 3616 KiB
002.txt AC 2 ms 3484 KiB
003.txt AC 2 ms 3480 KiB
004.txt AC 2 ms 3580 KiB
005.txt AC 2 ms 3584 KiB
006.txt AC 2 ms 3432 KiB
007.txt AC 3 ms 3492 KiB
008.txt AC 4 ms 3580 KiB
009.txt AC 2 ms 3588 KiB
010.txt AC 2 ms 3476 KiB
011.txt AC 2 ms 3580 KiB
012.txt AC 43 ms 5616 KiB
013.txt AC 43 ms 5568 KiB
014.txt AC 8 ms 3484 KiB
015.txt AC 6 ms 3564 KiB
016.txt AC 2 ms 3600 KiB
017.txt AC 3 ms 3584 KiB
018.txt AC 56 ms 6168 KiB
019.txt AC 74 ms 7476 KiB
020.txt AC 74 ms 7640 KiB
021.txt AC 65 ms 6752 KiB
022.txt AC 2 ms 3472 KiB
023.txt AC 3 ms 3472 KiB
024.txt AC 4 ms 3480 KiB
025.txt AC 3 ms 3512 KiB
026.txt AC 5 ms 3636 KiB
027.txt AC 2 ms 3448 KiB
028.txt AC 48 ms 5896 KiB
example0.txt AC 2 ms 3424 KiB
example1.txt AC 2 ms 3512 KiB
example2.txt AC 85 ms 7900 KiB