“玲珑杯”ACM比赛 Round #4 A -- chess play 【记忆模拟】

来源:互联网 发布:治安管理专业算法学吗 编辑:程序博客网 时间:2024/05/17 02:41

1046 - chess play

Time Limit:10s Memory Limit:64MByte

Submissions:740Solved:252

DESCRIPTION

Bob has a n×mn×m chessboard, which has two kinds of chess pieces.one is black, and the other is white.
As for chessboard, you can do three operations:

  • 1 1 x y1 1 x y (means add a white chess piece to position (x, y))
  • 1 2 x y1 2 x y(means add a black chess piece to position (x, y))
  • 2 x1 x22 x1 x2(means swap the x1x1 row and x2x2 row)

(note: if you add one new chess to a position where there already is a chess, you need throw the old chess away firstly.)
In the initial time the chessboard is empty, and your mission is to show the final chessboard.
Please look at the example for more details.

INPUT
There are multiple test cases.The first line is a number T (T 10T ≤10), which means the number of cases.For each case, the first line contains three integers n, m and q(1n,m103,1q106)(1≤n,m≤103,1≤q≤106). Then follow qq lines are the operations.As for each operation, 1xi,yin1≤xi,yi≤n
OUTPUT
Print n lines which containing m characters - the final chessboard.
SAMPLE INPUT
2
2 2 2
1 1 1 1
2 1 2
4 3 4
1 1 1 1
2 1 2
1 2 1 1
2 2 3
SAMPLE OUTPUT
..w.b.....w.....
SOLUTION
“玲珑杯”ACM比赛 Round #4


原题链接:http://www.ifrog.net/acm/problem/1046

题意:给你一个n*m的棋盘,初始是是空的,接下来有多个操作。

1,1,x,y:加一颗白棋到(x,y)。

1,2,x,y:加一颗黑棋到(x,y)。

2,x1,x2:x1行和y2行交换。

如果加子的地方原来就有棋子,则覆盖。

输出最终的棋盘状态。

官方题解:

#A. 记忆模拟
显然不可以纯暴力地去怼,只需要一个数组去记录第ii行目前真正的在f[i]f[i]行,然后交换操作只需要改变记录数组的值,实现O(1)O(1)交换。

直接模拟肯定TLE。由于只是交换行,所以要用一个数组记录当前行在初始棋盘的位置。

f[x]=y 表示当前第x行是初始状态下的第y行。

AC代码:

/**  * 行有余力,则来刷题!  * 博客链接:http://blog.csdn.net/hurmishine  **/#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=1e3+5;char a[maxn][maxn];int f[maxn];int main(){    //freopen("data.txt","r",stdin);    int T;    cin>>T;    int n,m,t;    while(T--)    {        cin>>n>>m>>t;        //memset(a,'.',sizeof(a));        for(int i=1; i<=n; i++)        {            f[i]=i;            for(int j=1; j<=m; j++)                a[i][j]='.';        }        int p1,p2,x,y;        while(t--)        {            cin>>p1;            if(p1==1)            {                cin>>p2>>x>>y;                if(p2==1)                    a[f[x]][y]='w';                else                    a[f[x]][y]='b';            }            else            {                cin>>x>>y;                swap(f[x],f[y]);            }        }        for(int i=1; i<=n; i++)        {            for(int j=1; j<=m; j++)                cout<<a[f[i]][j];            cout<<endl;        }    }    return 0;}
尊重原创,转载请注明出处:http://blog.csdn.net/hurmishine

0 0