B. Diagonal Walking v.2

链接

[https://i.cnblogs.com/EditPosts.aspx?opt=1]

题意

二维平面从原点出发k步,要到达的点(x,y),每个位置可以往8个方位移动,问到达目的地最多可以走多少斜路

如果不可以到达输出-1;

分析

找规律,看代码自己琢磨

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll q,x,y,k;
    cin>>q;
    while(q--){
        cin>>x>>y>>k;
        if(max(x,y)>k) {
            cout<<-1<<endl;
            continue;
        }
        if(x>y) swap(x,y);
        if((y-x)&1) k--;
        else if((k-y)&1) k-=2;
        cout<<k<<endl;
    }
    return 0;
} 

原文地址:https://www.cnblogs.com/mch5201314/p/9879710.html

时间: 2024-07-31 16:57:44

B. Diagonal Walking v.2的相关文章

Diagonal Walking v.2 CodeForces - 1036B (思维,贪心)

Diagonal Walking v.2 CodeForces - 1036B Mikhail walks on a Cartesian plane. He starts at the point (0,0)(0,0), and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point (0,0)(0,0), he can go to any o

CF 1036B Diagonal Walking v.2——思路

题目:http://codeforces.com/contest/1036/problem/B 比赛时只能想出不合法的情况还有走到终点附近的方式. 设n<m,不合法就是m<k.走到终点方式就是先斜着走了n*n的正方形,然后一拐一拐地走到终点或距离终点仅剩一个格子的地方.走到终点后可以走任意偶数步,走出去终点又走回来这样. 然后开始超麻烦地考虑,比如走很多横着的步使得起点向终点移动一些,然后-- 最后发现过不了样例. 赛后看看别人的代码,发现异常简单.就是到上面那一步之后, 如果一拐一拐地正好走

CF 1036 B Diagonal Walking v.2 —— 思路

题目:http://codeforces.com/contest/1036/problem/B 题意:从 (0,0) 走到 (n,m),每一步可以向八个方向走一格,问恰好走 k 步能否到达,能到达则输出最多能走多少斜步: 自己想得太复杂了... 首先,判断 -1 就看横纵距离中的较大值是否大于 k ,因为最少走 max(n,m) 步可以到达: 设 m > n: 如果 m - n 为奇数,那么显然会有一步必须直着走,那么 k --: 这里可以通过走法来调节剩余步数的奇偶,就是直着走过去或者拐一下走

Codeforces 1036B Diagonal Walking v.2 【贪心】

题目传送门:https://codeforces.com/contest/1036/problem/B 被这道题坑了,说白了还是菜. 贪心策略是先斜对角从(0,0)走到(n,n),然后往右拐(分奇偶考虑)[若n>m,swap(n,m)] 理论上是画画图,知道切入点是奇偶性后,就能想清楚了 #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<q

codeforces cf edu round#50 B. Diagonal Walking v.2

思路:当m > k时输出-1(设m是较大的数),当m-n是奇数时有一步不能走对角线所以k--,当走对角线可以直接到达终点,如果剩余的步数是奇数则有两步不能走对角线所以k - 2.(画图观察规律) #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { int q; scanf("%d",&q); while(q--) { ll n,m,k; scanf(&q

Codeforces Edu Round 50 A-D

A. Function Height 由于只能提升\(x\)为奇数的点,每个三角形的底一定为\(2\), 则要求我们求: \(2 * (h_1 + h_2 + - + h_n) / 2 = k\),使\(max(h_1, h_2-h_n)\)最小. 则应使每个\(h\)平摊重量,答案即为\(\lceil n/k \rceil\). #include <cstdio> #include <iostream> #include <cmath> typedef long lo

1.22训练赛 --ac2

Final standings Solved: 2 out of 7 ac:A题水题  b题思维题 b题: B. Diagonal Walking v.2 time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Mikhail walks on a Cartesian plane. He starts at the point (0,0

Educational Codeforces Round 40 (Rated for Div. 2) Partial Solution

从这里开始 小结 题目列表 Problem A Diagonal Walking Problem B String Typing Problem C Matrix Walk Problem D Fight Against Traffic Problem E Water Taps Problem F Runner's Problem Problem G Castle Defense Problem H Path Counting Problem I Yet Another String Match

基于HTML5的WebGL呈现A星算法的3D可视化

http://www.hightopo.com/demo/astar/astar.html 最近搞个游戏遇到最短路径的常规游戏问题,一时起兴基于HT for Web写了个A*算法的WebGL 3D呈现,算法基于开源 https://github.com/bgrins/javascript-astar 的javascript实现,其实作者也有个不错的2D例子实现 http://www.briangrinstead.com/files/astar/ ,只不过觉得所有A*算法的可视化实现都是平面的不够