UVA-512 Spreadsheet Tracking

題目鏈接Spreadsheet Tracking

題目大意:模擬Excel,進行插入(刪除)行、列以及尋找原始數據在最終表格的位置。

解題思路:由於要進行插入和刪除的操作,用二維數組的話實現非常麻煩,所以直接用vector套vector就方便多了,vector容器中的erase和insert方法用起來不能再爽。在插入和刪除操作時注意優先操作行(列)大的。


代碼如下:

#include <map>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int inf = 0x3f3f3f3f;
const int maxn = 2e4 + 15;

vector< vector<P> > Excel;
vector<int> temp;

int r, c;
void Init(){
    for(int i = 0; i < Excel.size(); i++)   
        Excel.clear();
    for(int i = 0; i < r; i++){
        vector<P> row;
        Excel.push_back(row);
        for(int j = 0; j < c; j++){
            Excel[i].push_back(P(i + 1, j + 1));
        }
    }
    return;
}

void DeleteRow(int n){
    for(int i = 0; i < r; i++){
        if(i == n - 1){
            Excel.erase(Excel.begin() + i);
            break;
        }
    }
    r--;
    return;
}

void DeleteColumn(int n){
    for(int i = 0; i < r; i++)
        Excel[i].erase(Excel[i].begin() + n - 1);
    c--;
    return;
}

void InsertRow(int n){
    vector<P> row;
    for(int i = 0; i < c; i++)
        row.push_back(P(-1, -1));
    Excel.insert(Excel.begin() + n - 1, row);
    r++;
    return; 
}

void InsertColumn(int n){
    for(int i = 0; i < r; i++){
        Excel[i].insert(Excel[i].begin() + n - 1, P(-1, -1));
    }
    c++;
    return; 
}

void Exchange(int x1, int y1, int x2, int y2){
    x1--; x2--; y1--; y2--;
    P t;
    t = Excel[x1][y1];
    Excel[x1][y1] = Excel[x2][y2];
    Excel[x2][y2] = t; 
    return;
}

void Find(int x, int y){
    int ansx = -1, ansy = -1;
    for(int i = 0; i < r; i++){
        for(int j = 0; j < c; j++){
            if(P(x, y) == Excel[i][j]){
                ansx = i + 1;
                ansy = j + 1;
                break;
            }   
        }
    }
    if(ansx == -1){
        cout << "Cell data in " << "(" << x << "," << y << ")" << " GONE" << endl;  
    }
    else{       
        cout << "Cell data in ";
        cout << "(" << x << "," << y << ")";
        cout << " moved to "; 
        cout << "(" << ansx << "," << ansy << ")" << endl;
    }
    return;
}

int main(){
    ios::sync_with_stdio(false);
    int on, x, y, n, m, op, x1, y1, x2, y2;
    string ope;
    int kase = 1;
    while(cin >> r >> c){
                if(!r && !c)
            break;
        if(kase != 1){
            cout << endl;
            cout << "Spreadsheet #" << kase << endl;
        }
        else
            cout << "Spreadsheet #" << kase << endl;
        kase++;

        Init();
        cin >> on;
        while(on--){
            cin >> ope;
            if(ope == "DR"){
                cin >> m;
                temp.clear();
                while(m--){
                    cin >> op;
                    temp.push_back(op);
                }
                sort(temp.begin(), temp.end(), greater<int>() );
                for(int i = 0; i < temp.size(); i++){
                    DeleteRow(temp[i]);
                }
            }
            else if(ope == "DC"){
                cin >> m;
                temp.clear();
                while(m--){
                    cin >> op;
                    temp.push_back(op);
                }
                sort(temp.begin(), temp.end(), greater<int>() );
                for(int i = 0; i < temp.size(); i++){
                    DeleteColumn(temp[i]);
                }
            }
            else if(ope == "IC"){
                cin >> m;
                temp.clear();
                while(m--){
                    cin >> op;
                    temp.push_back(op);
                }
                sort(temp.begin(), temp.end(), greater<int>() );
                for(int i = 0; i < temp.size(); i++){
                    InsertColumn(temp[i]);
                }                               
            }
            else if(ope == "IR"){
                cin >> m;
                temp.clear();
                while(m--){
                    cin >> op;
                    temp.push_back(op);
                }
                sort(temp.begin(), temp.end(), greater<int>() );
                for(int i = 0; i < temp.size(); i++){
                    InsertRow(temp[i]);
                }                   
            }               
            else if(ope == "EX"){
                cin >> x1 >> y1 >> x2 >> y2;
                Exchange(x1, y1, x2, y2);               
            }
        }
        cin >> n;
        while(n--){
            cin >> x >> y;
            Find(x, y); 
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章