Hackerrank--Lexicographic paths

题目链接

Krishnakant is standing at (0,0) in the Cartesian plane. He wants to go to the point (N,M) in the same plane using only horizontal and vertical moves of 1 unit. There are many ways of doing this, and he is writing down all such ways. Every way will comprise of few H moves and few V moves. i.e. moves in horizontal and vertical direction respectively. For example, if we want to go to point (2,2) from point (0,0), HVHV is one of the possible ways.

Given the value of K, he wants to know lexicographically Kth smallest way of going to (N,M) from (0,0).

Input Format
The first line contains an integer T , i.e., number of test cases. 
Next T lines will contain integers N,M and K.

Output Format
For each test case, print lexicographically Kth smallest path.

Constraints
1≤T≤100000 
1≤N≤10 
1≤M≤10 
0≤K<number of paths

Sample Input

2
2 2 2
2 2 3

Sample Output

HVVH
VHHV

Explanation

All the paths of going to (2,2) from (0,0) in lexicographically increasing order:

0.HHVV
1.HVHV
2.HVVH
3.VHHV
4.VHVH
5.VVHH

这题WA的我晕头转向。。题水人更水啊。

题目意思就是给出三个数,n, m, k,问从坐标系中的(0, 0)点走到(n, m)点的字典序第k小的路径是什么。

每次只能向右或者向下走一步,水平走记为‘H‘, 垂直走记为‘V‘。

这题并不是难题。但是由于不太喜欢或者说不太擅长处理坐标系相关的题目,WA了十几次。

以后做这种题目还是老老实实的画坐标系吧。

Accepted Code:

 1 #include <cassert>
 2 #include <iostream>
 3 #include <string>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cstdlib>
 7 using namespace std;
 8
 9 int dp[15][15], T, N, M, K;
10
11 int main(void) {
12     memset(dp, 0, sizeof(dp));
13     for (int i = 9; i >= 0; i--) dp[i][10] = 1;
14     for (int j = 9; j >= 0; j--) dp[10][j] = 1;
15     for (int i = 9; i >= 0; i--) for (int j = 9; j >= 0; j--) dp[i][j] = dp[i + 1][j] + dp[i][j + 1];
16     ios::sync_with_stdio(false);
17     cin >> T;
18     while (T--) {
19         cin >> N >> M >> K;
20         assert(N <= 10 && M <= 10 && N >= 1 && M >= 1);
21         K++;
22         string ans = "";
23         int x = 10 - N, y = 10 - M;
24         assert(K <= dp[x][y]);
25         for (int i = 0; i < N + M; i++) {
26             if (x == 10) {
27                 ans += ‘V‘;
28                 y++; continue;
29             } else if (y == 10) {
30                 ans += ‘H‘;
31                 x++; continue;
32             }
33             if (dp[x + 1][y] < K) {
34                 ans += ‘V‘;
35                 K -= dp[x + 1][y];
36                 y++;
37             } else {
38                 ans += ‘H‘;
39                 x++;
40             }
41         }
42         cout << ans << endl;
43     }
44     return 0;
45 }
时间: 2024-11-29 09:39:00

Hackerrank--Lexicographic paths的相关文章

soj 1015 Jill&#39;s Tour Paths 解题报告

题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every year, Jill takes a bicycle tour between two villages. There are different routes she can take between these villages, but she does have an upper limit

R8:Learning paths for Data Science[continuous updating…]

Comprehensive learning path – Data Science in Python Journey from a Python noob to a Kaggler on Python So, you want to become a data scientist or may be you are already one and want to expand your tool repository. You have landed at the right place.

leetcode笔记:Unique Paths

一. 题目描述 A robot is located at the top-left corner of a m n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish

LeetCode --- 62. Unique Paths

题目链接:Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (ma

62.Unique Paths (法1递归-动态规划法2数学公式)

A robot is located at the top-left corner of a m x n grid(marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. Therobot is trying to reach the bottom-right corner of the grid (marked 'Finish'in the

[LeetCode]Unique Paths

题目: 从左上角到右下角的所有可能路径. 思路1: 回溯法去递归遍历所有的路径,但是复杂度太大,无法通过.checkPath方法实现 动态规划法,从左上角到每一格的路径数与它的上面一格和左边一格的路径和: N(m,n)=N(m-1,n)+N(m,n-1): 注意:第一行和第一列的特殊情况. package com.example.medium; /** * A robot is located at the top-left corner of a m x n grid (marked 'Sta

Bonetrousle HackerRank 数学 + 思维题

https://www.hackerrank.com/contests/world-codesprint-6/challenges/bonetrousle 给定一个数n,和k个数,1--k这k个,要求选择b个数,使得这b个数的和等于n. 首先考虑最小值,在1--k中选择前b个数,是最小的,记为mi.最大值,后b个数相加,记为mx 注意到一个东西:如果mi <= n <= mx.那么是绝对可行的.因为mi总能增加1(同时保证满足要求),所以在这个区间里面的,都是可行解. 所以首先从mi开始枚举,

tarjan算法求桥双连通分量 POJ 3177 Redundant Paths

POJ 3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12598   Accepted: 5330 Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the re

LintCode : Unique Paths II

Problem Description: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. Code: public class Solutio

LintCode : Unique Paths

Problem Description: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid