Chessboard

来源:互联网 发布:淘宝全屏热点代码 编辑:程序博客网 时间:2024/05/16 11:27
Description

When the explorer Mahiro is venturing into the Mountains of Madness, he is caught by the Nyarlathotep, Nyarlathotep wants to play a dead game with Mahiro. Nyarlathotep has created an n×m chessboard, and the chessboard has two kinds of chess pieces--black and white. Nyarlathotep tells Mahiro an operation sequence including three operations:

1. add a white chess piece to position (x, y) (the input will be 1 1 x y)
2. add a black chess piece to position (x, y) (the input will be 1 2 x y)
2. swap the x1 row and x2 row (the input will be 2 x1 x2)

The length of the operation sequence will be q.

Notice: Originally, the chessboard is empty. If you add a chess piece to a position where it’s empty or there is already a chess piece, then this cell only contains the most recent chess piece.

Mahiro should find out the final chessboard as fast as possible, or he will be eaten by Nyarlathotep. Can you help him? Please look at the example for more details.

Input

The first line contains three integers n, m and q (1<=n<=100, 1<=m<=10000, 1<=q<=100000) Then follow q lines are the operations. As for each operation, the format is described above. (1<=xi<=n,1<=yi<=m)

Output

Output n lines each containing m characters. A ‘.’(period) for an empty cell, ‘b’ for a cell with black piece and ‘w’ for a cell with white piece.

Sample Input
 Copy sample input to clipboard 
样例一:2 2 21 1 1 12 1 2样例二:4 3 41 1 1 12 1 21 2 1 12 2 3
Sample Output
样例一:..w.样例二:b.....w.....

Problem Source: “火烈鸟杯”第八届中山大学程序设计新手赛网络预选赛

ps:居然因为这题没过预选赛。。GG
       自己的代码提交时不停报错。。现在还不知道错在哪。。
       不过也学到一点东西,不要顺着题目的想法走下去,往往可以实现优化
比如题目的row interchange,可以用下标映射,只需要交换下标即可


 下面是一个acm队的代码(差距好大。。。。):

#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<iostream>#include<set>#include<string>#define rep(i,j,k) for (i=j;i<=k;i++)#define reps(i,j,k) for (i=j;i>=k;i--)#define mem(a,b) memset(a,b,sizeof(a))#define maxn 3100#define inf 0x3fffffffusing namespacestd;int map[110][11000],n,m,q,id[110];int main(){    int i,j,k;    scanf("%d%d%d",&n,&m,&q);    rep(i,1,n)    rep(j,1,m)map[i][j]=0;    rep(i,1,n)id[i]=i;    rep(i,1,q){        int pos;        scanf("%d",&pos);        if (pos==1){            int col,x,y;            scanf("%d%d%d",&col,&x,&y);            map[id[x]][y]=col;        }        else{            int x,y;            scanf("%d%d",&x,&y);            swap(id[x],id[y]);        }    }    rep(i,1,n){        rep(j,1,m)if (map[id[i]][j]==0)printf(".");        elseif (map[id[i]][j]==1)printf("w");        elseprintf("b");        printf("\n");    }    return0;}




下面是我的:

主要思路就是直接用了string,以及swap,压根没考虑优化,但是提交只告诉是WA,并非TLE,就没往优化方面想,还是自己太菜了。。。c艹

#include <iostream>#include <algorithm>#include <string>using namespacestd;string input[220];int main(){        int N,M,Q;    scanf("%d%d%d",&N,&M,&Q);    for (int n=1;n<=N;n++)        for (int m=0;m<=M;m++)input[n][m]='.';  //若input[n][0]无填充值,后果?    while (Q--)    {        int n,x,y;        scanf("%d",&n);        if (n==2)        {            scanf("%d%d",&x,&y);            swap(input[x],input[y]);        }        elseif (n==1)        {            scanf("%d%d%d",&n,&x,&y);            if (n==1) input[x][y]='w';            elseinput[x][y]='b';        }    }    for (int n=1;n<=N;n++)    {        for (int m=1;m<=M;m++)printf("%c",input[n][m]);        printf("\n");    }    return0;}


0 0
原创粉丝点击