题目连接: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的更多相关文章

  1. 例题 3-5 谜题 uva227 Puzzle

    A children’s puzzle that was popular years ago consisted of a × frame which contained small squares ...

  2. UVA-227 Puzzle(模拟)

    题目: 题目浏览传送门 题意: 给出一个5*5的方格,里边有一个格子是空的,现在给出一串指令,A->空格向上移动,B->空格向下移动,R->空格向右移动,L->空格向左移动. ...

  3. UVA227 - Puzzle(紫书习题3.5)

    #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring&g ...

  4. [笔记]cin、cout与scanf、printf的效率差异对比分析

    之前上传UVa227 puzzle时,好不容易AC了,但发现自己用时50(ms),而在VJ上看到人家都是40ms.20ms,于是打开一个20ms的代码查看人家强在哪里.但结果研究了半天感觉差不多,于是 ...

  5. [UVA227][ACM/ICPC WF 1993]Puzzle (恶心模拟)

    各位大佬都好厉害…… 这个ACM/ICPC1993总决赛算黄题%%% 我个人认为至少要绿题. 虽然算法上面不是要求很大 但是操作模拟是真的恶心…… 主要是输入输出的难. 对于ABLR只需要模拟即可 遇 ...

  6. 谜题 (Puzzle,ACM/ICPC World Finals 1993,UVa227)

    题目描述:算法竞赛入门经典习题3-5 题目思路:模拟题 #include <stdio.h> #include <string.h> #define maxn 55 char ...

  7. 【习题 3-5 UVA-227】Puzzle

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟题.. 输入稍微恶心了点. getchar()一个一个搞就好. [代码] #include <bits/stdc++.h& ...

  8. 习题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 ...

  9. [刷题]算法竞赛入门经典 3-4/UVa455 3-5/UVa227 3-6/UVa232

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa455:Periodic Strings 代码: //UVa455 #inclu ...

随机推荐

  1. [转载]Linux的时间与时钟中断处理

    本文主要介绍在Linux下的时间实现以及系统如何进行时钟中断处理. 一. Linux的硬件时间 PC机中的时间有三种硬件时钟实现,这三种都是基于晶振产生的方波信号输入.这三种时钟为: 实时时钟RTC ...

  2. 【转】AAC ADTS格式分析

    1.ADTS是个啥 ADTS全称是(Audio Data Transport Stream),是AAC的一种十分常见的传输格式. 记得第一次做demux的时候,把AAC音频的ES流从FLV封装格式中抽 ...

  3. MyBatis配置解析

    MyBatis配置文件解析(概要) 1.configuration:根元素 1.1 properties:定义配置外在化 1.2 settings:一些全局性的配置 1.3 typeAliases:为 ...

  4. SecureCRT 安装及初始化配置

    安装 SecureCRT 7.3.4 安装以及破解方法 SecureCRT 6.5.0 汉化解压版 初始化配置 这里配置以SecureCRT 6.5.0 汉化解压版为例 1.调整SecureCRT终端 ...

  5. 同一台电脑启动两个或多个tomcat

    今天要在机子的tomcat上部署新的项目,需要访问的端口为80,与之前不同. 但要求不能更改原tomcat部署项目的端口,因为该tomcat内的项目正在对外使用中,且不能断开服务器. 那么,我就需要再 ...

  6. (转)SQL Server2005 异常处理机制(Begin try Begin Catch)

    begin try --SQL  end trybegin catch --sql (处理出错动作) end catch我们将可能会出错的sql 写在begin try...end try 之间,若出 ...

  7. select radio readonly

    首先 select radio 设置 disable的会无法提交数据. 这让我很头疼  而且 readonly 无效 后来发现.我把自己绕进去了..一般涉及 只读都是 不让用户修改 .而后台只更新 可 ...

  8. 初识数据字典【weber出品必属精品】

    数据字典结构 有两部分组成: 1. 基表:以$结尾的系统表,在创建数据库的时候,oracle自动创建的表 2. 用户可以访问的视图 数据字典的种类 DICTIONARY:简称DICT,所有的数据字典, ...

  9. CM3存储器系统

    1.位带(Bit-Band):如1M的地址都可以用bit访问,然后用32M的地址对应这1M的地址.其中这32M地址的每个字的最低位对应那1M可bit寻址的每个位.

  10. hdu5323 Solve this interesting problem(爆搜)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Solve this interesting problem Time Limit ...