Uva227.Puzzle
题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=163
| # | Problem | Verdict | Language | Run Time | Submission Date | |
| 13767338 | 227 | Puzzle | Accepted | C++ | 0.026 | 2014-06-19 02:39:04 |
| 13766276 | 227 | Puzzle | Wrong answer | C++ | 0.135 | 2014-06-18 16:48:26 |
Puzzle
Time limit: 3.000 seconds
A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained 24 small squares of equal size. A unique letter of the alphabet was printed on each small square. Since there were only 24 squares within the frame, the frame also contained an empty position which was the same size as a small square. A square could be moved into that empty position if it were immediately to the right, to the left, above, or below the empty position. The object of the puzzle was to slide squares into the empty position so that the frame displayed the letters in alphabetical order.
The illustration below represents a puzzle in its original configuration and in its configuration after the following sequence of 6 moves:
1) The square above the empty position moves.
2) The square to the right of the empty position moves.
3) The square to the right of the empty position moves.
4) The square below the empty position moves.
5) The square below the empty position moves.
6) The square to the left of the empty position moves.

Write a program to display resulting frames given their initial configurations and sequences of moves.
Input
Input for your program consists of several puzzles. Each is described by its initial configuration and the sequence of moves on the puzzle. The first 5 lines of each puzzle description are the starting configuration. Subsequent lines give the sequence of moves.
The first line of the frame display corresponds to the top line of squares in the puzzle. The other lines follow in order. The empty position in a frame is indicated by a blank. Each display line contains exactly 5 characters, beginning with the character on the leftmost square (or a blank if the leftmost square is actually the empty frame position). The display lines will correspond to a legitimate puzzle.
The sequence of moves is represented by a sequence of As, Bs, Rs, and Ls to denote which square moves into the empty position. A denotes that the square above the empty position moves; B denotes that the square below the empty position moves; L denotes that the square to the left of the empty position moves; R denotes that the square to the right of the empty position moves. It is possible that there is an illegal move, even when it is represented by one of the 4 move characters. If an illegal move occurs, the puzzle is considered to have no final configuration. This sequence of moves may be spread over several lines, but it always ends in the digit 0. The end of data is denoted by the character Z.
Output
Output for each puzzle begins with an appropriately labeled number (Puzzle #1, Puzzle #2, etc.). If the puzzle has no final configuration, then a message to that effect should follow. Otherwise that final configuration should be displayed.
Format each line for a final configuration so that there is a single blank character between two adjacent letters. Treat the empty square the same as a letter. For example, if the blank is an interior position, then it will appear as a sequence of 3 blanks - one to separate it from the square to the left, one for the empty position itself, and one to separate it from the square to the right.
Separate output from different puzzle records by one blank line.
Note: The first record of the sample input corresponds to the puzzle illustrated above.
Sample Input
TRGSJ
XDOKI
M VLN
WPABE
UQHCF
ARRBBL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAA
LLLL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAAAABBRRRLL0
Z
Sample Output
Puzzle #1:
T R G S J
X O K L I
M D V B N
W P A E
U Q H C F Puzzle #2:
A B C D
F G H I E
K L M N J
P Q R S O
T U V W X Puzzle #3:
This puzzle has no final configuration.
解题思路:简单模拟题。花点时间敲就好。坑爹的是输入的时候每次gets可能会吃上一行的回车。注意一下。 自己在做的时候写了两版,第一版比较冗杂,最后WA也不知道什么原因(没有读到eof怎么也应该是TLE)。 第二版的输入输出方法是对拍网上大神的,并自己重敲了一会。还要好好的去学习简化代码的方法。AC才是硬道理啊!
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <string>
#include <algorithm>
#include <numeric> using namespace std; int main() {
string Map[];
//char Map[5];
int x_s, y_s, cnt = ;
while() {
/*getline(cin, Map[0]);
for(int i = 0; i < 5; i++) {
if(Map[0][i] == ' ') {
x_s = 0; y_s = i;
}
}*/
//memset(Map, 1, sizeof(Map));
if(cnt) getchar();
if(cnt != ) cout << endl;
for(int i = ; i < ; i++) {
//gets(Map[i]);
getline(cin, Map[i]); if(!i && Map[].size() == ) {
return ;
}
for(int j = ; j < ; j++) {
if(Map[i][j] == ' ') {
x_s = i; y_s = j;
}
}
} string op_now, op = "";
while() {
cin >> op_now;
op += op_now;
if(op_now[op_now.size() - ] == '') {
break;
}
} bool flag = false;
for(int i = ; op[i] != ''; i++) {
if(op[i] == 'B') {
x_s += ;
if(x_s > ) flag = true;
if(!flag) swap(Map[x_s - ][y_s], Map[x_s][y_s]);
} else if(op[i] == 'A') {
x_s -= ;
if(x_s < ) flag = true;
if(!flag) swap(Map[x_s + ][y_s], Map[x_s][y_s]);
} else if(op[i] == 'R') {
y_s += ;
if(y_s > ) flag = true;
if(!flag) swap(Map[x_s][y_s - ], Map[x_s][y_s]);
} else if(op[i] == 'L') {
y_s -= ;
if(y_s < ) flag = true;
if(!flag) swap(Map[x_s][y_s + ], Map[x_s][y_s]);
}
if(flag) break;
} //cout << "Puzzle #:"
printf("Puzzle #%d:\n", ++cnt);
if(flag) {
cout << "This puzzle has no final configuration." << endl;
} else {
for(int i = ; i < ; i++) {
for(int j = ; j < ; j++) {
if(!j) cout << Map[i][j];
else cout << " " << Map[i][j];
}
cout << endl;
}
}
}
return ;
}
WA代码
#include <stdio.h>
#include <string.h> int main () {
int cases = ;
bool line = false;
char initial [] []; while ( gets (initial []) ) { if ( strcmp (initial [], "Z") == )
return ; gets (initial []);
gets (initial []);
gets (initial []);
gets (initial []); int blank_x;
int blank_y; for ( int i = ; i < ; i++ ) {
for ( int j = ; j < ; j++ ) {
if ( initial [i] [j] == ' ' ) {
blank_x = i;
blank_y = j;
i = j = ;
}
}
} char command [];
bool valid = true;
bool exit_koro = false; while ( !exit_koro && gets (command)) { for ( int i = ; command [i] != ; i++ ) { if ( command [i] == '' || !valid ) {
exit_koro = true;
break;
} switch (command [i]) {
case 'A' :
if ( blank_x == )
valid = false;
else {
initial [blank_x] [blank_y] = initial [blank_x - ] [blank_y];
initial [blank_x - ] [blank_y] = ' ';
blank_x--;
}
break; case 'B' :
if ( blank_x == )
valid = false;
else {
initial [blank_x] [blank_y] = initial [blank_x + ] [blank_y];
initial [blank_x + ] [blank_y] = ' ';
blank_x++;
}
break; case 'R' :
if ( blank_y == )
valid = false;
else {
initial [blank_x] [blank_y] = initial [blank_x] [blank_y + ];
initial [blank_x] [blank_y + ] = ' ';
blank_y++;
}
break; case 'L' :
if ( blank_y == )
valid = false;
else {
initial [blank_x] [blank_y] = initial [blank_x] [blank_y - ];
initial [blank_x] [blank_y - ] = ' ';
blank_y--;
}
break;
}
}
} if ( line )
printf ("\n");
line = true; printf ("Puzzle #%d:\n", ++cases); if ( valid ) {
for ( int i = ; i < ; i++ ) {
printf ("%c %c %c %c %c\n", initial [i] [], initial [i] [],
initial [i] [], initial [i] [], initial [i] []);
}
} else
printf ("This puzzle has no final configuration.\n"); } return ;
}
AC代码
Uva227.Puzzle的更多相关文章
- 例题 3-5 谜题 uva227 Puzzle
A children’s puzzle that was popular years ago consisted of a × frame which contained small squares ...
- UVA-227 Puzzle(模拟)
题目: 题目浏览传送门 题意: 给出一个5*5的方格,里边有一个格子是空的,现在给出一串指令,A->空格向上移动,B->空格向下移动,R->空格向右移动,L->空格向左移动. ...
- UVA227 - Puzzle(紫书习题3.5)
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring&g ...
- [笔记]cin、cout与scanf、printf的效率差异对比分析
之前上传UVa227 puzzle时,好不容易AC了,但发现自己用时50(ms),而在VJ上看到人家都是40ms.20ms,于是打开一个20ms的代码查看人家强在哪里.但结果研究了半天感觉差不多,于是 ...
- [UVA227][ACM/ICPC WF 1993]Puzzle (恶心模拟)
各位大佬都好厉害…… 这个ACM/ICPC1993总决赛算黄题%%% 我个人认为至少要绿题. 虽然算法上面不是要求很大 但是操作模拟是真的恶心…… 主要是输入输出的难. 对于ABLR只需要模拟即可 遇 ...
- 谜题 (Puzzle,ACM/ICPC World Finals 1993,UVa227)
题目描述:算法竞赛入门经典习题3-5 题目思路:模拟题 #include <stdio.h> #include <string.h> #define maxn 55 char ...
- 【习题 3-5 UVA-227】Puzzle
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟题.. 输入稍微恶心了点. getchar()一个一个搞就好. [代码] #include <bits/stdc++.h& ...
- 习题3-5 谜题(Puzzle, ACM/ICPC World Finals 1993, UVa227)
#include<stdio.h> #include<string.h> char s[5][5]; int main() { while(gets(s[0])) { int ...
- [刷题]算法竞赛入门经典 3-4/UVa455 3-5/UVa227 3-6/UVa232
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa455:Periodic Strings 代码: //UVa455 #inclu ...
随机推荐
- Codeforce 219 div1
B 4D"部分和"问题,相当于2D部分和的拓展,我是分解成2D部分和做的: f[x1][y1][x2][y2]=true/false 表示 左上(x1,y1) 右下(x2,y2)的 ...
- 【转】AAC ADTS格式分析
1.ADTS是个啥 ADTS全称是(Audio Data Transport Stream),是AAC的一种十分常见的传输格式. 记得第一次做demux的时候,把AAC音频的ES流从FLV封装格式中抽 ...
- JAVA日期处理(Timestamp)
要写一些与数据库连接时的日期处理,pstmt.setDate()的类型是java.sql.Date类型,这种符合规范的类型其实并没有把时分秒存进数据库,所以存取时就应该用setTimestamp()或 ...
- VSCode
下载: 打开终端控制器 wget http://download.microsoft.com/download/0/D/5/0D57186C-834B-463A-AECB-BC55A8E466AE/V ...
- ThinkPHP3.2.3中三大自动中的缺陷问题
我们在使用Thinkphp3.2.3框架时在对数据表进行模型化后就可以使用自动完成功能. 自动完成可以帮助我们更简便的完成对表单内容对数据表(集合)的填充,自动完成是基于: 当实例化数据库user后, ...
- Hadoop,HBase集群环境搭建的问题集锦(四)
21.Schema.xml和solrconfig.xml配置文件里參数说明: 參考资料:http://www.hipony.com/post-610.html 22.执行时报错: 23., /comm ...
- Gunplot 命令大全
在linux命令提示符下运行gnuplot命令启动,输入quit或q或exit退出. plot命令 gnuplot> plot sin(x) with line linetype 3 linew ...
- JavaScripts学习日记——ECMAscript
1.Function对象 Function是一个很特殊的对象,特殊在该对象就像java中的方法一样,可以运行,可以传参数. 三种定义function对象的方法: 1.function fun1(a,b ...
- (转)Div+CSS布局入门
在网页制作中,有许多的术语,例如:CSS.HTML.DHTML.XHTML等等.在下面的文章中我们将会用到一些有关于HTML的基本知识,而在你学习这篇入门教程之前,请确定你已经具有了一定的HTML基础 ...
- Asp.Net Mvc5新特性
One ASP.NET:统一平台 BootStrap:免费Css响应式页面 路由标记属性:简单,控制器,操作,前缀,参数,URL ASP.NET WEB API 2:路由标记属性,Oauth2.0,O ...