CodeForces 1301D :Time to Run 思维

传送门

题目描述

给你一个n×m的网格,相邻的格子之间有双向边(来回两条边).
问是否能不经过重复的边的情况下从左上角出发遍历k条边.

分析

因为每个点的度数均为偶数,所以很显然是一张欧拉图,构造一下路线即可

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
const int INF = 0x3f3f3f3f;
const int N = 100010;
const int mod = 1e9 + 7;
int n,m,k;
vector<PII> ans;

void work(int x,char y){
    if(k == 0|| x == 0) return;
    if(k <= x){
        ans.push_back({k,y});
        k = 0;
        return;
    }
    ans.push_back({x,y});
    k -= x;
    return;
}

int main(){
    scanf("%d%d%d",&n,&m,&k);
    ll p = 2 * (2 * n * m - n - m);
    if(p < k){
        puts("NO");
        return 0;
    }
    puts("YES");
    work(m - 1,'R');
    for(int i = 1;i < m;i++)
        work(n - 1,'D'),work(n - 1,'U'),work(1,'L');
    for(int i = 1;i < n;i++)
        work(1,'D'),work(m - 1,'R'),work(m - 1,'L');
    work(n - 1,'U');
    printf("%d\n",ans.size());
    for(int i = 0;i < ans.size();i++) printf("%d %c\n",ans[i].first,ans[i].second);
    return 0;
}

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值