posts - 403, comments - 310, trackbacks - 0, articles - 7
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          USACO 1.1.4 The Clocks

          Posted on 2007-05-30 21:34 ZelluX 閱讀(764) 評論(0)  編輯  收藏 所屬分類: Algorithm

          本來想練習了下deque的使用。用BFS,每個結點都記錄了到目前為止轉動的情況。結果發現內存消耗很大,就只好用DFS了。


          /*
          PROG: clocks
          ID: 06301031
          LANG: C++
          */


          #include 
          <iostream>
          #include 
          <fstream>
          #include 
          <deque>
          #include 
          <vector>

          using namespace std;

          // A B C D E F G H I
          // 0 1 2 3 4 5 6 7 8
          const int moves[9][5= {{0,1,3,4,-1}{0,1,2,-1,-1}{1,2,4,5,-1}{0,3,6,-1,-1},
                                   
          {1,3,4,5,7},  {2,5,8,-1,-1}{3,4,6,7,-1}{6,7,8,-1,-1}{4,5,7,8,-1}}
          ;

          static int minMoves = 50;
          static int bestSolution[9];

          void DFS(int status[], int method[], int t, int count) {
              
          int i, j;
              
              
          if (count > minMoves) {
                  
          return;
              }

              
          bool flag = true;
              
          for (i = 0; i < 9; i++{
                  
          if (status[i] != 12{
                      flag 
          = false;
                      
          break;
                  }

              }

              
          if (flag) {
                  
          if (count == minMoves) {
                      
          for (i = 0; i < 9; i++{
                          
          if (bestSolution[i] > method[i]) {
                              
          return;
                          }

                          
          if (bestSolution[i] < method[i]) {
                              
          break;
                          }

                      }

                  }

                  minMoves 
          = count;
                  
          for (int i = 0; i < 9; i++{
                      bestSolution[i] 
          = method[i];
                  }

                  
          return;
              }

              
              
          if (t >= 9{
                  
          return;
              }

              
              
          for (i = 0; i < 4; i++{
                  method[t] 
          = i;
                  
          if (i == 0{
                      DFS(status, method, t 
          + 1, count);
                      
          continue;
                  }

                  
                  
          // i != 0
                  for (j = 0; j < 5; j++{
                      
          if (moves[t][j] < 0{
                          
          break;
                      }

                      status[moves[t][j]] 
          += 3;
                      
          if (status[moves[t][j]] > 12{
                          status[moves[t][j]] 
          = 3;
                      }

                  }

                  DFS(status, method, t 
          + 1, count + i);
              }

              
              
          // Turn the status again, to change it back.
              for (j = 0; j < 5; j++{
                  
          if (moves[t][j] < 0{
                      
          break;
                  }

                  status[moves[t][j]] 
          += 3;
                  
          if (status[moves[t][j]] > 12{
                      status[moves[t][j]] 
          = 3;
                  }

              }

              method[t] 
          = 0;
          }


          int main() {
              ifstream fin(
          "clocks.in");
              ofstream fout(
          "clocks.out");
              
          int start[9], method[9];
              
          int i, j;
              
          for (i = 0; i < 9; i++{
                  fin 
          >> start[i];
              }

              
          for (i = 0; i < 9; i++{
                  method[i] 
          = 0;
              }

              
          for (i = 0; i < 9; i++{
                  bestSolution[i] 
          = 4;
              }

              
              DFS(start, method, 
          00);
              
              vector
          <int> answer;
              
          for (i = 0; i < 9; i++{
                  
          if (bestSolution[i] > 0{
                      
          for (j = 0; j < bestSolution[i]; j++{
                          answer.push_back(i 
          + 1);
                      }

                  }

              }

              fout 
          << answer[0];
              
          for (i = 1; i < answer.size(); i++{
                  fout 
          << " " << answer[i];
              }

              fout 
          << endl;
              fout.close();
              
              
          return 0;
          }

          主站蜘蛛池模板: 泗洪县| 星子县| 县级市| 保山市| 松阳县| 湘西| 钟祥市| 万宁市| 清镇市| 高台县| 镇坪县| 纳雍县| 禹城市| 望江县| 德惠市| 贡嘎县| 桐城市| 射洪县| 青冈县| 赞皇县| 潜江市| 鄂尔多斯市| 长子县| 新乡市| 肥乡县| 德州市| 鹤庆县| 朔州市| 驻马店市| 阿图什市| 开化县| 开阳县| 边坝县| 高台县| 衡水市| 阜平县| 固阳县| 彩票| 新沂市| 西乌珠穆沁旗| 博白县|