problem

链接:https://ac.nowcoder.com/acm/contest/10272/E
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
Special Judge, 64bit IO Format: %lld
题目描述
A robot is standing on an infinite 2-dimensional plane. Programmed with a string s_1s_2\cdots s_ns
1

s
2

⋯s
n

of length nn, where s_i \in {\text{‘U’}, \text{‘D’}, \text{‘L’}, \text{‘R’}}s
i

∈{’U’,’D’,’L’,’R’}, the robot will start moving from (0, 0)(0,0) and will follow the instructions represented by the characters in the string.

More formally, let (x, y)(x,y) be the current coordinate of the robot. Starting from (0, 0)(0,0), the robot repeats the following procedure nn times. During the ii-th time:
If s_i = \text{‘U’}s
i

=’U’ the robot moves from (x, y)(x,y) to (x, y+1)(x,y+1);
If s_i = \text{‘D’}s
i

=’D’ the robot moves from (x, y)(x,y) to (x, y-1)(x,y−1);
If s_i = \text{‘L’}s
i

=’L’ the robot moves from (x, y)(x,y) to (x-1, y)(x−1,y);
If s_i = \text{‘R’}s
i

=’R’ the robot moves from (x, y)(x,y) to (x+1, y)(x+1,y).

However, there is a mine buried under the coordinate (m_x, m_y)(m
x

,m
y

). If the robot steps onto (m_x, m_y)(m
x

,m
y

) during its movement, it will be blown up into pieces. Poor robot!

Your task is to rearrange the characters in the string in any order, so that the robot will not step onto (m_x, m_y)(m
x

,m
y

).
输入描述:
There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first line contains two integers m_xm
x

and m_ym
y

(-10^9 \le m_x, m_y \le 10^9−10
9
≤m
x

,m
y

≤10
9
) indicating the coordinate of the mine.

The second line contains a string s_1s_2\cdots s_ns
1

s
2

⋯s
n

of length nn (1 \le n \le 10^51≤n≤10
5
, s_i \in {\text{‘U’}, \text{‘D’}, \text{‘L’}, \text{‘R’}}s
i

∈{’U’,’D’,’L’,’R’}) indicating the string programmed into the robot.

It’s guaranteed that the sum of nn of all test cases will not exceed 10^610
6
.
输出描述:
For each test case output one line. If a valid answer exists print the rearranged string, otherwise print “Impossible” (without quotes) instead. If there are multiple valid answers you can print any of them.
示例1
输入
复制
5
1 1
RURULLD
0 5
UUU
0 3
UUU
0 2
UUU
0 0
UUU
输出
复制
LDLRUUR
UUU
Impossible
Impossible
Impossible

solution

题意:

  • 给出一个字符串,UDLR分别表示向上下左右移动。默认在坐标原点(0,0),求更改字符顺序,让其移动不经过题目地雷点(mx,my)。

思路:

  • 可以证明存在一种答案,使得相同的方向是连续排在一起的,即枚举UDLR的24种排列即可。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const char a[]="UDLR";//用0123对应ULDR,a是字符,c是数量,q是排列。
const int dx[]={0,0,-1,1},dy[]={1,-1,0,0};
int main(){//ios::sync_with_stdio(false); WAint T;  cin>>T;while(T--){int mx, my;  cin>>mx>>my;string s;  cin>>s;int c[4] = {};for(int i=0;i<s.size();i++){if(s[i]=='U')c[0]++;if(s[i]=='D')c[1]++;if(s[i]=='L')c[2]++;if(s[i]=='R')c[3]++;}//for(int i =0; i<4;i++)cout<<c[i]<<"\n";//起点或终点重合int tx=c[3]-c[2],ty=c[0]-c[1];if(tx==mx&&ty==my || mx==0&&my==0){cout<<"Impossible\n";continue;}int q[4]={0,1,2,3}, ook=0;do{int x=0,y=0,ok=1;for(int i=0; i<=3; i++){//方向for(int j=0;j<c[q[i]];j++){//移动(方向q[i]有长度c[q[i]])x += dx[q[i]], y+=dy[q[i]];if(x==mx&&y==my){ok=0; break;}}if(ok==0)break;}if(ok==1){//没有雷就全方向输出for(int i=0; i<=3; i++)for(int j=0; j<c[q[i]];j++)putchar(a[q[i]]);cout<<"\n";ook=1;break;}}while(next_permutation(q,q+4));if(ook)continue;cout<<"Impossible\n";}return 0;
}

