UVA 1156 - Pixel Shuffle

题目链接

题意:依据题目中的变换方式,给定一串变换方式,问须要运行几次才干回复原图像

思路:这题恶心的一比,先模拟求出一次变换后的相应的矩阵,然后对该矩阵求出全部循环长度,全部循环长度的公倍数就是答案

代码:

#include <stdio.h>
#include <string.h> const int N = 1100;
int t, n, g[N][N], vis[N][N], save[N][N];
char str[N], s[N]; void rot(int flag) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (!flag)
save[i][j] = g[n - j - 1][i];
else
save[n - j - 1][i] = g[i][j];
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
g[i][j] = save[i][j];
} void sym(int flag) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
save[i][j] = g[i][n - j - 1];
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
g[i][j] = save[i][j];
} void bhsym(int flag) {
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < n; j++)
save[i][j] = g[i][j];
}
for (int i = n / 2; i < n; i++)
for (int j = 0; j < n; j++)
save[i][j] = g[i][n - j - 1];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
g[i][j] = save[i][j];
} void bvsym(int flag) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i < n / 2) save[i][j] = g[i][j];
else save[i][j] = g[3 * n / 2 - 1 - i][j];
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
g[i][j] = save[i][j];
} void div(int flag) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (!flag) {
if (i % 2) save[i][j] = g[i / 2 + n / 2][j];
else save[i][j] = g[i / 2][j];
}
else {
if (i % 2) save[i / 2 + n / 2][j] = g[i][j];
else save[i / 2][j] = g[i][j];
}
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
g[i][j] = save[i][j];
} void mix(int flag) {
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (i % 2 == 0){
if (flag) {
if (j % 2 == 0) save[i][j] = g[i][j / 2];
else save[i][j] = g[i + 1][j / 2];
}
else {
if (j % 2 == 0) save[i][j / 2] = g[i][j];
else save[i + 1][j / 2] = g[i][j];
}
}else{
if (flag) {
if(j % 2 == 0) save[i][j] = g[i - 1][n / 2 + j / 2];
else save[i][j] = g[i][n / 2 + j / 2];
}
else {
if(j % 2 == 0) save[i - 1][n / 2 + j / 2] = g[i][j];
else save[i][n / 2 + j / 2] = g[i][j];
}
}
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
g[i][j] = save[i][j];
} void change(char *str) {
int len = strlen(str);
int flag = 1;
if (str[0] == '-') {
flag = 0;
str++;
}
if (strcmp(str, "tor") == 0) rot(flag);
else if (strcmp(str, "mys") == 0) sym(flag);
else if (strcmp(str, "myshb") == 0) bhsym(flag);
else if (strcmp(str, "mysvb") == 0) bvsym(flag);
else if (strcmp(str, "vid") == 0) div(flag);
else if (strcmp(str, "xim") == 0) mix(flag);
} void tra() {
int len = strlen(str);
int sn = 0;
for (int i = len - 1; i >= 0; i--) {
if (str[i] == ' ') {
s[sn] = '\0';
change(s);
sn = 0;
}
else {
s[sn++] = str[i];
}
}
s[sn] = '\0';
change(s);
} int gcd(int a, int b) {
if (!b) return a;
return gcd(b, a % b);
} int lcm(int a, int b) {
return a / gcd(a, b) * b;
} int solve() {
int ans = 1;
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (!vis[i][j]) {
vis[i][j] = 1;
int cnt = 1;
int x = g[i][j] / n;
int y = g[i][j] % n;
while (!vis[x][y]) {
cnt++;
vis[x][y] = 1;
int t = g[x][y] / n;
y = g[x][y] % n;
x = t;
}
ans = lcm(ans, cnt);
}
}
}
return ans;
} void init() {
scanf("%d", &n);
getchar();
gets(str);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
g[i][j] = i * n + j;
}
}
} int main() {
scanf("%d", &t);
while (t--) {
init();
tra();
printf("%d\n", solve());
if (t) printf("\n");
}
return 0;
}

