usaco msquare java

来源:互联网 发布:淘宝网店经营地址无效 编辑:程序博客网 时间:2024/05/16 10:14
/*ID: daniel.20LANG: JAVATASK: msquare*/import java.util.*;import java.io.*;class square{    int t;    String pre="";    int step;    square(int a,int b,String c){        t = a;        pre+=c;        step = b;    }    public int transformationA(){          int result=0;  int tmp = t;        for(int i=0;i<8;++i){              result=result*10+tmp%10;              tmp/=10;          }          return result;     }    public int  transformationB(){          int configuration=(t%100000-t%10000)*1000+(t-t%100000)/10+t%1000*10+(t%10000-t%1000)/1000;          return configuration;    }    public int transformationC(){        int result=0;          result=t-t%10000000;          result+=(t%100-t%10)*100000;          result+=(t%10000000-t%1000000)/10;          result+=t%100000-t%1000;          result+=(t%1000000-t%100000)/1000;          result+=(t%1000-t%100)/10;          result+=t%10;          return result;     }}public class msquare {    static void bfs(int target) throws IOException{        LinkedList<square> list = new LinkedList<square>();        list.add(new square(12345678,0,""));        HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>();        hm.put(12345678, 1);        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("msquare.out")));        if(hm.containsKey(target)){            out.println(0);            out.println();            out.close();            return;        }        while(!list.isEmpty()){            square tmp = list.pop();            int t1 = tmp.transformationA();            int t2 = tmp.transformationB();            int t3 = tmp.transformationC();            if(t1==target){//                System.out.println(tmp.step+1);//                System.out.println(tmp.pre+"A");                out.println(tmp.step+1);                out.println(tmp.pre+"A");                out.close();                return;            }            if(t2==target){//                System.out.println(tmp.step+1);//                System.out.println(tmp.pre+"B");                out.println(tmp.step+1);                out.println(tmp.pre+"B");                out.close();                return;            }            if(t3==target){//                System.out.println(tmp.step+1);//                System.out.println(tmp.pre+"C");                out.println(tmp.step+1);                out.println(tmp.pre+"C");                out.close();                return;            }            if(!hm.containsKey(t1)){                list.add(new square(t1,tmp.step+1,tmp.pre+"A"));                hm.put(t1,1);            }            if(!hm.containsKey(t2)){                list.add(new square(t2,tmp.step+1,tmp.pre+"B"));                hm.put(t2,1);            }            if(!hm.containsKey(t3)){                list.add(new square(t3,tmp.step+1,tmp.pre+"C"));                hm.put(t3,1);            }        }    }    public static void main(String[] args) throws Exception {        Scanner scan = new Scanner(new File("msquare.in"));        //Scanner scan = new Scanner(System.in);        String input="";        for(int i=0;i<8;i++){            input+=scan.nextInt();        }        bfs(Integer.parseInt(input));//        square s = new square(12345678,0,"");//        System.out.println(s.transformationA());//        System.out.println(s.transformationB());//        System.out.println(s.transformationC());    }}



这题是直接做吐血了, 类似的BFS在POJ上写也写烂了,今天看到一道马上很happy的来A. 然后就悲剧的交了15次.

思路从第一次就是对的,但是不知道usaco对java的设定是怎么回事,尼玛在test case 2就是测试数据为12345678那里TLE3次!!!

尼玛搜索都没有搜你Y怎么TLE的。剩下的卡在test cast 6上,每次TLE都是2点几秒,3点击秒,什么毛病!

然后我把string操作简单化一些,我知道string操作很花时间,但是这个才8位string,也没有很多操作。

到第12次左右testcase 6 还是2秒左右,最后我基本没改动,我提交想看test case的数据,然后莫名其妙过了,卡着我的test 6居然0.3秒过???


我上网看了所谓的康托展开,貌似也许会比java的垃圾hashmap快。这么一个BFS+判重不要搞这么复杂好不好。

真心不知道usaco怎么判java的。



又交了一次,这次更快了

   Test 1: TEST OK [0.144 secs, 274800 KB]   Test 2: TEST OK [0.281 secs, 274800 KB]   Test 3: TEST OK [0.130 secs, 274800 KB]   Test 4: TEST OK [0.144 secs, 274800 KB]   Test 5: TEST OK [0.266 secs, 274800 KB]   Test 6: TEST OK [0.338 secs, 274800 KB]   Test 7: TEST OK [0.468 secs, 276848 KB]   Test 8: TEST OK [0.634 secs, 280944 KB

很好,同样的代码再次提交

   Test 1: TEST OK [0.130 secs, 274800 KB]   Test 2: TEST OK [0.137 secs, 274800 KB]   Test 3: TEST OK [0.130 secs, 274800 KB]   Test 4: TEST OK [0.173 secs, 274800 KB]   Test 5: TEST OK [0.288 secs, 274800 KB]   Test 6: TEST OK [0.360 secs, 274800 KB]   Test 7: TEST OK [0.454 secs, 275824 KB]
  > Run 8: Execution error: Your program (`msquare') used more than        the allotted runtime of 1 seconds (it ended or was stopped at        2.088 seconds) when presented with test case 8. It used 275824 KB        of memory.         ------ Data for Run 8 [length=16 bytes] ------        4 3 1 2 5 6 7 8         ----------------------------
    Test 8: RUNTIME 2.088>1 (275824 KB)

尼玛时间差距不要太多吧.

这吊题目坑了我一晚上,浪费时间,艹,麻痹的我写信去usaco喷他们.

原创粉丝点击