001:特殊密码锁

总时间限制: 
1000ms 
内存限制: 
1024kB
描述

有一种特殊的二进制密码锁,由n个相连的按钮组成(n<30),按钮有凹/凸两种状态,用手按按钮会改变其状态。

然而让人头疼的是,当你按一个按钮时,跟它相邻的两个按钮状态也会反转。当然,如果你按的是最左或者最右边的按钮,该按钮只会影响到跟它相邻的一个按钮。

当前密码锁状态已知,需要解决的问题是,你至少需要按多少次按钮,才能将密码锁转变为所期望的目标状态。

输入
两行,给出两个由0、1组成的等长字符串,表示当前/目标密码锁状态,其中0代表凹,1代表凸。
输出
至少需要进行的按按钮操作次数,如果无法实现转变,则输出impossible。
样例输入
011
000
样例输出
1

[java]  view plain  copy
 print ?
  1. import java.util.Scanner;  
  2.   
  3. public class Main {  
  4.   
  5.     static int result = 99999999;  
  6.   
  7.     public static void fun(int start, int end, int count, StringBuffer one,  
  8.             StringBuffer two) {  
  9.         if (start == one.length()) {  
  10.             String temp1 = one.toString();  
  11.             String temp2 = two.toString();  
  12.             if (result > count && temp1.equals(temp2)) {  
  13.                 result = count;  
  14.             }  
  15.             return;  
  16.         }  
  17.         if (one.substring(start, start + 1) == two.substring(start, start + 1)) {  
  18.             // 如果当前值相等,就跳过直接往后数  
  19.             fun(start + 1, end, count, one, two);  
  20.         } else {  
  21.             // 如果当前值不相等,那就不管,直接判断下一个  
  22.             fun(start + 1, end, count, one, two);  
  23.             // 如果当前值不相等,那就改变它,再判断下一个  
  24.             one.replace(start, start + 1, two.substring(start, start + 1));  
  25.             if (start == 0) {  
  26.                 // 如果它是第一个,就改变它后面一个  
  27.                 if (one.substring(start + 1, start + 2).equals("0")) {  
  28.                     one.replace(start + 1, start + 2"1");  
  29.                 } else {  
  30.                     one.replace(start + 1, start + 2"0");  
  31.                 }  
  32.             } else if (start == one.length() - 1) {  
  33.                 // 如果它是最后一个,就改变它前面一个  
  34.                 if (one.substring(start - 1, start).equals("0")) {  
  35.                     one.replace(start - 1, start, "1");  
  36.                 } else {  
  37.                     one.replace(start - 1, start, "0");  
  38.                 }  
  39.             } else {  
  40.                 // 如果既不是最后一个,也不是最前面一个,就改变它前后  
  41.                 if (one.substring(start + 1, start + 2).equals("0")) {  
  42.                     one.replace(start + 1, start + 2"1");  
  43.                 } else {  
  44.                     one.replace(start + 1, start + 2"0");  
  45.                 }  
  46.                 if (one.substring(start - 1, start).equals("0")) {  
  47.                     one.replace(start - 1, start, "1");  
  48.                 } else {  
  49.                     one.replace(start - 1, start, "0");  
  50.                 }  
  51.             }  
  52.             fun(start + 1, end, count + 1, one, two);  
  53.         }  
  54.     }  
  55.   
  56.     public static void main(String[] args) {  
  57.         Scanner scanner = new Scanner(System.in);  
  58.         StringBuffer one = new StringBuffer();  
  59.         StringBuffer two = new StringBuffer();  
  60.         one.append(scanner.next());  
  61.         two.append(scanner.next());  
  62.         if (one.equals(two)) {  
  63.             result = 0;  
  64.         } else if (one.length() == 1) {  
  65.             result = 1;  
  66.         } else {  
  67.             fun(0, one.length(), 0, one, two);  
  68.         }  
  69.         if (result == 99999999) {  
  70.             System.out.println("impossible");  
  71.         } else {  
  72.             System.out.println(result);  
  73.         }  
  74.     }  
  75.   
  76. }  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值