UVA 1156 - Pixel Shuffle(模拟+置换)的更多相关文章

  1. uva 10710 - Chinese Shuffle(完美洗牌)

    option=com_onlinejudge&Itemid=8&category=474&page=show_problem&problem=1651"> ...

  2. LA 3510 (置换 循环分解) Pixel Shuffle

    思路挺简单的,题目中的每个命令(包括命令的逆)相当于一个置换. 用O(n2k)的时间复杂度从右往左求出这些置换的乘积A,然后求m使Am = I(I为全等置换) 还是先把A分解循环,m则等于所有循环节长 ...

  3. UVALive - 3510 Pixel Shuffle (置换)

    题目链接 有一个n*n的图像和7种置换,以及一个置换序列,求将这个序列重复做几次能得到原图像. 将这些置换序列乘起来可得到一个最终置换,这个置换所有循环节的长度的lcm即为答案. 注意置换是从右往左进 ...

  4. UVa 11210 - Chinese Mahjong 模拟, 枚举 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  5. UVA 12050 - Palindrome Numbers 模拟

    题目大意:给出i,输出第i个镜像数,不能有前导0. 题解:从外层开始模拟 #include <stdio.h> int p(int x) { int sum, i; ;i<=x;i+ ...

  6. Uva 679 Dropping Balls (模拟/二叉树的编号)

    题意:有一颗二叉树,深度为D,所有节点从上到下从左到右编号为1,2,3.....在结点一处放一个小球,每个节点是一个开关,初始全是关闭的,小球从顶点落下,小球每次经过开关就会把它的状态置反,现在问第k ...

  7. ●UVa 1589 Xiangqi(模拟)

    ●赘述题意 给出一个中国象棋残局,告诉各个棋子的位置,黑方只有1枚“将”,红方有至少2枚,至多7枚棋子,包含1枚“帅G”,和若干枚“车R”,“马H”,“炮C”.当前为黑方的回合,问黑方的“将”能否在移 ...

  8. 【每日一题】 UVA - 213 Message Decoding 模拟解码+读入函数+阅读题

    题意:阅读理解难度一道比一道难orz.手摸了好久样例 题解: 读入:大循环用getline读入header顺便处理一下,  里面再写两重循环,外层一次读三个串,内层一次读num个串. 之后就查表,线性 ...

  9. LA3510 Pixel Shuffle

    题意 PDF 分析 思路挺简单的,题目中的每个命令(包括命令的逆)相当于一个置换. 用\(O(n^2k)\)的时间复杂度从右往左求出这些置换的乘积A,然后求m使Am = I(I为全等置换) 还是先把A ...

随机推荐

  1. celery work logging 问题

    celery 的日志里只输出日志 不输入标准打印

  2. python try except 捕捉错误得到错误的时候的值

    try: dict_reason = self.get(name,id_number,mobile,card_number,**kwargs) except RetryError as e: # 获取 ...

  3. erlang虚拟机代码运行原理

    erlang是开源的,非常多人都研究过源码.可是.从erlang代码到c代码.这是个不小的跨度.并且代码也比較复杂. 所以这里,我利用一些时间,整理下erlang代码的运行过程.从erlang代码编译 ...

  4. [51Nod]NOIP2018提高组省一冲奖班模测训练(一)题解

    http://www.51nod.com/contest/problemList.html#!contestId=72&randomCode=147206 原题水题大赛.. A.珂朵莉的旅行 ...

  5. ConcurrentHashMap实现原理--转载

    原文地址:http://ajax-xu.iteye.com/blog/1104649 ConcurrentHashMap是Java 5中支持高并发.高吞吐量的线程安全HashMap实现.在这之前我对C ...

  6. C#里如何把一个DataTable的数据追加进数据库里的某个表

    方法一: DataTable table = new DataTable(); //TODO: init table... string connStr = "user id=" ...

  7. matlab 辅助函数 —— 文件下载与文件解压

    0. 可读性的提升 为了提升代码的交互友好性,可在代码执行一些耗时操作时,显示地输出一些文本信息,以显示进度: fprintf('Downloading xxfilename...\n') urlwr ...

  8. Bootstrap时间控件常用配置项

    1.给下面4个文本框初始化   $(function(){ $("#orderStartTime,#orderEndTime,#preSaleStartTime,#preSaleEndTim ...

  9. [python]bug和debug

    bug:代码中存在的语法或者逻辑问题 debug:自查和解决代码中的问题 (coding五分钟,debug两小时) 一.出现bug原因的四大类型 1.粗心 1)错误案例 上面这个错误就是因为 if语句 ...

  10. Nginx分发服务

    nginx配置分发tomcat服务 http://blog.csdn.net/yan_chou/article/details/53265775 http://www.cnblogs.com/deng ...