传送门
题目描述
给你一个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
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/