TNFSH judge 362

首次架設靜態網站,如有疏失,還請多多包涵。歡迎來信zhuastor@gmail.com

解題心得

稍微玩一下DFS,在某題單的挑戰題看到,一開始覺得可能是需要高難度剪枝,結果也沒有。就還蠻酷的題目。

解題想法

就DFS。

AC code

#include<bits/stdc++.h>
using namespace std;
int aa[13],bb[13],cc[13],dd[13];
bool ta;
void dfs(int a,int b,int c,int d){
    if(ta)return;
    if(a==13 && b==13 && c==13 && d==13){
        ta=true;
        return;
    }
    if(a<=12 && b<=12 && aa[a]==bb[b]){
        dfs(a+1,b+1,c,d);
    }
    if(a<=12 && c<=12 && aa[a]==cc[c]){
        dfs(a+1,b,c+1,d);
    }
    if(a<=12 && d<=12 && aa[a]==dd[d]){
        dfs(a+1,b,c,d+1);
    }
    if(c<=12 && b<=12 && bb[b]==cc[c]){
        dfs(a,b+1,c+1,d);
    }
    if(d<=12 && b<=12 && bb[b]==dd[d]){
        dfs(a,b+1,c,d+1);
    }
    if(c<=12 && d<=12 && cc[c]==dd[d]){
        dfs(a,b,c+1,d+1);
    }
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    while(cin >> aa[0]){
        for(int x=1;x<=12;x++)cin >> aa[x];
        for(int x=0;x<=12;x++)cin >> bb[x];
        for(int x=0;x<=12;x++)cin >> cc[x];
        for(int x=0;x<=12;x++)cin >> dd[x];
        dfs(0,0,0,0);
        if(!ta){
            cout << "NO" << endl;
        }
        else{
            cout << "YES" << endl;
        }
    }
    return 0;
}

題目連結 : 05校內複選D.大紙牌