“玲珑杯”ACM比赛 Round #4 A -- chess play 【记忆模拟】
来源:互联网 发布:治安管理专业算法学吗 编辑:程序博客网 时间:2024/05/17 02:41
Time Limit:10s Memory Limit:64MByte
Submissions:740Solved:252
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.
原题链接: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
- “玲珑杯”ACM比赛 Round #4 A -- chess play 【记忆模拟】
- “玲珑杯”ACM比赛 Round #4 A -- chess play
- 玲珑比赛 A -- chess play【记忆模拟】
- “玲珑杯”ACM比赛 Round #15 A
- “玲珑杯”ACM比赛 Round #4
- “玲珑杯”ACM比赛 Round #10-A-Black and White
- “玲珑杯”ACM比赛 Round #18 A(穷举)
- 玲珑杯”ACM比赛 Round #19 A simple math problem
- 玲珑杯-【 chess play】
- “玲珑杯”ACM比赛 Round #4 G -- See car
- “玲珑杯”ACM比赛 Round #4 E -- array(dp)
- “玲珑杯”ACM比赛 Round #4 E题
- 玲珑杯”ACM比赛 Round #4 E -- array【DP】
- 玲珑杯”ACM比赛 Round #4 G -- See car【Set】
- “玲珑杯”ACM比赛 Round #4 B Best couple
- 玲珑杯”ACM比赛 Round #5
- “玲珑杯”ACM比赛 Round #7
- “玲珑杯”ACM比赛 Round #7
- 数据库方面的面试题
- 《锋利的JQuery》读书笔记——第六章
- jquery中prop()方法和attr()方法的区别
- 利用 别人修改的 CAS 需注意如下问题
- itop4412 LCD设备驱动详解(二)之DRIVER
- “玲珑杯”ACM比赛 Round #4 A -- chess play 【记忆模拟】
- 利用浮动窗口提示用户操作
- 访问者模式
- windows下redis安装与配置
- seajs 配置
- 第一次接触Linux
- 文章标题
- 线性表的链式存储和实现
- 刹那光华,一闪而逝