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 ...
随机推荐
- 支持多文件上传,预览,拖拽,基于bootstra的上传插件fileinput 的ajax异步上传
首先需要导入一些js和css文件 <link href="__PUBLIC__/CSS/bootstrap.css" rel="stylesheet"&g ...
- struts的ognl.NoConversionPossible错误
JSP页面便利集合的时候,代码如下 <s:iterator value="storageList" id="stList" status="st ...
- startActivity与startActivityForResult的使用小结
1.在一个Activity中想要启动进入另一个Activity时,通常我们使用startActivity这个方法来实现,代码如下: 其中MainActivity为源activity,ActivityL ...
- (转)Building MariaDB on Mac OS X using Homebrew
https://kb.askmonty.org/en/building-mariadb-on-mac-os-x-using-homebrew/ Work has been done to provid ...
- [置顶] STM32移植contiki进阶之三(中):timer 中文版
鉴于自己英语水平不高,在这里,将上一篇关于contiki 的timer的文章翻译为中文,让自己在学习的时候,更方便点.文中有许多不是很通顺的地方,将就吧. Timers Contiki系统提供了一套时 ...
- UVA 10400 Game Show Math (dfs + 记忆化搜索)
Problem H Game Show Math Input: standard input Output: standard output Time Limit: 15 seconds A game ...
- Android Action Bar 详解篇 .
作者原创,转载请标明出处:http://blog.csdn.net/yuxlong2010 作为Android 3.0之后引入的新的对象,ActionBar可以说是一个方便快捷的导航神器.它可以作为活 ...
- SegmentFault 巨献 1024 程序猿游戏「红岸的呼唤」第三天任务攻略
第三关也不是一般的难呐,那么继续写一下解题过程(第四关会是什么样呢?). 高速传送门:http://segmentfault.com/game/3 在用我想到的方法(booth算法.矩阵变换.各种CP ...
- HTTP 503 错误 – 服务不可用 (Service unavailable)
介绍 因暂时超载或临时维护,您的 Web 服务器目前无法处理 HTTP 请求. 其含义是, 这是一个暂时情况,会有一些延误, 过 后将会得到缓解. 有些服务器在这种情况下也许干脆拒绝套接字(socke ...
- .net文件下载方法汇总
转载自:http://blog.sina.com.cn/s/blog_680942070101ahsq.html //TransmitFile实现下载 protected void Button1_C ...