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 ...
随机推荐
- iptables 概念 1
[ 实战笔记 -- iptables 概念 1 ] 一. 防火墙相关概念 # 从逻辑上讲,防火墙可以分为主机防火墙和网络防火墙. 1> 主机防火墙: 针对于单个主机进行防护 2> 网络防火 ...
- Win7 sql2005附加数据库失败 错误5120
错误信息如下: 标题: Microsoft SQL Server Management Studio Express------------------------------ 附加数据库 对于 ...
- 初探webpack之环境配置
先感叹一句,前端的发展真是太快了,ng和bb还没怎么学好就要过时了.现在感觉react当是未来的一个大方向. 以前一直用的grunt,不过前段时间作者已经停止更新了.正好webpack风头正盛,咱也不 ...
- 【BZOJ4403】序列统计 Lucas定理
[BZOJ4403]序列统计 Description 给定三个正整数N.L和R,统计长度在1到N之间,元素大小都在L到R之间的单调不降序列的数量.输出答案对10^6+3取模的结果. Input 输入第 ...
- WPF中DPI的问题
先搞清楚一下几个概念: DPI:dots per inch ,每英寸的点数.我们常说的鼠标DPI,是指鼠标移动一英寸的距离滑过的点数:打印DPI,每英寸的长度打印的点数:扫描DPI,每英寸扫描了多 ...
- 《挑战程序设计竞赛》2.1 穷竭搜索 POJ2718 POJ3187 POJ3050 AOJ0525
POJ2718 Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6509 Acce ...
- GDI+的应用
在VS中创建窗体 (1)CDI+清除绘画面 在窗体中写入代码: protected override void OnPaint(PaintEventArgs e){ Graphics g=e.Grap ...
- leetcode第一刷_Rotate Image
这个题该怎么说呢.旋转又要求inplace.一般就是要找到某种规律了.这个还是非常明显的,画一下原来的.再画一下旋转之后的.看看原来的跑到什么位置了. 牵扯到四个位置按顺时针方向互换一下位置,发现仅仅 ...
- ios 避免两个button同一时候被点击
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/superchaoxian/article/details/24631293 这个能够通过[btn ...
- 017-Hadoop Hive sql语法详解7-去重排序、数据倾斜
一.数据去重排序 1.1.去重 distinct与group by 尽量避免使用distinct进行排重,特别是大表操作,用group by代替 -- 不建议 select DISTINCT key ...