UV-1156 pixel shuffle (Replacement + simulation)

Source: Internet
Author: User
Tags shuffle

Description

Shuffling the pixels in a bitmap image sometimes yields random looking images. however, by repeating the shuffling enough times, one finally recovers the original images. this shoshould be no surprise, since ''shuffling "means applying a one-to-one mapping (or permutation) over the cells of the image, which come in finite number.


Problem


Your program shocould read a numberN, And a series of elementary transformations that define a ''shuffling"NXNImages. Then, your program shocould compute the minimal numberM(M> 0), such thatMApplications of always yield the originalNXNImage.

For instance if is counter-clockwise 90o rotation thenM= 4.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. this line is followed by a blank line, and there is also a blank line between two consecutive inputs.


Input is made of two lines, the first line is numberN(2N210,NEven). The numberNIs the size of images, one image is represented internally byNXNPixel matrix (AJi), whereIIs the row number andJIs the column number. The pixel at the upper left corner is at row 0 and column 0.

The second line is a non-empty list of at most 32 words, separated by spaces. valid words are the keywordsid, rot, sym, bhsym, bvsym, Div and mix, or a keyword followed ''-". each keyword key designates an elementary transform (as defined by Figure 1), andkey-designates the inverse of transform key. for instance, rot-is the inverse of counter-clockwise 90o rotation, that is clockwise 90o rotation. finally, the listK1,K2 ,...,KP designates the compound transform =K1OK2O...OKP. For instance, ''bvsym rot-"is the transform that first performs clockwise 90o rotation and then vertical authentication Ry on the lower half of the image.


Figure 1: Transformations of image ( AJi) into image ( BJi)


Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.


Your program shocould output a single line whose contents is the minimal numberM(M> 0) such that is the identity. You may assume that, for all test input, you haveM<1, 231.

Sample Input
2256rot- div rot div256bvsym div mix
Sample output
863457 question: Give You N * n matrices and several operations. After you repeat several times, you will get the original image idea: Each Command is a replacement, and the operation sequence is the product of replacement, we can finally deal with this matrix. One conclusion: for a loop a with a length of L, if and only when M is an integer multiple of L, A ^ m is a fully equal replacement, therefore, the answer is the minimum public multiple of multiple cycles.
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 1100;int n, g[maxn][maxn], vis[maxn][maxn], save[maxn][maxn];char str[maxn], tmp[maxn];int gcd(int a, int b) {return b == 0 ? a : gcd(b, a % b);}int lcm(int a, int b) {return a / gcd(a, b) * b;}void id() {for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)g[i][j] = save[i][j];}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];}id();}void sym(int flag) {for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)save[i][j] = g[i][n-1-j];id();}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-1-j];id();}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];}id();}void div(int flag) {for (int i = 0; i < n; i++)for (int j = 0; j < n; j++) {if (flag == 1) {if (i & 1) save[i / 2 + n / 2][j] = g[i][j];else save[i / 2][j] = g[i][j];}else {if (i & 1)save[i][j] = g[i / 2 + n / 2][j];else save[i][j] = g[i / 2][j];}}id();}void mix(int flag) {for (int i = 0; i < n; i++)for (int j = 0; j < n; j++) {if ((i & 1) == 0) {if (flag) {if ((j & 1) == 0)save[i][j] = g[i][j / 2];else save[i][j] = g[i + 1][j / 2];}else {if ((j & 1) == 0)save[i][j / 2] = g[i][j];else save[i + 1][j / 2] = g[i][j];}}else {if (flag) {if ((j & 1) == 0)save[i][j] = g[i - 1][n / 2 + j / 2];else save[i][j] = g[i][n / 2 + j / 2];}else {if ((j & 1) == 0)save[i - 1][n / 2 + j / 2] = g[i][j];else save[i][n / 2 + j / 2] = g[i][j];}}}id();}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] == ' ') {tmp[sn] = '\0';change(tmp);sn = 0;}else {tmp[sn++] = str[i];}}tmp[sn] = '\0';change(tmp);}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() {int t;scanf("%d", &t);while (t--) {init();tra();printf("%d\n", solve());if (t) printf("\n");}return 0;}

 

UV-1156 pixel shuffle (Replacement + simulation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.