程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 3106Flip and Turn(模擬)

POJ 3106Flip and Turn(模擬)

編輯:C++入門知識

Flip and Turn Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 957 Accepted: 330

Description

Let us define a set of operations on a rectangular matrix of printable characters.

A matrix A with m rows (1-st index) and n columns (2-nd index) is given. The resulting matrix B is defined as follows.

Transposition by the main diagonal (operation identifier is ‘1’): Bj,i = Ai,jTransposition by the second diagonal (‘2’): Bn?j+1,m?i+1 = Ai,jHorizontal flip (‘H’): Bm?i+1,j = Ai,jVertical flip (‘V’): Bi,n?j+1 = Ai,jRotation by 90 (‘A’), 180 (‘B’), or 270 (‘C’) degrees clockwise; 90 degrees case: Bj,m?i+1 = Ai,jRotation by 90 (‘X’), 180 (‘Y’), or 270 (‘Z’) degrees counterclockwise; 90 degrees case: Bn?j+1,i = Ai,j

You are given a sequence of no more than 100 000 operations from the set. Apply the operations to the given matrix and output the resulting matrix.

Input

At the first line of the input file there are two integer numbers — m and n (0 < m, n ≤ 300). Then there are m lines with n printable characters per line (we define a printable character as a symbol with ASCII code from 33 to 126 inclusive). There will be no additional symbols at these lines.

The next line contains the sequence operations to be performed, specified by their one-character identifiers. The operations should be performed from left to right.

Output

Two integer numbers, the number of rows and columns in the output matrix. Then the output matrix must follow, in the same format as the input one.

Sample Input

3 4
0000
a0b0
cdef
A1

Sample Output

3 4
cdef
a0b0
0000


題目大意:給你一個m*n的矩陣,通過順時針旋轉,逆時針旋轉,水平翻轉,垂直翻轉,對角線旋轉,反對角線旋轉變換得到新矩陣。但是不能直接對原矩陣操作,時間復雜度為O(10^5*10^5),會超時,但是如何保存變化的過程呢,我們不需要知道變化過程,只需要知道結果即可。由於旋轉翻轉都是中心對稱的,可以采用一個2*2的小正方形記錄變化情況,然後小矩陣變化得出最終結果之後再相應變化大矩陣。時間復雜度為O(10^5*5)
題目地址:Flip and Turn
AC代碼:
#include
#include
#include
#include
#include
#include
using namespace std;
char a[305][305];
char b[305][305];
char str[100005];
int m,n;
int tb[3][3];

void con1()
{
    int i,j;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            b[i][j]=a[j][i];
        }
    }

    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
            a[i][j]=b[i][j];
        a[i][j]='\0';
    }
    swap(n,m);
}

void con2()
{
    int i,j;
    for(i=1; i<=m; i++)
    {
        for(j=1; j<=n; j++)
        {
            b[n-j+1][m-i+1]=a[i][j];
        }
    }

    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
            a[i][j]=b[i][j];
        a[i][j]='\0';
    }
    swap(n,m);
}

void con3()
{
    int i,j;
    for(i=1; i<=m; i++)
    {
        for(j=1; j<=n; j++)
        {
            b[m-i+1][j]=a[i][j];
        }
    }

    for(i=1; i<=m; i++)
    {
        for(j=1; j<=n; j++)
            a[i][j]=b[i][j];
        a[i][j]='\0';
    }
}

void con4()
{
    int i,j;
    for(i=1; i<=m; i++)
    {
        for(j=1; j<=n; j++)
        {
            b[i][n-j+1]=a[i][j];
        }
    }

    for(i=1; i<=m; i++)
    {
        for(j=1; j<=n; j++)
            a[i][j]=b[i][j];
        a[i][j]='\0';
    }
}

void con5()
{
    int i,j;
    for(i=1; i<=m; i++)
    {
        for(j=1; j<=n; j++)
        {
            b[j][m-i+1]=a[i][j];
        }
    }

    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
            a[i][j]=b[i][j];
        a[i][j]='\0';
    }
    swap(m,n);
}

void con6()
{
    int i,j;
    for(i=1; i<=m; i++)
    {
        for(j=1; j<=n; j++)
        {
            b[n-j+1][i]=a[i][j];
        }
    }

    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
            a[i][j]=b[i][j];
        a[i][j]='\0';
    }
    swap(m,n);
}

void debug()
{
    int i,j;
    for(i=1;i<=2;i++)
    {
        for(j=1;j<=2;j++)
            cout<='A'&&str[i]<='C')
            {
                int s=str[i]-'A'+1;
                for(j=0;j='X'&&str[i]<='Z')
            {
                int s=str[i]-'X'+1;
                for(j=0;j


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved