lift and throw
import java.util.*;
import java.math.*; public class Main {
public static void main(String[] args) { Scanner sc = new Scanner(System.in);
int aPos = sc.nextInt();
int aMov = sc.nextInt();
int aThr = sc.nextInt();
int bPos = sc.nextInt();
int bMov = sc.nextInt();
int bThr = sc.nextInt();
int cPos = sc.nextInt();
int cMov = sc.nextInt();
int cThr = sc.nextInt();
sc.close(); Solution s = new Solution();
s.start(aPos, aMov, aThr, bPos, bMov, bThr, cPos, cMov, cThr);
}
} class Solution {
private int maxLength = 0; private int aMov;
private int aThr;
private int bMov;
private int bThr;
private int cMov;
private int cThr; public void start(int aPos, int aMov, int aThr,
int bPos, int bMov, int bThr,
int cPos, int cMov, int cThr) {
this.aMov = aMov;
this.aThr = aThr;
this.bMov = bMov;
this.bThr = bThr;
this.cMov = cMov;
this.cThr = cThr;
recursion(aPos, bPos, cPos, 511, 0, 0, 0);
System.out.println(this.maxLength);
} public void recursion(int curLengthA, int curLengthB, int curLengthC, int movStates, int aPos, int bPos, int cPos) {
int curMax = max_3(curLengthA, curLengthB, curLengthC);
if(maxLength < curMax)
maxLength = curMax; if(movStates == 0 || curLengthA < 0 || curLengthB < 0 || curLengthC < 0)
return ; //Walk
if( ((movStates & 448) == 256 || (movStates & 448) == 448 ) && aPos == 0) {
for(int i = 1; i <= this.aMov; i++) {
if(curLengthA + i != curLengthB && curLengthA + i != curLengthC)
recursion(curLengthA + i, curLengthB, curLengthC, movStates & 255, aPos, bPos, cPos);
if(curLengthA - i != curLengthB && curLengthA - i != curLengthC)
recursion(curLengthA - i, curLengthB, curLengthC, movStates & 255, aPos, bPos, cPos);
}
}
if( ((movStates & 56) == 32 || (movStates & 56) == 56 ) && bPos == 0) {
for(int i = 1; i <= this.bMov; i++) {
if(curLengthB + i != curLengthA && curLengthB + i != curLengthC)
recursion(curLengthA, curLengthB + i, curLengthC, movStates & 479, aPos, bPos, cPos);
if(curLengthB - i != curLengthA && curLengthB - i != curLengthC)
recursion(curLengthA, curLengthB - i, curLengthC, movStates & 479, aPos, bPos, cPos);
}
}
if( ((movStates & 7) == 4 || (movStates & 7) == 7 ) && cPos == 0) {
for(int i = 1; i <= this.cMov; i++) {
if(curLengthC + i != curLengthB && curLengthC + i != curLengthA)
recursion(curLengthA, curLengthB, curLengthC + i, movStates & 507, aPos, bPos, cPos);
if(curLengthC - i != curLengthB && curLengthC - i != curLengthA)
recursion(curLengthA, curLengthB, curLengthC - i, movStates & 507, aPos, bPos, cPos);
}
} // lift
if(Math.abs(curLengthA - curLengthB) == 1 && (movStates & 128) == 128 && aPos == 0 && bPos == 0) // a lift b
recursion(curLengthA, curLengthA, curLengthC == curLengthB ? curLengthA : curLengthC, movStates & 383, 0, 1, cPos == 1 ? 2 : 0);
if(Math.abs(curLengthA - curLengthB) == 1 && (movStates & 16) == 16 && aPos == 0 && bPos == 0) // b lift a
recursion(curLengthB, curLengthB, curLengthC == curLengthA ? curLengthB : curLengthC, movStates & 495, 1, 0, cPos == 1 ? 2 : 0);
if(Math.abs(curLengthA - curLengthC) == 1 && (movStates & 128) == 128 && aPos == 0 && cPos == 0) // a lift c
recursion(curLengthA, curLengthB == curLengthC ? curLengthA : curLengthB, curLengthA, movStates & 383, 0, bPos == 1 ? 2 : 0, 1);
if(Math.abs(curLengthA - curLengthC) == 1 && (movStates & 2) == 2 && aPos == 0 && cPos == 0) // c lift a
recursion(curLengthC, curLengthB == curLengthA ? curLengthC : curLengthB, curLengthC, movStates & 509, 1, bPos == 1 ? 2 : 0, 0);
if(Math.abs(curLengthB - curLengthC) == 1 && (movStates & 16) == 16 && bPos == 0 && cPos == 0) // b lift c
recursion(curLengthA == curLengthC ? curLengthB : curLengthA, curLengthB, curLengthB, movStates & 495, aPos == 1 ? 2 : 0, 0, 1);
if(Math.abs(curLengthB - curLengthC) == 1 && (movStates & 2) == 2 && bPos == 0 && cPos == 0) // c lift b
recursion(curLengthA == curLengthB ? curLengthC : curLengthA, curLengthC, curLengthC, movStates & 509, aPos == 1 ? 2 : 0, 1, 0); //throw
if(aPos + bPos + cPos == 1) { int lifting = 0;
if((movStates & 24) == 8)
lifting = 1;
if((movStates & 3) == 1)
lifting = 2; int throwed = 0;
if(bPos == 1)
throwed = 1;
if(cPos == 1)
throwed = 2; if(lifting == 0 && throwed == 1) { //a throw b
for(int i = 1; i <= this.aThr; i++) {
if(i + curLengthB != curLengthC)
recursion(curLengthA, curLengthB + i, curLengthC, movStates & 447, 0, 0, 0);
if(i - curLengthB != curLengthC)
recursion(curLengthA, curLengthB - i, curLengthC, movStates & 447, 0, 0, 0);
}
}
if(lifting == 0 && throwed == 2) { //a throw c
for(int i = 1; i <= this.aThr; i++) {
if(i + curLengthC != curLengthB)
recursion(curLengthA, curLengthB, curLengthC + i, movStates & 447, 0, 0, 0);
if(i - curLengthC != curLengthB)
recursion(curLengthA, curLengthB, curLengthC - i, movStates & 447, 0, 0, 0);
}
}
if(lifting == 1 && throwed == 0) { //b throw a
for(int i = 1; i <= this.bThr; i++) {
if(i + curLengthA != curLengthC)
recursion(curLengthA + i, curLengthB, curLengthC, movStates & 503, 0, 0, 0);
if(i - curLengthA != curLengthC)
recursion(curLengthA - i, curLengthB, curLengthC, movStates & 503, 0, 0, 0);
}
}
if(lifting == 1 && throwed == 2) { //b throw c
for(int i = 1; i <= this.bThr; i++) {
if(i + curLengthC != curLengthA)
recursion(curLengthA, curLengthB, curLengthC + i, movStates & 503, 0, 0, 0);
if(i - curLengthC != curLengthA)
recursion(curLengthA, curLengthB, curLengthC - i, movStates & 503, 0, 0, 0);
}
}
if(lifting == 2 && throwed == 0) { //c throw a
for(int i = 1; i <= this.cThr; i++) {
if(i + curLengthA != curLengthB)
recursion(curLengthA + i, curLengthB, curLengthC, movStates & 510, 0, 0, 0);
if(i - curLengthA != curLengthB)
recursion(curLengthA - i, curLengthB, curLengthC, movStates & 510, 0, 0, 0);
}
}
if(lifting == 2 && throwed == 1) { //c throw b
for(int i = 1; i <= this.cThr; i++) {
if(i + curLengthB != curLengthA)
recursion(curLengthA, curLengthB + i, curLengthC, movStates & 510, 0, 0, 0);
if(i - curLengthB != curLengthA)
recursion(curLengthA, curLengthB - i, curLengthC, movStates & 510, 0, 0, 0);
}
}
}
else if(aPos + bPos + cPos == 3) { int throwing = 0;
if(bPos == 0)
throwing = 1;
if(cPos == 0)
throwing = 2; if(throwing == 0) {
for(int i = 1; i <= this.aThr; i++) {
recursion(curLengthA, curLengthB + i, curLengthC + i, movStates & 447, 0, bPos - 1, cPos - 1);
recursion(curLengthA, curLengthB - i, curLengthC - i, movStates & 447, 0, bPos - 1, cPos - 1);
}
}
if(throwing == 1) {
for(int i = 1; i <= this.bThr; i++) {
recursion(curLengthA + i, curLengthB, curLengthC + i, movStates & 503, aPos - 1, 0, cPos - 1);
recursion(curLengthA - i, curLengthB, curLengthC - i, movStates & 503, aPos - 1, 0, cPos - 1);
}
}
if(throwing == 2) {
for(int i = 1; i <= this.cThr; i++) {
recursion(curLengthA + i, curLengthB + i, curLengthC, movStates & 510, aPos - 1, bPos - 1, 0);
recursion(curLengthA - i, curLengthB - i, curLengthC, movStates & 510, aPos - 1, bPos - 1, 0);
}
}
}
}
private int max_3(int a, int b, int c) {
if(a >= b && a >= c)
return a;
else if(b >= a && b >= c)
return b;
else
return c;
} }
这道题目是蓝桥杯官网练习系统的一道题目,我目前没有找到什么巧解来解决这道题目,大致上只是用暴力列举每一种情况。
当然这样做的坏处就是要面对极多的逻辑控制。我这代码最终也没有拿全分数,有3组测试数据跑出了时间限制。而且由于逻辑的控制过于复杂(对我来说),在不知道测试数据的情况下慢慢自己debug到现在已经是最好的情况了。其实继续下去也许能解决这个问题,但实在太恶心,等一些高人拿出比较精辟的想法吧。
lift and throw的更多相关文章
- 算法训练 Lift and Throw
算法训练 Lift and Throw 时间限制:3.0s 内存限制:256.0MB 问题描述 给定一条标有整点(1, 2, 3, ...)的射线. 定义两个点之间的距离为其下标之差 ...
- Java实现 蓝桥杯 算法训练 Lift and Throw
试题 算法训练 Lift and Throw 问题描述 给定一条标有整点(1, 2, 3, -)的射线. 定义两个点之间的距离为其下标之差的绝对值. Laharl, Etna, Flonne一开始在这 ...
- Codeforces Round #326 (Div. 2) B. Pasha and Phone C. Duff and Weight Lifting
B. Pasha and PhonePasha has recently bought a new phone jPager and started adding his friends' phone ...
- 总是有一个程序的bug没找到
算法训练 Lift and Throw 时间限制:3.0s 内存限制:256.0MB 问题描述 给定一条标有整点(1, 2, 3, ...)的射线. 定义两个点之间的距离为其下标之 ...
- Codeforces Round #326 (Div. 2) C. Duff and Weight Lifting 水题
C. Duff and Weight Lifting Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- 浅谈Java的throw与throws
转载:http://blog.csdn.net/luoweifu/article/details/10721543 我进行了一些加工,不是本人原创但比原博主要更完善~ 浅谈Java异常 以前虽然知道一 ...
- C++异常处理:try,catch,throw,finally的用法
写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使 ...
- java中的throw与throws的区别
什么时运行时异常?什么是非运行时异常? 通俗的讲: 运行时异常:就是编译通过,运行时就崩了,比如数组越界. 非运行时异常:就是编译不通过,这时就得必须去处理了.不然就没法运行了. 全面的讲: Thro ...
- js 利用throw 写的一个小程序
在下边的小程序中比较特殊的是使用isNaN()函数判断一个参数是不是数字, <!DOCTYPE html> <!DOCTYPE html> <html> <h ...
随机推荐
- [No000024]鲜为人知的编程真相
当程序员的经历让我知道了一些关于软件编程的事情.下面的这些事情可能会让朋友们对软件开发感到惊讶: 一个程序员用在写程序上的时间大概占他的工作时间的10-20% ,大部分的程序员每天大约能写出10-12 ...
- [No000014]听说不背单词,考英语会是这种下场-我们为什么必须背单词?
由于英语对于一个程序员来说,重要性你懂得.因此我会开始逐渐在博客上加入英语的一些东西. 听说不背单词,考英语会是这种下场 在中国, 「学英语」大抵遵循着这样一条 罗蒙诺索夫质量守恒定律 因为英语学着学 ...
- noip2013 火柴排队
题目描述 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相同, 两列火柴之间的距离定义为: ∑(ai-bi)^2 其中 ai 表示 ...
- nginx的主要用途
1.反向代理加速(无缓存),简单的负载均衡和容错 : 问题一:为什么反向代理可以做加速服务器? 反向代理接受发给web服务器的真实请求,但与web服务器不同的是,它们可以向其他的服务器进行通信.以便 ...
- Centos6 安装 Redis
先确认gcc和tcl已经安装 sudo yum install gcc-c++ sudo yum install tcl 解压, 编译和安装 .tar.gz /usr/src/ cd /usr/src ...
- windows 10磁盘占用100%解决方案
可以试试在 控制面板–管理工具–服务– HomeGroup Listener和HomeGroup Provider禁用. (这2项服务是家庭组共享用的,一般我们也不会去共享什么的.) 效果:我的磁盘是 ...
- npm淘宝镜像
前端开发会用到npm的包,但是国外的速度有时候很慢,幸运的是,淘宝做了镜像,一起来看看吧. https://npm.taobao.org/
- 兼容利器之X-UA-Compatible
文档兼容模式 不同浏览器之间经常产生各种奇异的现象,为了解决这些问题,使用以下方法,发现很多问题自动消失不见了 <meta http-equiv="X-UA-Compatible&qu ...
- [py]简易pick lucky num程序
程序功能: 1,用户输入数字,当用户输入指定数字时候,输出他输入的循环那次 2,第二次询问是否还要输 3,如果no 则 终止 4,如果yes则继续输入 判断输入是否大于首次输入的 如果大于则开始循环输 ...
- linux ISO/IMG make
sudo dd if=/PATH/*.ISO of=/dev/sdb 1.制作启动U盘需要sdb,不能sdb1,否则会提示isolinux.bin文件丢失 2.TF卡,设置sdb1?忘了 /* sy ...