搜索
首页数据库mysql教程USACO 1.2.2 Transformations(模拟)

分析 这是我第一次在ACM的题目中用OO的思想写的程序,看到标程,竟不谋而合,结构是类的。对正方形这个类分析,将会使问题变得简单,我觉得OO的分析和设计挺关键的,其实我一开始也没设计好,原先准备把7个bool函数当成类的成员方法,其实这个设计是不好的,

分析

       这是我第一次在ACM的题目中用OO的思想写的程序,看到标程,竟不谋而合,结构是类似的。对正方形这个类分析,将会使问题变得简单,我觉得OO的分析和设计挺关键的,其实我一开始也没设计好,原先准备把7个bool函数当成类的成员方法,其实这个设计是不好的,有点过了。其实应该是把旋转90度轴对称这两个方法作为类的成员方法,这样main中调用就方便自如了。

       最后,我觉得搞ACM,不仅是把题目A掉,同时也应注意程序的结构设计,因为”程序是给人看的“。


2013/3/31

       关于顺时针旋转90度,怎么由原来的坐标得到转换后的坐标,可以用计算机图像学里二维变换的知识,将连续推广到离散的,如下图所示。

       为了与二维数组对应,我将坐标系顺时针旋转了90度,这样就与二维数组的下标情况对应了,假设n为4。


USACO 1.2.2 Transformations(模拟)


关于变换矩阵,先把参考点移到原点,再顺时针旋转90度,最后移回原来参考点。复合变换矩阵:

USACO 1.2.2 Transformations(模拟)

MATLAB程序如下:

clc;
clear all;
syms x y n;

P = [x y 1];
xF = (n - 1) / 2.0;   % center = (xF yF)
yF = xF;
% theta = -pi / 2.0;

Tt1 = [
    1 0 0;
    0 1 0;
    -xF -yF 1
    ];

% 精度有损失
% Tr = [
%     cos(theta) sin(theta) 0;
%     -sin(theta) cos(theta) 0;
%     0 0 1
%     ];

Tr = [
    0 -1 0;
    1 0 0;
    0 0 1
    ];

Tt2 = [
    1 0 0;
    0 1 0;
    xF yF 1
    ];

Pt = P * Tt1 * Tr * Tt2;

display(P);
display(Pt);
变换结果
P =
 
[ x, y, 1]
 
 
Pt =
 
[ y, n - x - 1, 1]


源程序

// #define ONLINE_JUDGE
#define MY_DEBUG
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cassert>
using namespace std;

class Square {
private:
	typedef vector<char> vChar;
	typedef vector<vchar> vvChar;
	vvChar data;
	unsigned n;

public:
	// 用边长来构造
	Square (unsigned _n) : n(_n) {}

