是一道很有趣的题目

给出地图和起始点 与要求的步数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. std::condition_variable(3)复习

    #include <iostream> // std::cout #include <thread> // std::thread #include <mutex> ...

  2. Javascript中的函数中的this值

    看下面这段代码会在控制台上输出什么内容? <script> var url="fang.com"; var obj={ url:"soufun.com&quo ...

  3. poj3096

    C++的标准模版库的应用 Surprising Strings Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6625   ...

  4. 一个经验丰富的网站建设程序员的CSS资料

    没有就不能活的 53 个 CSS 技术 对新手实用的 20 个 CSS 建议 快速编写更好 CSS 代码的 5 种方法 50+ 个 CSS 创意案例和教程 101 个 CSS 小贴士.教程和范例 CS ...

  5. D3D9和OpenGL加载纹理图片的API是哪个?

    D3D9 创建一个空纹理,当返回 S_OK 且 ppTexture 纹理对象指针不为 NULL 时,则表示该函数调用成功. HRESULT D3DXCreateTexture( _In_  LPDIR ...

  6. Python是如何进行类型转换的?

    函数                      描述int(x [,base ])         将x转换为一个整数long(x [,base ])        将x转换为一个长整数float(x ...

  7. 2015.7.16(小高开忍住没有减仓,大盘涨3.5%,百股涨停——买进中重、中航,指导WXL错误)

    1.大智慧轻微高开,按照昨天总结的震荡行情指导操作(pic1) a.震荡行情,开盘5分钟的走势不能指导操作, b.操作一定要等到2峰2谷出现后再做!开盘价不能作为峰.谷! c.只有当通道出现2.0%以 ...

  8. Android:日常学习笔记(8)———探究UI开发(2)

    Android:日常学习笔记(8)———探究UI开发(2) 对话框 说明: 对话框是提示用户作出决定或输入额外信息的小窗口. 对话框不会填充屏幕,通常用于需要用户采取行动才能继续执行的模式事件. 提示 ...

  9. 给二维码(图片)添加文字(水印),让生成的二维码中间带logo

    <?php //生成二维码 require_once IA_ROOT . '/framework/library/qrcode/phpqrcode.php'; QRcode::png($url, ...

  10. 设计模式—迭代器Iterator模式

    什么是迭代器模式? 让用户通过特定的接口访问容器的数据,不需要了解容器内部的数据结构. 首先我们先模仿集合中ArrayList和LinkedList的实现.一个是基于数组的实现.一个是基于链表的实现, ...