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 ...
随机推荐
- 3280 easyfinding
3280 easyfinding 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 给一个M ...
- 【BZOJ3239】Discrete Logging BSGS
[BZOJ3239]Discrete Logging Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B ...
- Object类有哪些公用方法?
Object是所有类的父类,任何类都默认继承Object. clone 保护方法,实现对象的浅复制,只有实现了Cloneable接口才可以调用该方法,否则抛出CloneNotSupportedExce ...
- 通过spring boot提供restful api
1 将返回设置为produces = "application/json" 返回给客户端json格式的response. 2 对各种异常的处理 各种异常如何返回给客户端? 各种异常 ...
- DumpBinary
stdafx.h #include "targetver.h" #include <stdio.h> #include <tchar.h> #include ...
- HNOI2014
本蒟蒻到现在才把$HNOI2014$的坑填完... $AFO$之后码力急速下降... 感觉都没有码力了... 附上题解: $DAY1$: $T1$: BZOJ3571: [Hnoi2014]画框 $T ...
- 数据性能调校——查看最耗资源的各种SQL
从计划高速缓存中清除查询计划 DBCC FREEPROCCACHE 清除缓存中的过程 DBCC DROPCLEANBUFFERS清除内存中的数据 SELECT DB_ID('你的数据库名') tota ...
- ABAP开发中message dump
系统里边 消息 造成dump示例, 1.面向对象的method 中一般不能用stop, 例如data_change事件, ** sm30 不能stop, 2. 增强中 有些地方不能stop, 3.还有 ...
- SMW0 上传问题?
*SMW0 和 OAOR 的区别在哪3个方面? SMW0 上传 出现: 没有指派至MIME 类型
- python基础27 -----python进程终结篇-----IO模型
一.IO模型 1.IO模型分类 1.阻塞IO--------blocking IO 2.非阻塞IO------nonblocking IO 3. 多路复用IO------- multiplexing ...