Submission #24453774


Source Code Expand

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

set<vector<string>>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 dfs(ll num){
    //num:あと何個赤マスを置くか

    if(used.find(s)!=used.end()){
        return;//既に調査済みなので、探索せずに終了する
    }
    used.insert(s);//調査済みの盤面に、現在の盤面を加える

    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){
        s[pos.first][pos.second]='@';//赤く塗る
        dfs(num-1);                  //dfsを進める
        s[pos.first][pos.second]='.';//元に戻す
    }
}

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]=='.'){
                s[i][j]='@';//赤く塗る
                dfs(k-1);   //dfsを始める
                s[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 2331 Byte
Status AC
Exec Time 541 ms
Memory 34952 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 3504 KiB
001.txt AC 1 ms 3448 KiB
002.txt AC 2 ms 3552 KiB
003.txt AC 2 ms 3580 KiB
004.txt AC 2 ms 3436 KiB
005.txt AC 2 ms 3612 KiB
006.txt AC 4 ms 3544 KiB
007.txt AC 5 ms 3752 KiB
008.txt AC 2 ms 3456 KiB
009.txt AC 3 ms 3592 KiB
010.txt AC 2 ms 3444 KiB
011.txt AC 2 ms 3516 KiB
012.txt AC 244 ms 18836 KiB
013.txt AC 201 ms 17444 KiB
014.txt AC 16 ms 4188 KiB
015.txt AC 20 ms 4424 KiB
016.txt AC 7 ms 3608 KiB
017.txt AC 5 ms 3484 KiB
018.txt AC 317 ms 22644 KiB
019.txt AC 455 ms 31580 KiB
020.txt AC 499 ms 32232 KiB
021.txt AC 385 ms 27072 KiB
022.txt AC 3 ms 3644 KiB
023.txt AC 4 ms 3644 KiB
024.txt AC 2 ms 3504 KiB
025.txt AC 2 ms 3616 KiB
026.txt AC 10 ms 3972 KiB
027.txt AC 2 ms 3448 KiB
028.txt AC 266 ms 19176 KiB
example0.txt AC 2 ms 3612 KiB
example1.txt AC 2 ms 3516 KiB
example2.txt AC 541 ms 34952 KiB