Data in spreadsheets are stored in cells, which are organized in rows (r) and columns (c). Some operations on spreadsheets can be applied to single cells (r,c), while others
can be applied to entire rows or columns. Typical cell operations include inserting and deleting rows or columns and exchanging cell contents.

Some spreadsheets allow users to mark collections of rows or columns for deletion, so the entire collection can be deleted at once. Some (unusual) spreadsheets allow users to mark collections of rows or columns for insertions too. Issuing an insertion command
results in new rows or columns being inserted before each of the marked rows or columns. Suppose, for example, the user marks rows 1 and 5 of the spreadsheet on the left for deletion. The spreadsheet then shrinks to the one on the right.

If the user subsequently marks columns 3, 6, 7, and 9 for deletion, the spreadsheet shrinks to this.

1 2 3 4 5
1 2 24 8 22 16
2 18 19 21 22 25
3 24 25 67 22 71
4 16 12 10 22 58
5 33 34 36 22 40

If the user marks rows 2, 3 and 5 for insertion, the spreadsheet grows to the one on the left. If the user then marks column 3 for insertion, the spreadsheet grows to the one in the middle. Finally, if the
user exchanges the contents of cell (1,2) and cell (6,5), the spreadsheet looks like the one on the right.

You must write tracking software that determines the final location of data in spreadsheets that result from row, column, and exchange operations similar to the ones illustrated here.

Input

The input consists of a sequence of spreadsheets, operations on those spreadsheets, and queries about them. Each spreadsheet definition begins with a pair of integers specifying its initial number of rows
(r) and columns (c),
followed by an integer specifying the number (n) of spreadsheet operations. Row and column labeling begins with 1. The maximum number
of rows or columns of each spreadsheet is limited to 50. The following n lines specify the desired operations.

An operation to exchange the contents of cell (r1c1) with the contents of cell (r2,c2) is given by:

EX r1 c1 r2 c2

The four insert and delete commands--DC (delete columns), DR (delete rows), IC (insert columns), and IR (insert rows) are given by:

<commandA x1 x2  xA

where <command> is one of the four commands; A is a positive integer less than 10, and are the labels of
the columns or rows to be deleted or inserted before. For each insert and delete command, the order of the rows or columns in the command has no significance. Within a single delete or insert command, labels will be unique.

The operations are followed by an integer which is the number of queries for the spreadsheet. Each query consists of positive integers r and c, representing the row and column number of a cell in the original spreadsheet. For each query, your
program must determine the current location of the data that was originally in cell (rc). The end of input is indicated by a row consisting of a pair of zeros for the spreadsheet dimensions.

Output

For each spreadsheet, your program must output its sequence number (starting at 1). For each query, your program must output the original cell location followed by the final location of the data or the
word GONE if the contents of the original cell location were destroyed as a result of the operations. Separate output from different spreadsheets with a blank line.

The data file will not contain a sequence of commands that will cause the spreadsheet to exceed the maximum size.

Sample Input

7 9
5
DR   2  1 5
DC  4  3 6 7 9
IC  1  3
IR  2  2 4
EX 1 2 6 5
4
4 8
5 5
7 8
6 5
0 0

Sample Output

Spreadsheet #1
Cell data in (4,8) moved to (4,6)
Cell data in (5,5) GONE
Cell data in (7,8) moved to (7,6)
Cell data in (6,5) moved to (1,2)

先把命令保存下来,每次询问之后对询问位置执行一次操作,效率也不错。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>

using namespace std;

const int maxd = 10000;

struct Command
{
	char c[5];
	int r1, c1, r2, c2;
	int a, x[20];
} cmd[maxd];
int r, c, n;

int simulate(int& r0, int& c0)
{
	for (int i = 0; i < n; i++) {
		if (cmd[i].c[0] == 'E') {
			if (cmd[i].r1 == r0 && cmd[i].c1 == c0) {
				r0 = cmd[i].r2;
				c0 = cmd[i].c2;
			}
			else if (cmd[i].r2 == r0 && cmd[i].c2 == c0) {
				r0 = cmd[i].r1;
				c0 = cmd[i].c1;
			}
		}
		else {
			int dr = 0, dc = 0;
			for (int j = 0; j < cmd[i].a; j++) {
				int x = cmd[i].x[j];
				if (cmd[i].c[0] == 'I') {
					if (cmd[i].c[1] == 'R' && x <= r0) {
						dr++;
					}
					if (cmd[i].c[1] == 'C' && x <= c0) {
						dc++;
					}
				}
				else {
					if (cmd[i].c[1] == 'R' && x == r0) {
						return 0;
					}
					if (cmd[i].c[1] == 'C' && x == c0) {
						return 0;
					}
					if (cmd[i].c[1] == 'R' && x < r0) {
						dr--;
					}
					if (cmd[i].c[1] == 'C' && x < c0) {
						dc--;
					}
				}
			}
			r0 += dr;
			c0 += dc;
		}
	}
	return 1;
}

int main()
{
	int r0, c0, q, kase = 0;
	while (scanf("%d%d", &r, &c) == 2 && r) {
		scanf("%d", &n);
		// 把命令记录下来
		for (int i = 0; i < n; i++) {
			scanf("%s", cmd[i].c);
			if (cmd[i].c[0] == 'E') {
				scanf("%d%d%d%d", &cmd[i].r1, &cmd[i].c1, &cmd[i].r2, &cmd[i].c2);
			}
			else {
				scanf("%d", &cmd[i].a);
				for (int j = 0; j < cmd[i].a; j++) {
					scanf("%d", &cmd[i].x[j]);
				}
			}
		}
		if (kase > 0) printf("\n");
		printf("Spreadsheet #%d\n", ++kase);
		scanf("%d", &q);
		while (q--) {
			scanf("%d%d", &r0, &c0);
			printf("Cell data in (%d,%d) ", r0, c0);
			// 对每个查找进行一次完整的操作
			if (!simulate(r0, c0)) {
				printf("GONE\n");
			}
			else {
				printf("moved to (%d,%d)\n", r0, c0);
			}
		}
	}

	return 0;
}