第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(南京)签到题E Evil Coordinate相关推荐

  1. 第45届国际大学生程序设计竞赛(ICPC)银川站太原理工大学收获4枚奖牌

    第45届国际大学生程序设计竞赛(ICPC)银川站,由宁夏理工学院承办,于2021年5月15-16日在宁夏的石嘴山市进行. 太原理工大学在比赛中获得2银2铜共4枚奖牌的好成绩. 参加本次比赛的四个队,涵 ...

  2. 第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(济南)(重现赛)

    第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(济南)(重现赛) 导语 涉及的知识点 题目 C D G J M 参考文献 导语 日常的队内集训,开始的时候状态其实很好,但是到了后两题就出现了 ...

  3. 第45届国际大学生程序设计竞赛(ICPC)沈阳站太原理工大学收获1枚铜牌

    第45届ICPC沈阳区域赛,于2021年7月18日在东北大学南湖校区举行.太原理工大学2个队参加比赛,由20级中学没有学过编程的3名同学组成的队,首次参加现场赛并获得铜奖.

  4. 第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(南京)签到题K Co-prime Permutation,L Let‘s Play Curling

    序 emmm因为没时间补题(虽然签到有四题),所以只能先放两个签到. 这是比赛链接:https://ac.nowcoder.com/acm/contest/10272 这是题解链接:2020年ICPC ...

  5. 第 45 届国际大学生程序设计竞赛(ICPC)亚洲网上区域赛模拟赛 题解(除了C、G之后补)

    整理的算法模板合集: ACM模板 这次比赛好多原题呀-(就是那种稍微拓展了一点的原题) 目录 A.Easy Equation B.XTL's Chessboard D.Pokemon Ultra Su ...

  6. 第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(昆明),签到题HIL

    H. Hard Calculation 链接:https://ac.nowcoder.com/acm/contest/12548/H 来源:牛客网 题目描述 Hooray! It is the fir ...

  7. 第 45 届国际大学生程序设计竞赛(ICPC)亚洲网上区域赛模拟赛 B.XTL‘s Chessboard(思维)

    题目链接:https://ac.nowcoder.com/acm/contest/8688/B 题目描述 Xutianli is a perfectionist, who only owns &quo ...

  8. 第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(昆明),签到题4题

    文章目录 H. Hard Calculation I. Mr. Main and Windmills L. Simone and graph coloring J.Parallel Sort 补题链接 ...

  9. 【第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛】Simone and Graph Coloring

    #include <bits/stdc++.h> using namespace std; const int maxn = 1000005, INF = 0x7f7f7f7f; int ...

  10. 第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(昆明) AC(带悔贪心)

    下面两个题都是选择iii有个价值如果选择iii则不能选择i−1,i+1i-1,i+1i−1,i+1,让价值最大或最小 P1792 [国家集训队]种树 野心qwq 的博客 #include<bit ...

最新文章

  1. 通过钉钉群聊机器人推送zabbix告警
  2. 【纯干货】Amoeba实现MySQL主从同步与读写分离
  3. 由浅入深CIL系列:5.抛砖引玉:判断string是否为空的四种方法的CIL代码看看效率如何?...
  4. CodeIgniter辅助函数
  5. 7002.ubuntu18.04将软件图标固定到工具栏
  6. mdb 查询过于复杂_【律联云知产课堂】南京商标查询主要从哪些方面判断一个商标是否适合注册?...
  7. 索引sql server_SQL Server索引设计基础和准则
  8. KDD20 | 图模型的信息融合专题
  9. 洛谷 P1560 [USACO5.2]蜗牛的旅行Snail Trails(不明原因的scanf错误)
  10. HDU 6321(状压dp)
  11. jquery 取的单选按钮组的值
  12. 使用 PyQT5 来做一个简易版库存管理系统(二)
  13. java 排序返回索引_java数组排序和索引
  14. Spring(一) 构建简单Web应用
  15. NBOJ 1184 Elaine's Queue deque的运用
  16. 植物大战僵尸:你还在因为玩不过僵尸而气到吃电脑吗?不要怕,教你修改用户存档,逆天翻盘,吊打僵尸**
  17. JS打包工具rollup——完全入门指南
  18. Matplotlib空气质量数据分析(附数据集下载)
  19. 产品经理的思维模型大全(建议收藏)
  20. 破解手机辐射危害健康的流言

热门文章

  1. 谷歌浏览器(chrome) —— 扩展应用程序
  2. CentOs虚拟机NAT模式下静态IP的配置
  3. 数据集可视化——tile(贴砖)
  4. 量化感知训练_一文速览EMNLP 2020中的Transformer量化论文
  5. python入门指南-python3.6.0入门指南(官方版).pdf
  6. python画条形图-python3使用matplotlib绘制条形图
  7. python对于设计师有什么用-《学习PYTHON—做个有编程能力的设计师》
  8. python能做什么毕业设计-毕业设计涉及到python?看我用Python优雅的写论文!
  9. 语音识别十年来发展的历程
  10. 苹果版App开发心得