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 ...
 
随机推荐
- poj 2407 Relatives(简单欧拉函数)
			
Description Given n, a positive integer, how many positive integers less than n are relatively prime ...
 - Linux中图形界面和文本模式相互切换
			
1.默认开机进入文本模式 如果想让开机自动进纯文本模式, 修改/etc/inittab 找到其中的 id:5:initdefault: 这行指示启动时的运行级是5,也就是图形模式 改成3就是文本模式了 ...
 - 【POJ2114】Boatherds 树分而治之
			
做广告: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog. ...
 - C++11下的线程池以及灵活的functional + bind + lamda
			
利用boost的thread实现一个线程类,维护一个任务队列,以便可以承载非常灵活的调用.这个线程类可以方便的为后面的线程池打好基础.线程池还是动态均衡,没有什么别的.由于minGW 4.7 对 C+ ...
 - javascript---String与Arry
			
var str = "liuzhanqi"; document.write(str["length"]);//等价str.l ength var str = s ...
 - 华为Oj  找出字符串第一个出现一次的字符
			
#include <stdio.h> #include <string.h> char firstSingle(char *str) { int hash[255]={0}; ...
 - 2 MD5加密  java实现
			
百度百科对MD5的说明是: Message Digest Algorithm MD5(中文名为消息摘要算法第 五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护. MD5即Mess ...
 - hdu Intelligent IME
			
算法:字典树 题意:手机9键拼音:2:abc 3:def 4:ghi 5:jkl 6:mno 7:pqrs 8:tuv 9:wxyz: 第一行读入一个T,代表测试组数: 之后输入两个整数n和m, 有 ...
 - sdsdd
			
while(scanf("%d",&n)!=EOF) { res=-; level(tmp,n,res,); printf("%d/n",res); }
 - I/O复用-epoll模型
			
epoll函数 epoll函数的使用与select.poll上有很大的差异. epoll使用一组函数来完成任务,而不是单个函数. epoll把用户关心的文件描述符上的事件放在内核里的一个事件表中,从而 ...