	Square rotateClockwise90() {
		Square tmp(n);
		for (unsigned int i = 0; i data[n - 1 - j][i]);
			}
			tmp.data.push_back(vcTmp);
		}

		return tmp;
	}

	Square rotateClockwise180() {
		return this->rotateClockwise90().rotateClockwise90();
	}

	Square rotateClockwise270() {
		return this->rotateClockwise180().rotateClockwise90();
	}

	Square reflecteHorizontal() {
		Square tmp(n);
		for (unsigned int i = 0; i data[i][n - j - 1]);
			}
			tmp.data.push_back(vcTmp);
		}

		return tmp;	
	}

	bool operator==(const Square &other) const {
		if (this->n != other.n) {
			return false;
		}

		for (unsigned i = 0; i data[i][j] != other.data[i][j]) {
					return false;
				}
			}
		}

		return true;
	}

	friend istream & operator>>(istream& is, Square &s) {
		for (unsigned int i = 0; i > cTmp;
				vcTmp.push_back(cTmp);
			}
			s.data.push_back(vcTmp);
		}

		return is;
	}

	friend ostream & operator= 1) {
				cout = 1) {
					cout > sideLen;
	Square sa(sideLen);
	Square sb(sideLen);
	cin >> sa >> sb;

#ifndef MY_DEBUG
	cout 
<p><br>
</p>
<p><br>
</p>
<p><br>
</p>
<p>附:</p>
<h3 id="标程">标程</h3>
<p>注:它的旋转函数中坐标变换错了。</p>

<pre class="brush:php;toolbar:false">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define MAXN 10

typedef struct Board Board;
struct Board {
    int n;
    char b[MAXN][MAXN];
};

/* rotate 90 degree clockwise: [r, c] -> [c, n+1 - r] */
Board
rotate(Board b)
{
    Board nb;
    int r, c;

    nb = b;
    for(r=0; r<b.n r for c nb.b b.b return nb reflect board horizontally:> [r, n-1 -c] */
Board
reflect(Board b)
{
    Board nb;
    int r, c;

    nb = b;
    for(r=0; r<b.n r for c nb.b b.b return nb non-zero if and only boards are equal int eqboard b board bb bb.n bb.b rdboard n b.n="n;" getc assert void main file change fin='fopen("transform.in",' fout='fopen("transform.out",' null fscanf rotate else reflect fprintf exit><br>

<h3 id="题目">题目</h3>


<center>
<strong><span>Transformations</span></strong><br>
</center>

<p>A square pattern of size N x N (1 
</p>
<ul>
<li>#1: 90 Degree Rotation: The pattern was rotated clockwise 90 degrees.</li>
<li>#2: 180 Degree Rotation: The pattern was rotated clockwise 180 degrees.</li>
<li>#3: 270 Degree Rotation: The pattern was rotated clockwise 270 degrees.</li>
<li>#4: Reflection: The pattern was reflected horizontally (turned into a mirror image of itself by reflecting around a vertical line in the middle of the image).</li>
<li>#5: Combination: The pattern was reflected horizontally and then subjected to one of the rotations (#1-#3).</li>
<li>#6: No Change: The original pattern was not changed.</li>
<li>#7: Invalid Transformation: The new pattern was not obtained by any of the above methods.</li>
</ul>
<p>In the case that more than one transform could have been used, choose the one with the minimum number above.</p>
<h3 id="PROGRAM-NAME-transform">PROGRAM NAME: transform</h3>
<h3 id="INPUT-FORMAT">INPUT FORMAT</h3>
<table>
<tbody>
<tr>
<td>Line 1:</td>
<td>A single integer, N</td>
</tr>
<tr>
<td>Line 2..N+1:</td>
<td>N lines of N characters (each either `@' or `-'); this is the square before transformation</td>
</tr>
<tr>
<td>Line N+2..2*N+1:</td>
<td>N lines of N characters (each either `@' or `-'); this is the square after transformation</td>
</tr>
</tbody>
</table>
<h3 id="SAMPLE-INPUT-file-transform-in">SAMPLE INPUT (file transform.in)</h3>
<pre class="brush:php;toolbar:false">3
@-@
---
@@-
@-@
@--
--@

OUTPUT FORMAT

A single line containing the the number from 1 through 7 (described above) that categorizes the transformation required to change from the `before' representation to the `after' representation.

SAMPLE OUTPUT (file transform.out)

1


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
在MySQL中使用视图的局限性是什么?在MySQL中使用视图的局限性是什么?May 14, 2025 am 12:10 AM

mysqlviewshavelimitations:1)他们不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinSorsubqueries.2)他们canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

确保您的MySQL数据库:添加用户并授予特权确保您的MySQL数据库:添加用户并授予特权May 14, 2025 am 12:09 AM

porthusermanagementInmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

哪些因素会影响我可以在MySQL中使用的触发器数量?哪些因素会影响我可以在MySQL中使用的触发器数量?May 14, 2025 am 12:08 AM

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)复杂的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)

mysql:存储斑点安全吗?mysql:存储斑点安全吗?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

mySQL:通过PHP Web界面添加用户mySQL:通过PHP Web界面添加用户May 14, 2025 am 12:04 AM

通过PHP网页界面添加MySQL用户可以使用MySQLi扩展。步骤如下:1.连接MySQL数据库,使用MySQLi扩展。2.创建用户,使用CREATEUSER语句,并使用PASSWORD()函数加密密码。3.防止SQL注入,使用mysqli_real_escape_string()函数处理用户输入。4.为新用户分配权限,使用GRANT语句。

mysql:blob和其他无-SQL存储,有什么区别?mysql:blob和其他无-SQL存储,有什么区别?May 13, 2025 am 12:14 AM

mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而alenosqloptionslikemongodb,redis和calablesolutionsoluntionsoluntionsoluntionsolundortionsolunsolunsstructureddata.blobobobsimplobissimplobisslowderperformandperformanceperformancewithlararengelitiate;

mySQL添加用户:语法,选项和安全性最佳实践mySQL添加用户:语法,选项和安全性最佳实践May 13, 2025 am 12:12 AM

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

MySQL:如何避免字符串数据类型常见错误?MySQL:如何避免字符串数据类型常见错误?May 13, 2025 am 12:09 AM

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollat​​ionsEttingsefectery.1)usecharforfixed lengengters lengengtings,varchar forbariaible lengength,varchariable length,andtext/blobforlabforlargerdata.2 seterters seterters seterters seterters

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。