Uva - 512 - Spreadsheet Tracking的更多相关文章

  1. Spreadsheet Tracking

     Spreadsheet Tracking  Data in spreadsheets are stored in cells, which are organized in rows (r) and ...

  2. 【例题 4-5 uva 512】Spreadsheet Tracking

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每个操作对与一个点来说变化是固定的. 因此可以不用对整个数组进行操作. 对于每个询问,遍历所有的操作.对输入的(x,y)进行相应的变 ...

  3. uva 512

    1. 问题 不知道怎么存储操作 看代码注释,else if等 2. 代码 #include <iostream> #include <stdio.h> #include < ...

  4. 踪电子表格中的单元格(Spreadsheet Tracking, ACM/ICPC World Finals 1997, UVa512)

    有一个r行c列(1≤r,c≤50)的电子表格,行从上到下编号为1-r,列从左到右编号为1 -c.如图4-2(a)所示,如果先删除第1.5行,然后删除第3, 6, 7, 9列,结果如图4-2(b) 所示 ...

  5. UVA 215 Spreadsheet Calculator (模拟)

    模拟题.每个单元格有表达式就dfs,如果有环那么就不能解析,可能会重复访问到不能解析的单元格,丢set里或者数组判下重复. 这种题首先框架要对,变量名不要取的太乱,细节比较多,知道的库函数越多越容易写 ...

  6. 思维水题:UVa512-Spreadsheet Tracking

    Spreadsheet Tracking Data in spreadsheets are stored in cells, which are organized in rows (r) and c ...

  7. Spreadsheet Calculator 电子表格计算器 (Uva 215)

    原题:https://uva.onlinejudge.org/external/2/215.pdf 有一个M x N的表格,每个单元格是个数字或者表达式.表达式由单元格编号和+ - 号组成 输出单元格 ...

  8. Learning a Deep Compact Image Representation for Visual Tracking

    这篇博客对论文进行了部分翻译http://blog.csdn.net/vintage_1/article/details/19546953,不过个人觉得博主有些理解有误. 这篇博客简单分析了代码htt ...

  9. CUDA Samples: Ray Tracking

    以下CUDA sample是分别用C++和CUDA实现的生成光线跟踪图像,并对其中使用到的CUDA函数进行了解说,code参考了<GPU高性能编程CUDA实战>一书的第六章,CUDA各实现 ...

随机推荐

  1. django rest-framework 4.REST的认证和权限

    目前,我们的API对谁可以编辑或删除代码段没有任何限制.我们想要一些更先进的行为,以确保:(这段话抄自官网) 代码段始终与创建者相关联. 只有身份验证的用户可以创建片段. 只有片段的创建者可以更新或删 ...

  2. Linux学习之CentOS(十)----Linux 的账号与群组

    Linux 的账号与群组 管理员的工作中,相当重要的一环就是『管理账号』啦!因为整个系统都是你在管理的, 并且所有一般用户的账号申请,都必须要透过你的协助才行!所以你就必须要了解一下如何管理好一个服务 ...

  3. 【python标准库模块三】Os模块和Sys模块学习

    Os模块 导入os模块 import os 获取当前工作目录 os.getcwd() 切换目录,跟linux中的cd一样 os.chdir("文件夹名") 递归生成文件夹 os.m ...

  4. 通讯协议序列化解读(二) protostuff详解教程

    上一篇文章 通讯协议序列化解读(一):http://www.cnblogs.com/tohxyblog/p/8974641.html  前言:上一面文章我们介绍了java序列化,以及谷歌protobu ...

  5. linux和android开发链接

    1.Tracy Mcgrady的专栏冰山一角:linux和Android底层开发,主要是mtk系列点击打开链接 2.郁闷Wednesday:嵌入式linux 单片机 android,点击打开链接 3. ...

  6. Linux测量kernel子模块加载时间的方法

    1. 在文件kernel/init/main.c里面,在接口do_one_initcall( )中,将initcall_debug设置为true,然后编译boot.img 2. 使用adb shell ...

  7. Xcode 中的断言

    转自:http://weibo.com/p/100808885591f113cdedc3301794e5e7d7e9f0/home?from=page_100808&mod=TAB#_rnd1 ...

  8. Android的Ui层次

    UI 概览 Android 应用中的所有用户界面元素都是使用 View 和 ViewGroup 对象构建而成.View 对象用于在屏幕上绘制可供用户交互的内容.ViewGroup 对象用于储存其他 V ...

  9. 剑指Offer——中国银行面试知识储备

    剑指Offer--中国银行面试知识储备+面试内容 事件介绍 时间:2016.11.23 08:30 地点:北京市海淀区永丰路299号南门(中国银行软件中心) 事件:中国银行面试(中英文面试) 注意事项 ...

  10. (Java)微信之个人公众账号开发(二)——接收并处理用户消息(下)

    接下来,我们再讲一下图文消息: 如图: 大家可以先从开发者文档中了解一下图文消息的一些参数: 如上图,用户回复4时,ipastor返回了几条图文消息,上图中属于多图文消息,当然还有单图文消息,图文消息 ...