USACO 1.2 Problem 2

来源:互联网 发布:动漫创作软件 编辑:程序博客网 时间:2024/06/12 01:27

代码丑的不能直视系列,其实可以用循环把代码整的好看一些,不过没有这个必要了。


题目描述:

输入一组图案,再输入一组图案,给你几个操作,让你判断是否能通过这些操作把第一幅图案转换到第二幅。那么这道题是基础题,因为只需要按顺序来一遍就好了。

算法思想:

也就是训练了一下写旋转数组和对称数组的技巧,其他就没有什么值得说的了。就是各种循环,看code的话应该直接就可以看懂。

关于rotate和reflect,其实用纸写下来就可以完成变换。

代码部分:

#include <fstream>#include <iostream>#include <map>using namespace std;ifstream fin("transform.in");ofstream fout("transform.out");int n;char c[12][12];char tmp[12][12];char res[12][12];bool match(char c1[12][12],char c2[12][12]) {for (int i = 1; i <= n; ++i) {for (int j = 1; j <= n; ++j) {if (c1[i][j] != c2[i][j]) return false;}}return true;}void rotate(char c1[12][12]) {for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {tmp[j][n+1-i] = c1[i][j];}}for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {c1[i][j] = tmp[i][j];}}}void reflect(char c1[12][12]) {for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {tmp[i][n+1-j] = c1[i][j];}}for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {c1[i][j] = tmp[i][j];}}}int main() {fin >> n;for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {fin >> c[i][j];}}for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {fin >> res[i][j];}}rotate(c);if (match(c, res)) {fout << "1" << endl;return 0;}rotate(c);if (match(c, res)) {fout << "2" << endl;return 0;}rotate(c);if (match(c, res)) {fout << "3" << endl;return 0;}rotate(c);reflect(c);if (match(c, res)) {fout << "4" << endl;return 0;}rotate(c);if (match(c, res)) {fout << "5" << endl;return 0;}rotate(c);if (match(c, res)) {fout << "5" << endl;return 0;}rotate(c);if (match(c, res)) {fout << "5" << endl;return 0;}rotate(c);if (match(c, res)) {fout << "6" << endl;return 0;}fout << "7" << endl;return 0;}


0 0
原创粉丝点击