2019 GUDT WPTC 1 Div2 Problem E(题解) codeforces 115B

原题

题目大意

题目背景是割草,给一个图,然后给出图中草的坐标的,然后要把这些草全部割掉,工作员从图中的(1,1)坐标出发,刚开始面向右边,每次只能进行两个操作 (1)往面向的方向走一格(2)往下走一格并且转向.问工作员把草全部割掉至少走几步?

题目分析

有题可知工作员的行进方向,第一行朝右,第二行朝左,第三行又朝右.因此只要想明白题目还是很好解的,具体过程我会在代码里注释.

代码

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <iostream>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <string>
 8 #include <utility>
 9 #include <queue>
10 #include <stack>
11 const int INF=0x3f3f3f3f;
12 using namespace std;
13
14 char map[200][200];
15 int a[200]; //用来存同一行的草
16
17 int main()
18 {
19     int n,m;
20     cin>>n>>m;
21     for(int i=0;i<n;i++)
22     for(int j=0;j<m;j++)
23     cin>>map[i][j];
24     int k=0,temp=0,ans=0,vir=0; //k是行数,temp是记录起始位置,vir用来记录往下走了多少格
25     while(k!=n)
26     {
27         int cnt=0; //cnt用来记录同一行的草的数量
28         if(k&1) //奇数行工作员向左(k从0开始)
29         {
30             for(int i=m-1;i>=0;i--)
31             if(map[k][i]==‘W‘) a[cnt++]=i;
32             if(cnt) //这一行有草
33             {
34                 //a[0]是该行最右边的草,a[cnt-1]是该行最左边的草
35                 if(a[0]>temp) ans=ans+a[0]-temp+a[0]-a[cnt-1];
36                 else ans=ans+temp-a[cnt-1];
37                 vir=k;
38             }
39         }
40         else
41         {
42             for(int i=0;i<m;i++)
43             if(map[k][i]==‘W‘) a[cnt++]=i;
44
45             if(cnt)
46             {
47                 //a[cnt-1]是该行最右边的草,a[0]是该行最左边的草
48                 if(a[0]<temp) ans=ans+temp-a[0]+a[cnt-1]-a[0];
49                 else ans=ans+a[cnt-1]-temp;
50                 vir=k;
51             }
52         }
53         /* 测试代码可以无视
54         for(int i=0;i<cnt;i++)
55         printf("a[%d]=%d ",i+1,a[i]);
56         cout<<endl;
57         printf("ans=%d\nvir=%d\n",ans,vir);
58         */
59         if(cnt) temp=a[cnt-1];  //这里注意一下
60         k++;
61     }
62     cout<<ans+vir<<endl;
63     return 0;
64 }

原文地址:https://www.cnblogs.com/VBEL/p/10440963.html

时间: 2024-08-30 14:58:45

2019 GUDT WPTC 1 Div2 Problem E(题解) codeforces 115B的相关文章

programming-challenges Shoemaker&amp;#39;s Problem (110405) 题解

Greedy. 证明: Let's say we have job 1, 2, ..., n, and they have time and fine as t1, f1, t2, f2, ..., tn, fn and they are in the order of t1/f1 <= t2/f2 <= t3/f3 <= ... <= tn/fn So this is the objective schedule. Now we change 1 with m (1 < m

programming-challenges Shoemaker&#39;s Problem (110405) 题解

Greedy. 证明: Let's say we have job 1, 2, ..., n, and they have time and fine as t1, f1, t2, f2, ..., tn, fn and they are in the order of t1/f1 <= t2/f2 <= t3/f3 <= ... <= tn/fn So this is the objective schedule. Now we change 1 with m (1 < m

2019 湖南多校第五场题解

解题过程 开场byf去修锅,各种spj没写,还有写挂的.开场shl写A,lfw写K,shl写E,shl写G,前期签到顺利.随后lfw推出D的规律,开始写,遇到了一些错误,首先MLE了,因为k^2预处理,lfw用的string来存不同的子串丢到unordered_map里,那么就是k^3的存储,肯定炸,接着换成了hash值,但是单关键字冲突了,WA了一发,接着用双关键字,使用一个pair存map,map带的logn常数MLE了一发,中间B题加上了spj,lfw习惯性输出空格,WA一发后过了,最后l

2019 ICPC南昌邀请赛比赛过程及题解

解题过程 中午吃饭比较晚,到机房lfw开始发各队的账号密码,byf开始读D题,shl电脑卡的要死,启动中...然后听到谁说A题过了好多,然后shl让blf读A题,A题blf一下就A了.然后lfw读完M题(shl的电脑终于打开了,然后输入密码,密码错误...自闭),说AC 自动机板题,然后找板子,,,突然发现自己读错题目.后来不知道怎么A的.shl copy了一遍密码终于登上账号.然后lfw一个人用单调栈A掉了I题:byf 秒了H题: shl和byf读j题,读完吧题意告诉lfw,lfw说水题,然后

Light OJ 1004 - Monkey Banana Problem dp题解

1004 - Monkey Banana Problem PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You are in the world of mathematics to solve the great "Monkey Banana Problem". It states that, a monkey enters into a diamond shaped two dim

Lintcode1 A+B Problem solution 题解

[题目描述] Write a function that add two numbers A and B. You should not use + or any arithmetic operators. Notice:There is no need to read data from standard input stream. Both parameters are given in function aplusb, you job is to calculate the sum and

2019 浙大校赛解题过程及题解

解题过程  开场lfw看J,被孪生素数误导,没有看出水题的本质,byf及时提醒后,lfw忘记N=1的情况要特判.之后byf写E,忘记开long long,wa+1.Shl看出A题思路,告诉lfw开始写A,lfw忘记先排序,WA+1.byf看出B题做法,lfw写java过掉,shl与lfw讨论G,lfw开始写G,byf看出C可以hash暴力,lfwG忘记清空一个值,导致特殊情况会加两次,wa+1,byf使用mp作hash,带logn常数TLE,随后转成long long的hash值,使用unord

[题解][Codeforces]Good Bye 2019 简要题解

构造题好评,虽然这把崩了 原题解 A 题意 二人游戏,一个人有 \(k_1\) 张牌,另一个人 \(k_2\) 张,满足 \(2\le k_1+k_2=n\le 100\),每张牌上有一个数,保证所有的牌上的数互不相同且在 \([1,n]\) 内 每回合双方都会出一张牌,牌上数小的一方的牌会给牌上数大的一方 拿到所有 \(n\) 张牌的一方赢得比赛 求两人都采取最优策略的情况下谁会赢 多组数据,数据组数 \(t\le100\) 做法:贪心 设 \(a\) 和 \(b\) 为双方的最大数 显然如果

#417 Div2 Problem C Sagheer and Nubian Market (二分 &amp;&amp; std::accumulate)

题意 : 给你 n 件物品和你拥有的钱 S, 接下来给出这 n 件物品的价格, 这些物品的价值不是固定不变的, 价格的变化公式是 a[i]+k*i (i代表第 i 件物品, k 代表你选择买的物品数量, a[i]为物品的底价), 现问你最多能够买多少件物品和所买物品总和, 输出时应该使得所买物品总和尽量小 分析 : 如果我当前能买 k 件物品, 那我肯定能买数量小于 k 的物品, 如果我当前买不起 k 件物品, 那我肯定也不能买比 k 件要多的物品.所以可以考虑二分解法, 在1~n之间二分查找