VK Cup 2017 - Qualification 1 C 贪心
是一道很有趣的题目
给出地图和起始点 与要求的步数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 贪心的更多相关文章
- VK Cup 2017 - Квалификация 1
CF上的VK Cup 2017资格赛1,好像很水,因为只有俄文所以语言是最大的障碍--不过之后正式赛貌似就有英文了.(比赛貌似只有开俄文模式才看的到--) 时长1天,不随时间扣分.FallDream ...
- DP VK Cup 2012 Qualification Round D. Palindrome pairs
题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 = ...
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) 题解【ABCDE】
A. Vicious Keyboard 题意:给你一个字符串,里面只会包含VK,这两种字符,然后你可以改变一个字符,你要求VK这个字串出现的次数最多. 题解:数据范围很小,暴力枚举改变哪个字符,然后c ...
随机推荐
- SVG 与 Canvas:如何选择
SVG 与 Canvas:如何选择 61(共 69)对本文的评价是有帮助 - 评价此主题 本主题一开始将对 SVG 与 Canvas 进行简要比较,接下来会讨论大量的比较代码示例,如光线跟踪和绿屏 ...
- 【BZOJ2819】Nim 树状数组+LCA
[BZOJ2819]Nim Description 著名游戏设计师vfleaking,最近迷上了Nim.普通的Nim游戏为:两个人进行游戏,N堆石子,每回合可以取其中某一堆的任意多个,可以取完,但不可 ...
- mysql中对应oracle中的to_char()和to_number()函数
TO_CHAR(): CAST(123 AS CHAR(3)) TO_NUMBER(): cast( '123 ' as SIGNED INTEGER)
- jquery遍历json与数组方法总结
来自:http://www.php100.com/html/program/jquery/2013/0905/5927.html 先我们来参考each() 方法,each()规定为每个匹配元素规定运行 ...
- 【python】-- Django Form
Django Form Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容(自定义样式) 一.F ...
- 如何搭建一个 Git 版本控制服务端?
Gogs 下载和安装 https://github.com/gogits/gogs # 下载二进制压缩包 不检查服务器证书 root@cheungxiongwei:~# wget --no-check ...
- 使用远程管理工具Xshell
介绍 目前常用的远程管理工具 Xshell -- 免费版/收费版 SecureCRT Putty 安装Xshell 选择免费版即可 连接服务器 连接成功 Xshell连接不上排查流程 还有可能是Lin ...
- F110 BADI增强
F110*JOB*&------------------------------------------------------------- F110 BADI FI_BSTM_MC_EXI ...
- 1.2 使用电脑测试MC20模块的GPS功能测试
需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...
- UI控件之UIImageView
UIImageView:图像视图,用于在应用程序中显示图片 UIImage:是将图片文件转换为程序中的图片对象 UIImageView是UIImage的载体 方法一:用此方法创建图片对象,会将图片ca ...