UVA 1156 - Pixel Shuffle(模拟+置换)
2016-01-05 15:35
204 查看
UVA 1156 - Pixel Shuffle
题目链接题意:依据题目中的变换方式,给定一串变换方式,问须要运行几次才干回复原图像
思路:这题恶心的一比,先模拟求出一次变换后的相应的矩阵,然后对该矩阵求出全部循环长度,全部循环长度的公倍数就是答案
代码:
#include <stdio.h> #include <string.h> const int N = 1100; int t, n, g , vis , save ; char str , s ; 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; }
相关文章推荐
- cocos2dx-网络连接客户端部分学习
- 数据库引擎配置 - 数据目录
- 景子渊:原油沥青白银暴涨暴跌,空单被套之后多单又被套,频频套单怎么办?
- 深度学习与计算机视觉系列(2)_图像分类与KNN
- 【Android基础学习】带进度条的WebView 2
- 菜鸟之路【计算导论与C基础】练习1 计算概论第五周 B-04作业
- Ionic 自动生成icon和splash图片
- 从头认识java-16.1 文件目录列表(File类的list())
- Window7下安装openssl完整版(亲测实现)
- 关于新增分区过程中遇到的问题(续)
- for循环输出九九乘法表
- 二维数组模拟手机翻转
- [CentOS 0010] CentOS 配置mysql允许远程登录
- erlangCPU异常波动debug过程
- Failure [INSTALL_FAILED_DEXOPT]解决方案
- Application Structure(应用程序的结构)
- Volley请求源码解读 (1) ---无缓存请求
- Volley请求源码解读 (1) ---无缓存请求
- android java文件调用.so文件
- 字段计算器通过python脚本完成要素节点坐标信息提取到属性字段中