Jeanny
寂兮,寥兮,独立不改,周行而不殆
#include<bitsstdc++.h>
using namespace std;
int b[] = {0, 0, 0, 0, 0, 1, 1, 1, 0}, vis[300][300][3];
/*
  8 7 6 5 4 3 2 1
  c p f m b b g g
  1 1 1 1 1 1 1 1
& 1 0 
-----------------
&     1 0     1
&     1 0       1
&     1 0     1 1
&     0 1 1
&     0 1 0 1
      0 1 1 1

*/
int chk(int ls){
            // if(ls & 11000000 == 10000000) if(ls & 00111111 > 0) 犯人在警察不在且还有其他人
    if((ls & 192) == 128 && (ls & 63) > 0) return 0; 
           // if(ls & 110011  -  100011) //爸爸在妈妈不在,女儿在
    if((ls & 51) >= 33 && (ls & 51) <= 35) return 0;
            // if(ls & 111100 > 011100)//妈妈在爸爸不在,儿子在
    if((ls & 60) == 28 || (ls & 60) == 24 || (ls & 60) == 20) return 0;
    return 1;
}
int ans = INT_MAX, s[3],go[105][3],bk[105][3],lb[105][3],lg[105][3],rid[105], id[105];
void print(int x){
    int s[15], t = 0;
    memset(s, 0, sizeof s);
    while(x){
        s[++t] = (x&1);
        x >>= 1;
    }
    for(int i = 8; i >= 1; i--)
        cout<<s[i];
}
void dfs(int t, int ls, int rs, int r, int peo){//11101100
    // cout<<endl;
    // cout<<"ls,rs: "<<ls<<" "<<rs<<endl;
    // if(t >= 11) return; 
     int s[3] = {ls, rs};//s[0] = ls, s[1] = rs;
    // if(chk(ls) == 0 || chk(rs) == 0) return;
    // cout<<"t1: "<<t<<endl;
    // cout<<ls<<" ";print(ls);cout<<" , "; cout<<rs<<" ";print(rs);cout<<endl;
   
    // cout<<"ls, rs : "<<ls<<" "<<rs<<endl;
    // cout<<"rrrrrrr: "<<r<<" ,t: "<<t<<" "<<endl;
    if(ls == 0 && rs == 255){
        if(t < ans){
            ans = t;
            for(int i = 1; i <= t; i++){
                id[i] = rid[i], lg[i][1] = go[i][1], lg[i][2] = go[i][2], lb[i][1] = bk[i][1], lb[i][2] = bk[i][2];
            }   
        }
        return;
    //    ans = min(ans, t);
       
    }
    // cout<<"r:"<<r<<endl;
    if(r == 1)
        for(int i = 1; i <= 8; i++){  
            if(b[i] == 0) continue;//成年人非犯人可以过河
            if((1 << (i - 1) & s[r]) == 0) continue;//i这个人在   
            if(chk(s[r] & ~(1 <<(i-1))) && chk(s[r^1] | (1 <<(i-1)))) {
                // cout<<"回—i: "<<i<<" "<<r<<endl;
                // cout<<endl;
                //回来之后该去右边了
                // cout<<(s[r^1] | (1 <<(i-1)))<<" "<<(s[r] & ~(1 <<(i-1)))<<" "<<vis[s[r^1] | (1 <<(i-1))][s[r] & ~(1 <<(i-1))][r^1]<<endl;
                if(vis[s[r^1] | (1 <<(i-1))][s[r] & ~(1 <<(i-1))][r^1] == 1) continue;
                vis[s[r^1] | (1 <<(i-1))][s[r] & ~(1 <<(i-1))][r^1] = 1;
                if(peo == i) continue;
                rid[t+1] = r;
                bk[t+1][1] = i; bk[t+1][2] = 0;
                dfs(t+1, s[r^1] | (1 <<(i-1)), s[r] & ~(1 <<(i-1)),  r^1, i);
                vis[s[r^1] | (1 <<(i-1))][s[r] & ~(1 <<(i-1))][r^1] = 0;
            }
        }
    for(int i = 1; i < 8; i++){//0 1 1 1 1 1 1 1
        if(((1 << (i - 1)) & s[r]) == 0) continue;//i这个人在
        for(int j = i+1; j <= 8; j++){
            if((1 << (j - 1) & s[r]) == 0) continue;
            if(peo == i*10+j) continue;
            // cout<<ls<<" ";print(ls);cout<<" , "; cout<<rs<<" ";print(rs);cout<<endl;
            if(b[i] == 0 && b[j] == 0) continue;
            // cout<<i<<"  ___ "<<j<<endl;
            // cout<<chk(s[r] & ~(1 <<(i-1)) & ~(1 << (j-1))) <<endl;
            // cout<<chk(s[r^1] | (1 <<(i-1)) | (1 <<(j-1)))<<endl;
            // cout<<"r: "<<r<<endl;
            //不是小孩和犯人
            // cout<<"1,i,j: "<<i<<" "<<j<<endl;
            if(chk(s[r] & ~(1 <<(i-1)) & ~(1 << (j-1))) && chk(s[r^1] | (1 <<(i-1)) | (1 <<(j-1)))) {
                // cout<<i<<"  ___ "<<j<<endl;
                // cout<<"peo: "<<peo<<endl;
                // cout<<"tt,r:"<<t<<" "<<r<<endl;
                // print(s[r^1]);cout<<" ";print(s[r]);cout<<endl;
                // print(ls);cout<<" ";print(rs); cout<<endl;
                // cout<<"r_change:"<<r<<endl;
                if(r == 0){
                    // cout<<"ppp : "<<(s[r] & ~(1 <<(i-1)) & ~(1 << (j-1)))<<" "<<(s[r^1] | (1 <<(i-1)) | (1 <<(j-1)))<<endl;
                    if(vis[s[r] & ~(1 <<(i-1)) & ~(1 << (j-1))][ s[r^1] | (1 <<(i-1)) | (1 <<(j-1))][r^1] == 1) continue;
                    vis[s[r] & ~(1 <<(i-1)) & ~(1 << (j-1))][ s[r^1] | (1 <<(i-1)) | (1 <<(j-1))][r^1] = 1;
                    if(peo == i*10+j) continue;
                    rid[t+1] = r;
                    go[t+1][1] = i, go[t+1][2] = j; 
                    dfs(t+1, s[r] & ~(1 <<(i-1)) & ~(1 << (j-1)),  s[r^1] | (1 <<(i-1)) | (1 <<(j-1)), r^1, i*10+j);
                    vis[s[r] & ~(1 <<(i-1)) & ~(1 << (j-1))][ s[r^1] | (1 <<(i-1)) | (1 <<(j-1))][r^1] = 0;
                    // cout<<endl;
                } 
                else if(r == 1){
                    if( vis[s[r^1] | (1 <<(i-1)) | (1 <<(j-1))][ s[r] & ~(1 <<(i-1)) & ~(1 << (j-1))][r^1] == 1) continue;
                    vis[s[r^1] | (1 <<(i-1)) | (1 <<(j-1))][ s[r] & ~(1 <<(i-1)) & ~(1 << (j-1))][r^1] = 1;
                    if(peo == i*10+j) continue;
                    rid[t+1] = r;
                    bk[t+1][1] = i, bk[t+1][2] = j;
                    dfs(t+1, s[r^1] | (1 <<(i-1)) | (1 <<(j-1)),  s[r] & ~(1 <<(i-1)) & ~(1 << (j-1)),  r^1 , i*10+j);
                    vis[s[r^1] | (1 <<(i-1)) | (1 <<(j-1))][ s[r] & ~(1 <<(i-1)) & ~(1 << (j-1))][r^1] = 0;
                }        
            }
        }
    }
}
int main(){
    dfs(0,255,0,0,0);
    for(int i = 1; i <= ans; i++){
        if(id[i] == 0)
            cout<<"qu: "<<lg[i][1]<<" "<<lg[i][2]<<endl;
        else
            cout<<"hui: "<<lb[i][1]<<" "<<lb[i][2]<<endl;
    }
    cout<<ans<<endl;
    return 0;
}
posted on 2025-03-16 12:32  Jeanny  阅读(12)  评论(0)    收藏  举报