Codeforces Round #504 E. Down or Right

题目描述:交互题。 有一个\(n \times n\)的方阵,有一些格子是障碍,从\((1, 1)\)出发,只能向右向下走,能走到\((n, n)\),你有\(4n\)次询问,每次询问\((r_1, c_1)\)能否走到\((r_2, c_2)\),但这两个点的曼哈顿距离要大于\(n-1\),最后输出一条从\((1, 1)\)到\((n, n)\)的路径。

solution

从\((1, 1)\)出发,优先向下走,向下走能到\((n, n)\)就向下走,走到对角线。然后从\((n, n)\)出发,优先向左走,\((1, 1)\)能到左边的点就向左走,走到对角线,这样构造能保证最终在对角线的点一定重合。

因为优先向下走能保证\(D_1-R_1\)最大,那后面一半的\(R_2-D_2=(n-1-R_1)-(n-1-D_1)=D_1-R_1\)最大,而优先向左走正是保证\(R_2-D_2\)最大,因此对角线的点一定重合。

时间复杂度:\(O(2n)\)

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n;
  4. vector<char> ans;
  5. char st[10];
  6. bool ask(int r1, int c1, int r2, int c2)
  7. {
  8. printf("? %d %d %d %d\n", r1, c1, r2, c2);
  9. fflush(stdout);
  10. scanf("%s", st);
  11. return st[0]=='Y';
  12. }
  13. void solve()
  14. {
  15. scanf("%d", &n);
  16. int x=1, y=1;
  17. for (int i=1; i<=n-1; ++i)
  18. if (ask(x+1, y, n, n)) x++, ans.push_back('D');
  19. else y++, ans.push_back('R');
  20. x=n, y=n;
  21. for (int i=1; i<=n-1; ++i)
  22. if (ask(1, 1, x, y-1)) y--, ans.push_back('R');
  23. else x--, ans.push_back('D');
  24. printf("! ");
  25. for (int i=0; i<n-1; ++i) putchar(ans[i]);
  26. for (int i=n*2-2-1; i>=n-1; --i) putchar(ans[i]);
  27. puts("");
  28. fflush(stdout);
  29. }
  30. int main()
  31. {
  32. solve();
  33. return 0;
  34. }

Codeforces Round #504 E. Down or Right的更多相关文章

  1. Codeforces Round #504 D. Array Restoration

    Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...

  2. Codeforces 1023 A.Single Wildcard Pattern Matching-匹配字符 (Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Fi)

    Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A. Single Wildcard Patter ...

  3. E - Down or Right Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)

    http://codeforces.com/contest/1023/problem/E 交互题 #include <cstdio> #include <cstdlib> #i ...

  4. Codeforces Round #504:D. Array Restoration

    D. Array Restoration 题目链接:https://codeforces.com/contest/1023/problem/D 题意: 给出一个序列,现在要求对一个全为0的序列执行q次 ...

  5. Codeforces Round 504

    (交互题真神奇,,,我自己瞎写了一发目测样例都没过去就AC了...) (只出了两题的竟然没掉下蓝名真是可怕) A:我的代码太不美观了,放个同学的(因为我是c++63分的蒟蒻所以根本不知道那些函数怎么用 ...

  6. Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-D- Array Restoration

    我们知道不满足的肯定是两边大中间小的,这样就用RMQ查询两个相同等值的区间内部最小值即可,注意边界条件 #include<bits/stdc++.h> #define x first #d ...

  7. Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-C-Bracket Subsequence

    #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...

  8. Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-A-Single Wildcard Pattern Matching

    #include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> ...

  9. Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) E. Down or Right

    从(1,1,n,n)每次只变一个坐标,进行询问. 如果问到对角线有距离限制, 再从(1,1,n/2,n/2)询问到(n/2,n/2,n,n) 记住前半部分贪心忘上走,后本部分贪心往右走 因为最后的路线 ...

随机推荐

  1. 51nod 1293 球与切换器 | DP

    51nod 1293 球与切换器 | DP 题面 有N行M列的正方形盒子.每个盒子有三种状态0, -1, +1.球从盒子上边或左边进入盒子,从下边或右边离开盒子.规则: 如果盒子的模式是-1,则进入它 ...

  2. XML外部实体(XXE)注入详解

    ###XML与xxe注入基础知识 1.XMl定义 XML由3个部分构成,它们分别是:文档类型定义(Document Type Definition,DTD),即XML的布局语言:可扩展的样式语言(Ex ...

  3. 单点登录(十八)----cas4.2.x客户端增加权限控制shiro

    我们在上面章节已经完成了cas4.2.x登录启用mongodb的验证方式. 单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方式完整流程 也完成了获取管理员身份属性 ...

  4. 最新版的Android4.4.2 SDK无法下载解决

    http://hi.baidu.com/petercao2008/item/65362d2bdbddfacba5275a50 问题: Downloading ARM EABI v7a System I ...

  5. print显示特定的数据格式

    原文网址 1.查看数据print  variable        查看变量print  *array@len      查看数组(array是数组指针,len是需要数据长度)可以通过添加参数来设置输 ...

  6. md5sum/opensll md5

    http://m.blog.csdn.net/article/details?id=42041329 MD5算法常常被用来验证网络文件传输的完整性,防止文件被人篡改.MD5全称是报文摘要算法(Mess ...

  7. Python高手之路【四】python函数装饰器,迭代器

    def outer(func): def inner(): print('hello') print('hello') print('hello') r = func() print('end') p ...

  8. python中高阶函数与装饰器(2)

    函数返回值为内置函数名: def sum(*args):    def sum_in():        ax = 0        for n in args:            ax = ax ...

  9. 用canvas绘制验证码

    在通常的登录界面我们都可以看到验证码,验证码的作用是检测是不是人在操作,防止机器等非人操作,防止数据库被轻而易举的攻破. 验证码一般用PHP和java等后端语言编写: 但是在前端,用canva或者SV ...

  10. 2017 清北济南考前刷题Day 6 afternoon

    期望得分:100+100+30=230 实际得分: 正解: 枚举最高的位,这一位m是1但实际用了0 然后剩余的低位肯定是 正数就用1,负数用0 考场思路:数位DP #include<cstdio ...