ACWing116. 飞行员兄弟

Acwing 116. 飞行员兄弟

【思路】
16个开关,用二进制表示对16个开关的可能操作方案。即从0到2^16 -1开始枚举,该数的01二进制串1表示对应位置摁,0表示对应位置不摁。位置与矩阵的对应关系如下:

在这里插入图片描述

import java.io.*;
import java.util.ArrayList;
import java.util.List;

class node{
    int x, y;
    public node(int xx, int yy){
        x = xx;
        y = yy;
    }
}
public class Main{
    /*
    同一行翻偶数次抵消 奇数次反转
    */
    static int N = 4;
    static char g[][] = new char[N][N];
    static char backup[][] = new char[N][N];
	
	//十字形翻转
    public static void change(int i, int j){
        for(int x = 0; x < 4; x ++){
            g[i][x] = g[i][x]== '-'? '+':'-';
            g[x][j] = g[x][j]== '-'? '+':'-';
        }
        //注意x,y这个位置上面翻了两次 要翻多一次回来
        g[i][j] = g[i][j]== '-'? '+':'-';
    }
    public static boolean check(){
        for(int i = 0; i < 4; i ++)
            for(int j = 0; j < 4; j++)
                if(g[i][j] == '+') return false;
        return true;
    }
    //将二维坐标映射为一维
    public static int get(int x, int y){
       return x * 4 + y;
   }
    public static void main(String args[])throws Exception{
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        for(int i = 0; i < 4; i++) 
            g[i] = bf.readLine().toCharArray();
        for(int i = 0; i < 4; i ++)
            for(int j = 0; j < 4; j++)
                backup[i][j] = g[i][j];
                
        List<node> res = new ArrayList<>();
        for(int op = 0; op < 1 << 16; op ++){// 1 <<16 为2^16
        
            List<node> tmp = new ArrayList<>();
            
            //还原
            for(int i = 0; i < 4; i ++)
                for(int j = 0; j < 4; j++)
                    g[i][j] = backup[i][j];
                
            for(int i = 0; i < 4; i ++)
                for(int j = 0; j < 4; j ++)
                    if( (  op >> get(i, j) &1 )== 1){//0 1 2 3 4
                        tmp.add( new node(i, j));
                        change(i, j);
                    }
            
           
                    
            if( check() ) //全部把手已打开
                if(res.isEmpty() || res.size() > tmp.size() ) res = new ArrayList<>(tmp); 
                
        }   
        System.out.println(res.size() );
        for(node t: res)
            System.out.println( t.x + 1 +" "+  (t.y + 1)  );
                
        
        
        
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值