Last active
October 27, 2018 04:27
-
-
Save WindAzure/46ad3eb3f953446c27e559b323fbe828 to your computer and use it in GitHub Desktop.
UVa 512
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <stdio.h> | |
#include <string.h> | |
#include <tuple> | |
#include <vector> | |
using namespace std; | |
auto originSpreadsheetRow = 0; | |
auto originSpreadsheetCol = 0; | |
vector<vector<int>> spreadsheet; | |
void initializeSpredsheet() | |
{ | |
spreadsheet.clear(); | |
auto elementData = 0; | |
for (auto rowIndex = 0; rowIndex < originSpreadsheetRow; rowIndex++) | |
{ | |
auto row = vector<int>{}; | |
for (auto colIndex = 0; colIndex < originSpreadsheetCol; colIndex++) | |
{ | |
row.push_back(elementData++); | |
} | |
spreadsheet.push_back(row); | |
} | |
} | |
void arrangeTargetList(vector<int> &targetList, int boundary, int increase) | |
{ | |
sort(targetList.begin(), targetList.end()); | |
auto effectiveNumber = 0; | |
vector<int> resultTargetList; | |
for (auto &target : targetList) | |
{ | |
resultTargetList.push_back(target + increase * effectiveNumber); | |
effectiveNumber++; | |
} | |
targetList = resultTargetList; | |
} | |
void modifyColumns(bool isInsertColumn, vector<int> &columnList) | |
{ | |
for (auto column : columnList) | |
{ | |
for (auto &row : spreadsheet) | |
{ | |
if (isInsertColumn) | |
{ | |
row.insert(row.begin() + column, -1); | |
} | |
else | |
{ | |
row.erase(row.begin() + column); | |
} | |
} | |
} | |
} | |
void modifyRows(bool isInsertRow, vector<int> &rowList) | |
{ | |
vector<int> newRow(spreadsheet[0].size(), -1); | |
for (auto row : rowList) | |
{ | |
if (isInsertRow) | |
{ | |
spreadsheet.insert(spreadsheet.begin() + row, newRow); | |
} | |
else | |
{ | |
spreadsheet.erase(spreadsheet.begin() + row); | |
} | |
} | |
} | |
void runAction(char operation[5], vector<int> &targetList) | |
{ | |
auto isInsert = (operation[0] == 'I'); | |
auto boundary = (operation[1] == 'C') ? spreadsheet[0].size() : spreadsheet.size(); | |
auto offset = isInsert ? 1 : -1; | |
arrangeTargetList(targetList, boundary, offset); | |
if (operation[1] == 'C') | |
{ | |
modifyColumns(isInsert, targetList); | |
} | |
else | |
{ | |
modifyRows(isInsert, targetList); | |
} | |
} | |
void insertOperation() | |
{ | |
char operation[5]; | |
scanf("%s", operation); | |
if (strcmp(operation, "EX") == 0) | |
{ | |
auto srcRow = 0, srcCol = 0; | |
auto dstRow = 0, dstCol = 0; | |
scanf("%d%d%d%d", &srcRow, &srcCol, &dstRow, &dstCol); | |
swap(spreadsheet[srcRow - 1][srcCol - 1], spreadsheet[dstRow - 1][dstCol - 1]); | |
} | |
else | |
{ | |
auto targetQuantity = 0; | |
auto targetList = vector<int>{}; | |
scanf("%d", &targetQuantity); | |
for (auto i = 0; i < targetQuantity; i++) | |
{ | |
auto target = 0; | |
scanf("%d", &target); | |
targetList.push_back(target - 1); | |
} | |
runAction(operation, targetList); | |
} | |
} | |
tuple<bool, int, int> queryCell(int queryRow, int queryCol) | |
{ | |
auto targetNumber = (queryRow - 1) * originSpreadsheetCol + (queryCol - 1); | |
auto spreadshsheetRow = spreadsheet.size(); | |
auto spreadshsheetCol = spreadsheet[0].size(); | |
for (auto row = 0; row < spreadshsheetRow; row++) | |
{ | |
for (auto col = 0; col < spreadshsheetCol; col++) | |
{ | |
if (spreadsheet[row][col] == targetNumber) | |
{ | |
return make_tuple(true, row + 1, col + 1); | |
} | |
} | |
} | |
return make_tuple(false, 0, 0); | |
} | |
void queryOperation() | |
{ | |
auto queryTimes = 0; | |
scanf("%d", &queryTimes); | |
for (auto i = 0; i < queryTimes; i++) | |
{ | |
auto queryRow = 0, queryCol = 0; | |
scanf("%d%d", &queryRow, &queryCol); | |
printf("Cell data in (%d,%d) ", queryRow, queryCol); | |
auto result = queryCell(queryRow, queryCol); | |
if (get<0>(result)) | |
{ | |
printf("moved to (%d,%d)\n", get<1>(result), get<2>(result)); | |
} | |
else | |
{ | |
puts("GONE"); | |
} | |
} | |
} | |
int main() | |
{ | |
auto T = 1; | |
while (~scanf("%d%d", &originSpreadsheetRow, &originSpreadsheetCol)) | |
{ | |
if (originSpreadsheetRow == 0 && originSpreadsheetCol == 0) break; | |
initializeSpredsheet(); | |
auto operationTimes = 0; | |
scanf("%d", &operationTimes); | |
for (auto i = 0; i < operationTimes; i++) | |
{ | |
insertOperation(); | |
} | |
if (T != 1) printf("\n"); | |
printf("Spreadsheet #%d\n", T++); | |
queryOperation(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment