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; } }
  • 相关阅读:
    Linux防火墙使用配置
    es安装笔记
    git仓库免密码登陆配置
    swgger前后端分离api生成
    关于redis
    学习笔记关于springboot
    idea 安装记录
    随记
    开课吧--Python数据分析--第4节 数据七十二变--互动练习:如果你不需要,就让它消失!
    jupyter使用方法
  • 原文地址:https://www.cnblogs.com/wang3/p/3143052.html
Copyright © 2011-2022 走看看