传送门:http://www.ifrog.cc/acm/problem/1046
思路:
显然不可以纯暴力地去怼,只需要一个数组去记录第i行目前真正的在cur[i],然后交换操作只需要改变记录数组的值,实现O(1)交换。复杂度O(q*n)
代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
#define mst(ss,b) memset(ss,b,sizeof(ss));
#define rep(i,k,n) for(int i=k;i<=n;i++)
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
const int N=1e3+10;
int cur[N], g[N][N];
int main(){
int T;
read(T);
while(T--){
int n, m, q;
mst(g, 0);
read(n), read(m), read(q);
rep(i, 1, n)cur[i] = i;
while(q--){
int op, col, x, y;
read(op);
if(op == 1){
read(col), read(x), read(y);
g[cur[x]][y] = col;
}
else{
read(x), read(y);
swap(cur[x], cur[y]);
}
}
rep(i, 1, n){
rep(j, 1, m){
if(g[cur[i]][j] == 0)printf(".");
else if(g[cur[i]][j] == 1)printf("w");
else printf("b");
}
printf("\n");
}
}
return 0;
}
描述:
1046 - chess play
Time Limit:10s Memory Limit:64MByte
Submissions:775Solved:264
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
(1≤n,m≤103,1≤q≤106)(1≤n,m≤103,1≤q≤106). Then follow
qq lines are the operations.As for each operation,
1≤xi,yi≤n1≤xi,yi≤n
OUTPUT
Print n lines which containing m characters - the final chessboard.
SAMPLE INPUT
22 2 21 1 1 12 1 24 3 41 1 1 12 1 21 2 1 12 2 3
SAMPLE OUTPUT
..w.b.....w.....
SOLUTION