• poj1726


    黑书上的题目的加强版

    思路差不多。。这届看黑书p117例题4 ”舞蹈家“怀特先生

      1 /*
      2   State:Accepted
      3   Time:2013-03-10 02:44:14
      4 */
      5 #include <iostream>
      6 #include <cstring>
      7 #include <string>
      8 #include <cstdlib>
      9 #include <cstdio>
     10 #include <algorithm>
     11 #include <cmath>
     12 const int num[4][4]=
     13 {{3,5,5,7},
     14  {5,3,7,5},
     15  {5,7,3,5},
     16  {7,5,5,3}};
     17 using namespace std;
     18 int f[150][6][6][5] , lx[150][6][6][5] , ly[150][6][6][5],
     19     lz[150][6][6][5] , a[110];
     20 int n ,ans ,ansx, ansy, ansz;
     21 char s[105]; 
     22 const char  path[3]={'.','L','R'};
     23 
     24 bool left(int x, int y){ return  (x == 1 || x == 2) && y == 0; }
     25 bool right(int x , int y){ return (y == 1 || y == 2) && x == 3;}
     26 
     27 void updata(int i , int x , int y , int now){
     28      if (x == now || (!left(x ,y))){
     29            if (f[i+1][now][y][1] > f[i][x][y][0] + 1){
     30                  f[i+1][now][y][1] = f[i][x][y][0] + 1;          
     31                  lx[i+1][now][y][1] = x;
     32                  ly[i+1][now][y][1] = y;
     33                  lz[i+1][now][y][1] = 0;
     34            }
     35            if (f[i+1][now][y][1] > f[i][x][y][2] + 1){
     36                  f[i+1][now][y][1] = f[i][x][y][2] + 1;          
     37                  lx[i+1][now][y][1] = x;
     38                  ly[i+1][now][y][1] = y;
     39                  lz[i+1][now][y][1] = 2;
     40            }
     41            if (f[i+1][now][y][1] > f[i][x][y][1] + num[x][now]){
     42                  f[i+1][now][y][1] = f[i][x][y][1] + num[x][now];          
     43                  lx[i+1][now][y][1] = x;
     44                  ly[i+1][now][y][1] = y;
     45                  lz[i+1][now][y][1] = 1;
     46            }                 
     47      } 
     48      if (y == now || (!right(x ,y))){
     49            if (f[i+1][x][now][2] > f[i][x][y][0] + 1){
     50                  f[i+1][x][now][2] = f[i][x][y][0] + 1;           
     51                  lx[i+1][x][now][2] = x;
     52                  ly[i+1][x][now][2] = y;
     53                  lz[i+1][x][now][2] = 0;
     54            }
     55            if (f[i+1][x][now][2] > f[i][x][y][1] + 1){
     56                  f[i+1][x][now][2] = f[i][x][y][1] + 1;
     57                  lx[i+1][x][now][2] = x;
     58                  ly[i+1][x][now][2] = y;
     59                  lz[i+1][x][now][2] = 1;
     60            }
     61            if (f[i+1][x][now][2] > f[i][x][y][2] + num[y][now]){
     62                  f[i+1][x][now][2] = f[i][x][y][2] + num[y][now];           
     63                  lx[i+1][x][now][2] = x;
     64                  ly[i+1][x][now][2] = y;
     65                  lz[i+1][x][now][2] = 2;
     66            }                 
     67      }
     68      
     69 }
     70 
     71 void print(int i , int j, int k , int l){
     72       if  (i == 0) return;
     73       int x = lx[i][j][k][l];
     74       int y = ly[i][j][k][l];
     75       int z = lz[i][j][k][l];
     76       print(i - 1, x,y,z);
     77       printf("%c",path[l]);
     78 }
     79 
     80 void solve(){
     81       n = strlen(s);
     82       for (int i = 0; i < n; ++i){
     83            if (s[i] == '.' ) a[i + 1] = 4; 
     84            if (s[i] == 'L' ) a[i + 1] = 0;
     85            if (s[i] == 'U' ) a[i + 1] = 1;
     86            if (s[i] == 'D' ) a[i + 1] = 2;
     87            if (s[i] == 'R' ) a[i + 1] = 3;
     88       }
     89       for (int i = 0; i <= n ; ++i)
     90          for (int j = 0; j <= 4; ++j)
     91             for (int k = 0; k <= 4; ++k)
     92                 for (int l = 0; l <= 2; ++l)
     93                     f[i][j][k][l] = 100000000;
     94 
     95       f[0][0][3][0] = 0;
     96       for (int i = 0; i < n ; ++i)
     97          for (int j = 0; j < 4; ++j)
     98             for (int k = 0; k < 4; ++k)
     99                  if (j != k)
    100                     if (a[i + 1] != 4) updata(i , j, k , a[i+1]);
    101                     else {
    102                             for (int l = 0 ; l < 4; ++l) updata(i , j , k , l);
    103                             for (int l = 0; l <= 2; ++l)
    104                                 if (f[i+1][j][k][0] > f[i][j][k][l]){
    105                                    f[i+1][j][k][0] = f[i][j][k][l];
    106                                    lx[i+1][j][k][0] = j;
    107                                    ly[i+1][j][k][0] = k;
    108                                    lz[i+1][j][k][0] = l;
    109                             }
    110                          }
    111       ans = 100000000;
    112       for (int i = 0; i < 4; ++i)
    113           for (int j = 0; j < 4; ++j)
    114              for (int k = 0; k < 3; ++k)
    115                if (i != j && f[n][i][j][k] < ans){
    116                       ans = f[n][i][j][k];
    117                       ansx = i;
    118                       ansy = j;
    119                       ansz = k;
    120                }
    121       print(n, ansx ,ansy,ansz);
    122       printf("\n");
    123       
    124 
    125 }
    126 
    127 int main(){
    128      freopen("poj1726.in","r", stdin);
    129      freopen("poj1726.out","w",stdout);
    130      scanf("%s",&s);
    131      while (s[0] !='#'){
    132           solve();
    133           scanf("%s",&s);
    134      }
    135      
    136 }
  • 相关阅读:
    20165229预备作业三
    20165529学习基础和C语言基础调查
    20165229我所期待的师生关系
    20165210 Java第七周学习总结
    20165210 Java第六周学习总结
    20165210 Java第一次实验报告
    20165210 Java第五周学习总结
    20165210 Java第四周学习总结
    20165210 Java第三周学习总结
    20165210 Java第二周学习总结
  • 原文地址:https://www.cnblogs.com/yzcstc/p/2977888.html
Copyright © 2020-2023  润新知