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 ...
随机推荐
- apache2 httpd 基于域名的虚拟主机配置 for centos6X 和debian-8
全系统虚拟主机: for debian 系统的apache2 域名 虚拟主机
- 【转】FLV视频封装格式详解
Overview Flash Video(简称FLV),是一种流行的网络格式.目前国内外大部分视频分享网站都是采用的这种格式. File Structure 从整个文件上开看,FLV是由The FLV ...
- hdu 2853
虚拟赛一开始lyf就对我说这是一道匹配的题目,我一看明显裸的最优匹配,敲完提交wrong, 题目要求改变尽量少的公司,就是如果遇到相等的权值,优先选择跟他原来匹配的,KM匹配是按序号大小来的,如果一个 ...
- [RxJS] Reactive Programming - Using cached network data with RxJS -- withLatestFrom()
So now we want to replace one user when we click the 'x' button. To do that, we want: 1. Get the cac ...
- Chapter 4: Spring and AOP:Spring's AOP Framework -- draft
Spring's AOP Framework Let's begin by looking at Spring's own AOP framework - a proxy-based framewor ...
- sql server 2008 中的架构(schame)理解
机构是属于数据库里面的.在一个数据库中,每一张表都属于一个架构,架构就像是命名空间,把数据库中的表分成不同的组,一个组就是一个命名空间,方便管理
- Linux 运维笔记
#配置静态地址网卡DEVICE="eth0"BOOTPROTO=staticHWADDR="00:0C:29:DC:EA:F7"NM_CONTROLLED=&q ...
- 主运行循环main run loop的一些理解
应用主运行循环负责处理所有用户相关的事件.UIApplication对象在应用启动时安装主运行循环并且使用此循环去处理事件和处理基于视图的界面更新.正如名字所表明的,该主运行循环是在应用的主线程app ...
- 实施双工通信框架:SignalR
SignalR:基于Asp.net平台构建,利用JavaScript或者Websockets,实现在客户端与服务端异步通信的框架. Html5新规范:WebSocket
- Android学习之Activity之间的数据传递
Activity与Activity之间很多情况下都需要进行数据的传递,下面就用几个简单的例子来看一下. (一).一个Activity启动另一个Activity并将数据传递到这个Activity当中 思 ...