UVA227解题报告
来源:互联网 发布:ios虚拟定位软件 编辑:程序博客网 时间:2024/06/05 14:10
因为网格中存在空格所以用gets录入,首先录入一行数据,如果第一个字符为'Z'则break退出循环
其次是对指令的接受与处理,接受指令可以用getchar(),遇到换行符跳过。处理也很简单,在接受指令之前我们需要一个定位操作,找到空格所在的位置,然后根据指令确定要移动到的坐标,用swap函数交换数据,更新空格位置
大体思路就这样,附上AC代码
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxn = 5 + 10;const char S[] = "ABLR";const int dir[4][2] = { {-1, 0}, {1, 0},{0, -1},{0, 1} };char G[maxn][maxn];int main(){ int T = 0; while(1) { gets(G[0]); if(G[0][0] == 'Z') break; //分离案例 if(T++) printf("\n"); for(int i = 1; i < 5; i++) gets(G[i]); //寻找空格 int x0,y0; for(int i = 0; i <5; i++) for(int j = 0; j < 5; j++) if(G[i][j] == ' ') { x0 = i; y0 = j; break; } //进行移动 int ok = 1; char c; while((c = getchar()) && c != '0') { int k; if(!ok) continue; if(c == '\n') continue; if(strchr(S,c) == NULL) { ok = 0; continue; } else k = strchr(S,c) - S; int x = x0 + dir[k][0]; int y = y0 + dir[k][1]; if(x < 0 || x > 4 || y < 0 || y > 4) { ok = 0; continue; } else { swap(G[x][y], G[x0][y0]); x0 = x, y0 = y; } } getchar(); //打印解 printf("Puzzle #%d:\n",T); if(ok) for(int i=0;i<5;i++) printf("%c %c %c %c %c\n",G[i][0],G[i][1],G[i][2],G[i][3],G[i][4]); else printf("This puzzle has no final configuration.\n"); } return 0;}
阅读全文
1 0
- UVA227解题报告
- 第三章:UVa227 解题报告
- UVA227
- uva227
- UVA227
- Uva227
- UVa227
- Uva227
- UVa227
- UVa227
- UVA227
- UVA227
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- 解题报告
- 【ORACLE】ORACLE初始化参数分类思维导图
- IO流中的File类
- Oracle 11gR2 RAC修改SCAN IP地址
- 理解java动态代理
- RecycleView获取当前屏幕中itemview的显示区域
- UVA227解题报告
- Java 中的悲观锁和乐观锁的实现
- 面试题记录第一节——(activity、fragment)
- Shell脚本的静态检查工具shellcheck
- Mysql 查询
- oracle 11g RAC 修改public ip ,vip , scan ip
- TensorFlow 基础知识笔记之队列和线程
- 【数据结构与算法】海量数据解决方案 bitmap
- MOOC清华《面向对象程序设计》第8章:栈的实现(采用适配器模式)