116. 飞行员兄弟

116. 飞行员兄弟

dfs写法

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#define x first
#define y second
using namespace std;
const int N = 6;
typedef pair<int,int> PII;
char str[N][N];
vector<PII> res;
vector<PII> temp;


void turn_all(int r,int c)
{
    for(int i=1;i<=4;i++)
    {
        str[i][c] =(str[i][c] =='-' ? '+' : '-');
        str[r][i] =(str[r][i] == '-' ?'+':'-');
    }
    str[r][c] = (str[r][c]=='-' ?'+':'-');
}

void dfs(int r,int c)
{
    //cout<<"当前的行和列"<<r<<" "<<c<<"\n";
    if(r==4 && c==5)
    {
        bool found = true;
        for(int i=1;i<=4;i++)
            for(int j=1;j<=4;j++)   
            {
                if(str[i][j] =='+')
                {
                    found = false;
                    break;
                }
            }
        if(found)
        {
            if(res.empty() || res.size() > temp.size())
                res = temp;
        }    
        return;    
    }
    if(c==5)
    {
        r +=1;
        c=1;
    }

    //修改和不修改递归谁在前都行,但是无论谁在前都需要先恢复现场
    //不修改递归
   // turn_all(r,c);
   // temp.pop_back();
    dfs(r,c+1);
    
    //修改递归
    turn_all(r,c);
    temp.push_back({r,c});
    dfs(r,c+1);
    
    //恢复现场
    temp.pop_back();
    turn_all(r,c);
}
int main()
{
    for(int i=1;i<=4;i++)
        for(int j=1;j<=4;j++)
            cin>>str[i][j];
    dfs(1,1);
    cout<<res.size()<<endl;
    for(int i=0;i<res.size();i++)
        cout<<res[i].x<<" "<<res[i].y<<endl;
}

数组位运算写法

用数组位运算时,输入的下标最好从0开始。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#define x first
#define y second
using namespace std;
typedef pair<int,int> PII;
const int N = 6;
char str[N][N],backup[N][N];
vector<PII> res;
int get_num(int r,int c)	//返回这个数0 1 2 3 4 5---15
{
    return 4*r + c;
}
void turn(int r,int c)	//修改一行一列
{
    for(int i=0;i<4;i++)
    {
        str[i][c] = (str[i][c] =='+' ? '-':'+');
        str[r][i] = (str[r][i] =='+' ? '-':'+');
    }
    str[r][c] = (str[r][c] =='+' ?'-' :'+');
}
int main()
{
    for(int i=0;i<4;i++)
        for(int j=0;j<4;j++)
            cin>>str[i][j];
    for(int op = 0; op < 1<<16 ;op++)   //枚举2^16次方种情况。二进制上该位为1表示turn 为0表示不turn
    {
        vector <PII> temp;
        memcpy(backup,str,sizeof(str)); //备份原始数组
        for(int i=0;i<4;i++)
            for(int j=0;j<4;j++)
            {
                if(op>>get_num(i,j) & 1) //op的第多少位  0~15
                {
                    temp.push_back({i,j});
                    turn(i,j);
                }
            }
        bool found = true;
        for(int i=0;i<4;i++)
            for(int j=0;j<4;j++)
            {
                if(str[i][j] == '+')
                {
                    found = false;
                    break;
                }
            }
        if(found)
        {
            if(res.empty() || res.size() > temp.size())
                res = temp;
        }
        memcpy(str,backup,sizeof(str));     //还原
    }
    cout<<res.size()<<"\n";
    for(int i=0;i<res.size();i++)
        cout<<res[i].x + 1<<' '<<res[i].y + 1<<"\n";
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值