UVa512 - Spreadsheet Tracking

大致思路:

        ①暴力破解。

        ②记录每个格子的初始x-y,在进行cmd时,直接全部遍历每个格子,并修改new_x-new_y。

        ③DR,DC:用 vector 存 label 值,对其排序后,从大到小遍历 vector 中内容,并修改new_x-new_y,若刚好是要删除的行 / 列,new_x=0 / new_y=0;若new_x / new_y > 要删除的行 / 列 ,new_x-- / new_y--。

        ④IC,IR:(同③)用 vector 存 label 值,对其排序后,从大到小遍历 vector 中内容,并修改new_x-new_y,若new_x / new_y >= 要增加的行 / 列 ,new_x++ / new_y++。

        ⑤EX:遍历所有格子,找到满足条件的new_x-new_y,赋给定的值。

        ⑥查找:遍历所有格子,找到满足条件的x-y,输出对应的new_x-new_y(不为0,即存在)。

Bug点:

        存在空白格子包含数的格子进行交换,因此在判断是否增删时,不能仅仅对第一个单元格进行坐标判断

AC代码:
//#define LOCAL 
#include <bits/stdc++.h>
using namespace std;

struct cell{
	int x,y;
	int new_x,new_y;
};
int main(){
	#ifdef LOCAL
	freopen("data.in","r",stdin);
	freopen("data.out","w",stdout);
	#endif
	int text=1;
	while(1){
		int m,n;cin>>m>>n;
		if(m==0&&n==0)break;
		cell c[55][55];
		for(int i=1;i<=m;i++){
			for(int j=1;j<=n;j++){
				c[i][j].x=i;
				c[i][j].y=j;
				c[i][j].new_x=i;
				c[i][j].new_y=j;
			}
		}
		int x;cin>>x;
		for(int i=0;i<x;i++){
			string s;cin>>s;
			if(s=="DR"){//删除 行 
				int temp;cin>>temp;
				vector<int>v;
				for(int j=0;j<temp;j++){
					int x;cin>>x;
					v.push_back(x);
				}
				sort(v.begin(),v.end());
				for(int j=temp-1;j>=0;j--){
					for(int k=m;k>=1;k--){
						for(int l=1;l<=n;l++){
							if(c[k][l].new_x ==v[j]){
								c[k][l].new_x=0;
								c[k][l].new_y=0;
							}else if(c[k][l].new_x>v[j])c[k][l].new_x--;
						} 
					}
				} 
			}else if(s=="DC"){
				int temp;cin>>temp;
				vector<int>v;
				for(int j=0;j<temp;j++){
					int x;cin>>x;
					v.push_back(x);
				}
				sort(v.begin(),v.end());
				for(int j=temp-1;j>=0;j--){
					for(int k=n;k>=1;k--){
						for(int l=1;l<=m;l++){
							if(c[l][k].new_y ==v[j]){
								c[l][k].new_y=0;
								c[l][k].new_x=0;
							}
							else if(c[l][k].new_y>v[j])c[l][k].new_y--;
						} 
					}
				} 
			}else if(s=="IC"){
				int temp;cin>>temp;
				vector<int>v;
				for(int j=0;j<temp;j++){
					int x;cin>>x;
					v.push_back(x);
				}
				sort(v.begin(),v.end());
				for(int j=temp-1;j>=0;j--){
					for(int k=n;k>=1;k--){
						for(int l=1;l<=m;l++){
							 if(c[l][k].new_y>=v[j])c[l][k].new_y++;
						} 
					}
				} 
			}else if(s=="IR"){
				int temp;cin>>temp;
				vector<int>v;
				for(int j=0;j<temp;j++){
					int x;cin>>x;
					v.push_back(x);
				}
				sort(v.begin(),v.end());
				for(int j=temp-1;j>=0;j--){
					for(int k=m;k>=1;k--){
						for(int l=1;l<=n;l++){
							 if(c[k][l].new_x>=v[j])c[k][l].new_x++;
						}
					}
				} 
			}else {
				int x1,y1,x2,y2;
				cin>>x1>>y1>>x2>>y2;
				
				int flag=0;
				for(int i=1;i<=m;i++){
					for(int j=1;j<=n;j++){
						if(c[i][j].new_x ==x1&&c[i][j].new_y==y1 ){
							c[i][j].new_x=x2;c[i][j].new_y=y2;
							flag++;
						}else if(c[i][j].new_x ==x2&&c[i][j].new_y==y2 ){
							c[i][j].new_x=x1;c[i][j].new_y=y1;
							flag++;
						}
					}
					if(flag==2)break;
				}
			}
		}
		if(text>1)cout<<endl;
		cout<<"Spreadsheet #"<<text<<endl;
		text++;
		int data;cin>>data;
		for(int i=0;i<data;i++){
			int a,b;cin>>a>>b;
			int flag=0;
			int x1=0,y1=0;
			for(int i=1;i<=m;i++){
				for(int j=1;j<=n;j++){
					if(c[i][j].x ==a&&c[i][j].y ==b){
						flag=1;
						x1=c[i][j].new_x;y1=c[i][j].new_y;
						break;
					}
				}
				if(flag)break;
			}
			if(flag&&x1&&y1)cout<<"Cell data in ("<<a<<","<<b<<") moved to ("<<x1<<","<<y1<<")\n";
			else cout<<"Cell data in ("<<a<<","<<b<<") GONE\n";
		}
	}
} 

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值