uva 101(线性表)
来源:互联网 发布:傲飞数据整合平台下载 编辑:程序博客网 时间:2024/05/16 10:40
题解:用栈的数组存木块,然后设置一个数组储存每个木块当前所在的位置,每次移动都要更新,注意不要把位置的储存成了值,另外,如果a和b木块在同一个位置就不需要移动。
例如:
sample input
2
move 0 over 1
move 1 over 0
quit
输出结果是
0:
1: 1 0
#include <cstdio>#include <cstring>#include <stack>using namespace std;const int N = 30;stack<int> s[N];int flag[N];void init (int pl, int bl) {while (s[pl].top() != bl) {int temp = s[pl].top();flag[temp] = temp;s[temp].push(temp);s[pl].pop();}return;}void change (int bl1, int bl2) {stack<int> temp;int flag1 = flag[bl1];while (s[flag1].top() != bl1) {temp.push(s[flag1].top());s[flag1].pop();}temp.push(s[flag1].top());s[flag1].pop();int flag2 = flag[bl2];while (!temp.empty()) {s[flag2].push(temp.top());flag[temp.top()] = flag2;temp.pop();}}int main() {int n, bl1, bl2;char keyw1[N], keyw2[N];scanf("%d", &n);for (int i = 0; i < n; i++) {s[i].push(i);flag[i] = i;}getchar();while (1) {scanf("%s", keyw1);if (keyw1[0] == 'q')break;scanf("%d%s%d", &bl1, keyw2, &bl2);if (keyw1[0] == 'm') {if (keyw2[1] == 'n' && flag[bl1] != flag[bl2]) {int temp2 = flag[bl2];init(temp2, bl2);int temp1 = flag[bl1];init(temp1, bl1);s[temp2].push(bl1);s[temp1].pop();flag[bl1] = temp2;}else if (flag[bl1] != flag[bl2]){int temp1 = flag[bl1];init(temp1, bl1);int temp2 = flag[bl2];s[temp2].push(bl1);s[temp1].pop();flag[bl1] = temp2;}}else {if (keyw2[1] == 'n' && flag[bl1] != flag[bl2]) {int temp2 = flag[bl2];init(temp2, bl2);change(bl1, bl2);}else {if (flag[bl1] != bl2)change(bl1, bl2);}}}for (int i = 0; i < n; i++) {printf("%d:", i);if (!s[i].empty()) {stack<int> temp;while (!s[i].empty()) {temp.push(s[i].top());s[i].pop();}while (!temp.empty()) {printf(" %d", temp.top());temp.pop();}printf("\n");}elseprintf("\n");}return 0;}
0 0
- uva 101(线性表)
- uva 673(线性表)
- uva 133(线性表)
- uva 127(线性表)
- uva 10152(线性表)
- uva 442(线性表)
- uva 11111(线性表)
- uva 540(线性表)
- uva 10050(线性表)
- Uva 10152 - ShellSort//排序,线性表
- uva 11234(二叉树、线性表)
- UVa 127 线性表模拟题
- UVA 101 (暑期-线性表-D-The Blocks Problem)
- Uva 11584 线性DP
- UVA - 242 线性DP
- Uva 算法入门经典(数据结构基础)线性表题目
- uva-133绝对是史上最简单的线性表题目
- UVA 673 (暑假-线性表 -A - Parentheses Balance)
- MFC怎么给对话框添加背景
- 各开源框架使用与设计总结 - 混混人生
- MySQL 主从复制 详细讲解(一)
- 表格提交 前台校验单元格不能为空且为数字
- Hibernate缓存
- uva 101(线性表)
- U-boot在S3C2440上的移植详解
- hdu1875 畅通工程再续
- RequireJS入门
- C# Java间进行RSA加密解密交互(三)
- JAVA内存分配全面解析
- C# 判断32位还是64位系统的一个简单方法(当然目标平台不能是x86)
- libUV 写TCPServer
- java xipin 111qqqq