poj 3106 模拟+栈优化

来源:互联网 发布:io域名申请 编辑:程序博客网 时间:2024/06/11 09:40

因为傻逼错误debug了好长时间┳_┳  题目链接点这儿

题目来源Northeastern Europe 2005

题目不长,我把叙述都搬过来了。。

Flip and Turn
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 997 Accepted: 342

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,j
  • Transposition by the second diagonal (‘2’): Bnj+1,mi+1 = Ai,j
  • Horizontal flip (‘H’): Bmi+1,j = Ai,j
  • Vertical flip (‘V’): Bi,nj+1 = Ai,j
  • Rotation by 90 (‘A’), 180 (‘B’), or 270 (‘C’) degrees clockwise; 90 degrees case: Bj,mi+1 = Ai,j
  • Rotation by 90 (‘X’), 180 (‘Y’), or 270 (‘Z’) degrees counterclockwise; 90 degrees case: Bnj+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 < mn ≤ 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 40000a0b0cdefA1

Sample Output

3 4cdefa0b00000

就是模拟一个矩阵的各种翻转、旋转操作,网上貌似只有一种通过操作一个2*2小矩阵,最后通过这个小矩阵的状态来确定大矩阵状态的方法。这里采用另一种方法。


事实上,所有的操作都可以由沿主对角线翻转和沿中垂线翻转两种操作组成,所以,我们可以将每种操作都拆成10串。而且如果有两个1或者两个0放在一起,那么两者的作用会抵消掉。所以,我们可以用一个栈来维护现有的操作序列,如果将要压入栈中的操作和栈顶的操作相同,那么就将栈顶元素pop出去。


而且注意到操作数满8个(10101010或01010101)时,矩阵会恢复原状态,所以我们只需要取出栈顶的几个元素即可。


下面放出代码,姿势相当不优美,会考虑优化。

//winoros3106Accepted1000K94MSG++3214B2014-04-05 21:47:06#include <vector>#include <list>#include <map>#include <set>#include <deque>#include <queue>#include <stack>#include <bitset>#include <algorithm>#include <functional>#include <numeric>#include <utility>#include <sstream>#include <iostream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <cctype>#include <string>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <ctime>#define max(a,b) ((a)>(b)?(a):(b))#define min(a,b) ((a)>(b)?(b):(a))#define rep(i,initial_n,end_n) for(int (i)=(initial_n);(i)<(end_n);i++)#define repp(i,initial_n,end_n) for(int (i)=(initial_n);(i)<=(end_n);(i)++)#define reep(i,initial_n,end_n) for((i)=(initial_n);(i)<(end_n);i++)#define reepp(i,initial_n,end_n) for((i)=(initial_n);(i)<=(end_n);(i)++)#define eps 1.0e-9#define MAX_N 1010using namespace std;typedef pair<int, int> pii;typedef pair<double, double> pdd;typedef long long ll;typedef unsigned long long ull;stack<int> s, ss;char atlas[310][310], a[1000010], b[310][310];int main() {    int n, m;    while(scanf("%d%d", &n, &m)>0) {    rep(i, 0, n) scanf("%s", atlas[i]);    scanf("%s", a);    int len = strlen(a);    rep(i, 0, len) {        int top = -1, j;        if(!s.empty()) top = s.top();        if(a[i] == '1') {            j = 1;            if(top == 0) for(; j > 0 && !s.empty(); j--) s.pop();            while(j--) s.push(j % 2);        }        else if(a[i] == '2') {            j = 3;            if(top == 1) for(; j > 0 && !s.empty(); j--) s.pop();            while(j--) s.push((j+1)%2);        }        else if(a[i] == 'H') {            j = 3;            if(top == 0) for(; j > 0 && !s.empty(); j--) s.pop();            while(j--) s.push(j%2);        }        else if(a[i] == 'V') {            if(top != 1) s.push(1);            else s.pop();        }        else if(a[i] == 'A' || a[i] == 'Z') {            j = 2;            if(top == 0) for(; j > 0 && !s.empty(); j--) s.pop();            while(j--) s.push((j+1)%2);        }        else if(a[i] == 'B' || a[i] == 'Y') {            j = 4;            if(top == 0) for(; j > 0 && !s.empty(); j--) s.pop();            while(j--) s.push((j+1)%2);        }        else {            j = 2;            if(top == 1) for(; j > 0 && !s.empty(); j--) s.pop();            while(j--) s.push(j%2);        }    }    len = s.size();    len %= 8;    rep(i, 0, len) ss.push(s.top()), s.pop();    rep(i, 0, len) {        int j = ss.top(); ss.pop();        if(j == 0) {            rep(ii, 0, m) {                rep(jj, 0, n) {                b[ii][jj] = atlas[jj][ii];                }            b[ii][n] = 0;            }            swap(atlas, b);            swap(n, m);        }        if(j == 1) {            rep(ii, 0, n) {                rep(jj, 0, m) {                    b[ii][jj] = atlas[ii][m-1-jj];                }                b[ii][m] = 0;            }            swap(atlas, b);        }    }    printf("%d %d\n", n, m);    rep(i, 0, n) puts(atlas[i]);    }    return 0;}


0 0
原创粉丝点击