CF1301D-Time to Run s 构造 思维

一定有一种走法可以把图中的所有边都遍历一遍。

怎么走,我们想办法把每一行都用一样的走法。

先直接往右走,然后往左走,然后往下,以此类推为基本想法。

那么除了左右两边,其他的竖向边都无法被顾及到怎么办?先整体横着走,再整体竖着走?发现不行。

所以我们往左走变成,下,上,左。这样子边向左走边把所有竖向边经过。最后一行比较特殊,直接左走就行。

然后这样子最后回到最后一行的开端时,还有一条竖着指向左上的路径,没走过,走一下就行了。

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 using namespace std;
  5 int n,m,k,tot,ans_cnt,ans_sum[3100];
  6 char ans[3100][5];
  7 int main()
  8 {
  9     scanf("%d%d%d",&n,&m,&k);
 10     if (k > 4 * n * m - 2 * n - 2 * m)
 11     {
 12         printf("NO
");
 13         return 0;
 14     }
 15     int tcnt;
 16     for (int i = 1;i <= n;i++)
 17     {
 18         //R
 19         tcnt = min(m - 1,k);
 20         k -= tcnt;
 21         if (tcnt / 4)
 22         { 
 23             ans_cnt++;
 24             ans_sum[ans_cnt] = tcnt / 4;
 25             for (int o = 1;o <= 4;o++)
 26                 ans[ans_cnt][o] = 'R';        
 27         }
 28         if (tcnt % 4)
 29         {
 30             ans_cnt++;
 31             ans_sum[ans_cnt] = 1;
 32             for (int o = 1;o <= tcnt % 4;o++)
 33                 ans[ans_cnt][o] = 'R';
 34         }
 35         if (k == 0)
 36             break;
 37         //L
 38         if (i != n)
 39         {
 40             tcnt = min((m - 1) * 3,k);
 41             k -= tcnt;
 42             if (tcnt / 3)
 43             {
 44                 ans_cnt++;
 45                 ans_sum[ans_cnt] = tcnt / 3;
 46                 ans[ans_cnt][1] = 'D';
 47                 ans[ans_cnt][2] = 'U';
 48                 ans[ans_cnt][3] = 'L';
 49             }
 50             if (tcnt % 3)
 51             {
 52                 ans_cnt++;
 53                 ans_sum[ans_cnt] = 1;
 54                 ans[ans_cnt][1] = 'D';
 55                 if (tcnt % 3 >= 2)
 56                     ans[ans_cnt][2] = 'U'; 
 57             }
 58             if (k == 0)
 59                 break;
 60         }else
 61         {
 62             tcnt = min(m - 1,k);
 63             k -= tcnt;
 64             if (tcnt / 4)
 65             {
 66                 ans_cnt++;
 67                 ans_sum[ans_cnt] = tcnt / 4;
 68                 for (int o = 1;o <= 4;o++)
 69                     ans[ans_cnt][o] = 'L';        
 70             }
 71             if (tcnt % 4)
 72             {
 73                 ans_cnt++;
 74                 ans_sum[ans_cnt] = 1;
 75                 for (int o = 1;o <= tcnt % 4;o++)
 76                     ans[ans_cnt][o] = 'L';
 77             }
 78             if (k == 0)
 79                 break;
 80         }
 81         //D/U
 82         if (i != n)
 83         {
 84             tcnt = 1;
 85             k -= tcnt;
 86             ans_cnt++;
 87             ans_sum[ans_cnt] = 1;
 88             ans[ans_cnt][1] = 'D';        
 89             if (k == 0)
 90                 break;
 91         }else
 92         {
 93             tcnt = min(n - 1,k);
 94             k -= tcnt;
 95             if (tcnt / 4)
 96             {
 97                 ans_cnt++;
 98                 ans_sum[ans_cnt] = tcnt / 4;
 99                 for (int o = 1;o <= 4;o++)
100                     ans[ans_cnt][o] = 'U';        
101             }
102             if (tcnt % 4)
103             {
104                 ans_cnt++;
105                 ans_sum[ans_cnt] = 1;
106                 for (int o = 1;o <= tcnt % 4;o++)
107                     ans[ans_cnt][o] = 'U';
108             }
109             if (k == 0)
110                 break;
111         }
112     }
113     printf("YES
%d
",ans_cnt);
114     for (int i = 1;i <= ans_cnt;i++)
115         printf("%d %s
",ans_sum[i],ans[i] + 1);
116     return 0;
117 }
原文地址:https://www.cnblogs.com/iat14/p/12315688.html