是一道很有趣的题目

给出地图和起始点 与要求的步数k 要求走k步 正好回到起始点 输出字典序最小的移动路径 DLRU这样

可以想到DU LR都是相同数量的 于是k必须是一个偶数

然后走了弯路QAQ 从这个地方入手 可以想到贪心按顺序DLRU去四个方向挨个跑 跑k/2次 然后原路返回

然而因为走了k/2步之后 可能接着DLRU走回去 所以原路返回可能不是最优的 但是k是1e6 肯定不能dfs

后来发现可以bfs处理一下起始点到每个点的最短距离 代表从这个点走到起始点最少多少步

然后每一步都去贪心的走 但是要看剩余步数能不能回去

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<map>
#include<iostream>
#include<string>
#include<vector>
#include<queue>
using namespace std;
#define L long long char ans[1000050] ;
int n , m , q ;
char s[1050][1050] ; char z[4] = {'D' , 'L' , 'R' , 'U'} ;
int dx[4] = {1,0,0,-1} ;
int dy[4] = {0,-1,1,0} ; int dis[1050][1050] ; void bfs(int sx , int sy){
dis[sx][sy]=0;
queue<pair<int ,int > > que;
que.push(make_pair(sx,sy) ) ;
while(!que.empty()) {
pair<int ,int > u = que.front() ; que.pop() ;
for(int i = 0 ; i < 4 ; i ++ ) {
int nx = u.first + dx[i] ;
int ny = u.second + dy[i] ;
int t = dis[u.first][u.second] + 1 ;
if(nx <= n && ny >= 1 && ny <= m && s[nx][ny] != '*' && dis[nx][ny] > t && nx >= 1) {
dis[nx][ny] = t ;
que.push(make_pair(nx,ny) ) ;
}
}
}
} int main(){
scanf("%d%d%d" , &n, &m , &q) ;
for(int i=1;i<=n;i++)scanf("%s",s[i]+1);
if(q%2==1){
printf("IMPOSSIBLE\n");
return 0;
}
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)dis[i][j] = 999999999 ;
int sx,sy;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(s[i][j]=='X')bfs(i,j),sx=i,sy=j;
}
}
int cnt = 0 ;
while(cnt < q) {
cnt ++ ;
int i ;
for(i = 0 ; i < 4 ; i ++ ){
int nx = sx+dx[i];
int ny = sy+dy[i];
if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && s[nx][ny] != '*' && dis[nx][ny] <= q - cnt) {
sx = nx ;
sy = ny ;
ans[cnt]=z[i];
break ;
}
}
if(i == 4) {
cnt = -1 ;
break ;
}
}
if(cnt == -1) printf("IMPOSSIBLE\n");
else {
for(int i=1;i<=cnt;i++)printf("%c",ans[i]);
printf("\n");
}
}

  

VK Cup 2017 - Qualification 1 C 贪心的更多相关文章

  1. VK Cup 2017 - Квалификация 1

    CF上的VK Cup 2017资格赛1,好像很水,因为只有俄文所以语言是最大的障碍--不过之后正式赛貌似就有英文了.(比赛貌似只有开俄文模式才看的到--) 时长1天,不随时间扣分.FallDream ...

  2. DP VK Cup 2012 Qualification Round D. Palindrome pairs

    题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...

  3. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 菜鸡只会ABC!

    Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 全场题解 菜鸡只会A+B+C,呈上题解: A. Bear and ...

  4. Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) A B C D 水 模拟 二分 贪心

    A. Is it rated? time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  5. Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3)(A.B.C,3道暴力题,C可二分求解)

    A. Is it rated? time limit per test:2 seconds memory limit per test:256 megabytes input:standard inp ...

  6. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) C. Bear and Different Names 贪心

    C. Bear and Different Names 题目连接: http://codeforces.com/contest/791/problem/C Description In the arm ...

  7. VK Cup 2016 - Qualification Round 1 (Russian-Speaking Only, for VK Cup teams) D. Running with Obstacles 贪心

    D. Running with Obstacles 题目连接: http://www.codeforces.com/contest/637/problem/D Description A sports ...

  8. Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) E. Prairie Partition 二分+贪心

    E. Prairie Partition It can be shown that any positive integer x can be uniquely represented as x =  ...

  9. Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) 题解【ABCDE】

    A. Vicious Keyboard 题意:给你一个字符串,里面只会包含VK,这两种字符,然后你可以改变一个字符,你要求VK这个字串出现的次数最多. 题解:数据范围很小,暴力枚举改变哪个字符,然后c ...

随机推荐

  1. 学生成绩管理系统【c】

    #include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> #d ...

  2. 3N Numbers

    D - 3N Numbers Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem Statement Let N b ...

  3. js HTML DOM TableRow 对象(innerHTML)

    TableRow 对象 TableRow 对象代表一个 HTML 表格行. 在 HTML 文档中 <tr> 标签每出现一次,一个 TableRow 对象就会被创建. TableRow 对象 ...

  4. grafana-----Time Range Controls

    Grafana提供了许多方法来管理时间的可视化数据的范围,在Dashboard-level和Panel-level都有. 在右上角,您有主仪表板时间选择器(它位于“Zoom out”和“Refresh ...

  5. 2014-08-28——PC端几款主流浏览器的内核

    Trident(IE浏览器) Mozilla(Gecko)(熟悉的有Firefox,Flock等浏览器) WebKit(熟悉的有Safari.Chrome等浏览器) Opera(presto)(Ope ...

  6. 插叙LTE-2

      LTE TDD与LTE FDD技术简介和比较 标签: 频分双工(FDD) 时分双工(TDD) LTE 摘要:UTRA 的长期演进(Long Term Evolution ,LTE) 技术存在LTE ...

  7. 安装nginx包

    1.二进制安装源码包,直接输入yum stall nginx -y就可以 2,后面会涉及路径,所以先查下nginx的路径rpm -ql nginx 3,进入bin目录进行设置 4, 5,查看系统自带的 ...

  8. Linux中的history命令

    history -c  清空历史命令 -w 把缓存中的历史命令写入历史命令保存文件说明: a.在用户登录的时候执行的命令会先存在缓存里 b.当用户退出的时候会把缓存里的命令写到文件里 c.用会执行命令 ...

  9. sql server监控图解

  10. 005-MYSQL数据库设计原则

    1.核心原则 不在数据库做运算; cpu计算务必移至业务层; 控制列数量(字段少而精,字段数建议在20以内); 平衡范式与冗余(效率优先:往往牺牲范式) 拒绝3B(拒绝大sql语句:big sql.拒 ...