当前位置: 首页 > news >正文

洛谷每日一题(P1205 [USACO1.2] 方块转换 Transformations)矩阵变换

原题目链接:

P1205 [USACO1.2] 方块转换 Transformations - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

原题目截图:

思路分析:

这题目还是比较简单,模拟一下旋转变化的过程,然后注意变换的规律就行了。

  1. 读取输入:首先读取矩阵的大小 n,然后分别读取两个矩阵 startend

  2. 定义操作函数

    • op1:顺时针旋转90度。
    • op2:逆时针旋转90度(通过两次旋转180度实现)。
    • op3:逆时针旋转90度。
    • op4:水平方向翻转。
    • op5:先水平翻转,然后尝试顺时针或逆时针旋转90度。
    • op6:不做任何操作。
  3. 比较矩阵:通过调用上述操作函数,比较操作后的 start 矩阵是否与 end 矩阵相等。

  4. 输出结果:根据操作函数的返回值,输出对应的操作编号。如果所有操作都无法将 start 矩阵转换成 end 矩阵,则输出7。

解决代码:

#include<iostream>
using namespace std;
#include<vector>bool op1(vector<vector<char>>&start,vector<vector<char>>& end,int n) {//顺时针旋转90度vector<vector<char>>martix = start;for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {martix[j][n - 1 - i] = start[i][j];}}return end == martix;}bool op2(vector<vector<char>>& start,vector<vector<char>>& end, int n) {vector<vector<char>>martix = start;for (int i = n - 1; i >= 0; i--) {for (int j = n - 1; j >= 0; j--) {martix[n - 1 - i][n - 1 - j] = start[i][j];}}return end == martix;}bool op3(vector<vector<char>>& start,vector<vector<char>>& end, int n) {vector<vector<char>>martix = start;//逆时针旋转90度for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {martix[n-1-j][i] = start[i][j];}}return end == martix;}bool op4(vector<vector<char>>& start,vector<vector<char>>& end, int n) {vector<vector<char>>martix = start;//水平方向翻转for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {martix[i][n-1-j] = start[i][j];}}return end == martix;}bool op5(vector<vector<char>>& start,vector<vector<char>>& end, int n) {vector<vector<char>>martix = start;//水平方向翻转后,再执行op1,op2,op3之间的其中一种方式for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {martix[i][n - 1 - j] = start[i][j];}}return op1(martix, end, n) || op2(martix, end, n) || op3(martix, end, n);}bool op6(vector<vector<char>>& start,vector<vector<char>>& end, int n) {vector<vector<char>>martix = start;//不变return end == start;}int main() {int n;cin >> n;vector<vector<char>>start(n,vector<char>(n));vector<vector<char>>end(n, vector<char>(n));for (int i = 0; i < n; i++) {string str;cin >> str;for (int j = 0; j < str.size();j++) {start[i][j] = str[j];}}for (int i = 0; i < n; i++) {string str;cin >> str;for (int j = 0; j < str.size(); j++) {end[i][j] = str[j];}}if (op1(start, end, n))  cout << 1;else if (op2(start, end, n)) cout << 2;else if (op3(start, end, n)) cout << 3;else if (op4(start, end, n)) cout << 4;else if (op5(start, end, n)) cout << 5;else if (op6(start, end, n)) cout << 6;else cout << 7;  //无法转换return 0;}


http://www.mrgr.cn/news/44036.html

相关文章:

  • 微服务架构:核心组件解析与设计思考(服务发现、API网关、 配置中心、负载均衡、服务调用、服务熔断、链路追踪、消息队列、服务安全、分布式事务)
  • [C#]C# winform部署yolov11-pose姿态估计onnx模型
  • Golang 进阶5—— 反射
  • ECML PKDD 2024 | 时空数据(Spatial-Temporal)和时间序列(Time series)论文总结
  • Linux环境下的日志文件的实现
  • mmdetection报错:value error: need at least one array to concatenate
  • cnn突破六(四层bpnet网络公式)
  • 国内超声波清洗机哪个品牌好?力荐四款超耐用超声波清洗机!
  • 【React】事件机制
  • clientWidth,offsetWidth,scrollHeight
  • 产品经理都会的ComfyUI搭建指南
  • SAP 投资 1200 万新元推动新加坡的人工智能创新
  • React响应式修改数组和对象
  • 通过 Groovy 实现业务逻辑的动态变更
  • C# 非泛型集合基础:ArrayList与Hashtable的使用与注意事项
  • c#代码介绍23种设计模式_19状态者模式
  • Vue入门-Vue中实例和java中类的相同和不同
  • 阿里云百炼通义大模型接入流程,手把手教程
  • Python中的数据可视化:从入门到进阶
  • Hive数仓操作(十六)