zoukankan      html  css  js  c++  java
  • Top Coder SRM 582 DIV2 1000

    没有想到好的算法,用回溯法给爆破了。。。

    public
    class ColorTheCells{ private int min_time; public int minimalTime(int[] dryingTime){ int len = dryingTime.length; int[] dryThen = new int[len]; int i; for(i=0;i<len;i++) dryThen[i] = 0; min_time = 0x7fffffff; for(i=0;i<len;i++) paint(dryThen,dryingTime,0,i,0); return min_time; } private void paint(int[] dryThen,int[] dryTime,int cur_cell,int next_cell,int cur_time){ //1.move to next_cell //2.paint //3.check time,then paint next or return //4.unpaint int len = dryThen.length; //step 1 while(cur_cell>next_cell+1){ //move left //wait to move if(dryThen[cur_cell - 1]>cur_time) cur_time = dryThen[cur_cell-1]; //move cur_time++; cur_cell--; } while(cur_cell<next_cell-1){ //move right //wait to move if(dryThen[cur_cell+1]>cur_time) cur_time = dryThen[cur_cell+1]; //move cur_time++; cur_cell++; } if(cur_cell==next_cell){ //move left or right if(cur_cell>0&&cur_cell<len-1){ if(dryThen[cur_cell-1]<dryThen[cur_cell+1]){ //wait left if(dryThen[cur_cell-1]>cur_time) cur_time = dryThen[cur_cell-1]; //move left cur_time++; cur_cell--; } else{ //wait right if(dryThen[cur_cell+1]>cur_time) cur_time = dryThen[cur_cell+1]; //move right cur_time++; cur_cell++; } } else if(cur_cell>0){ //wait left if(dryThen[cur_cell-1] > cur_time) cur_time = dryThen[cur_cell-1]; //move left cur_time++; cur_cell--; } else if(cur_cell<len-1){ //wait right if(dryThen[cur_cell+1]>cur_time) cur_time = dryThen[cur_cell+1]; //move right cur_time++; cur_cell++; } } //step 2 cur_time++; // System.out.println("paint"+next_cell+",time="+cur_time); dryThen[next_cell]=cur_time+dryTime[next_cell]; //step 3 if(cur_time>=min_time){ } else{ //paint all the unpainted cell int i,count=0; for(i=0;i<dryThen.length;i++){ //System.out.println(i); if(dryThen[i]==0){ paint(dryThen,dryTime,cur_cell,i,cur_time); count++; } } //log the new min_time if(count==0&&cur_time<min_time){ // System.out.println(cur_time); min_time = cur_time; } } //step 4 dryThen[next_cell]=0; } }
  • 相关阅读:
    1062 Talent and Virtue (25 分)
    1083 List Grades (25 分)
    1149 Dangerous Goods Packaging (25 分)
    1121 Damn Single (25 分)
    1120 Friend Numbers (20 分)
    1084 Broken Keyboard (20 分)
    1092 To Buy or Not to Buy (20 分)
    数组与链表
    二叉树
    时间复杂度与空间复杂度
  • 原文地址:https://www.cnblogs.com/wang3/p/3143052.html
Copyright © 2011-2022 走看看