UVA 227 - Puzzle
2016-01-17 10:19
423 查看
Puzzle
A children’s puzzle that was popular 30 years ago consisted of a 5×5 frame which contained 24 smallsquares of equal size. A unique letter of the alphabet was printed on each small square. Since therewere only 24 squares within the frame, the frame also containedan empty position which was the samesize as a small square. A square could be moved into that empty position if it were immediately to theright, to the left, above, or below the empty position. The object of the puzzle was to slide squaresinto 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 afterthe 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 andthe sequence of moves on the puzzle. The first 5 lines of each puzzle description are the startingconfiguration. 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 otherlines follow in order. The empty position in a frame is indicated by a blank. Each display line containsexactly 5 characters, beginning with the character
on the leftmost square (or a blank if the leftmostsquare 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 squaremoves into the empty position. A denotes that the square above the empty position moves; B denotesthat the square below the empty position moves; L denotes that
the square to the left of the emptyposition moves; R denotes that the square to the right of the empty position moves. It is possible thatthere is an illegal move, even when it is represented by one of the 4 move characters. If an illegal moveoccurs, the puzzle
is considered to have no final configuration. This sequence of moves may be spreadover 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.). Ifthe puzzle has no final configuration, then a message to that effect should follow. Otherwise that finalconfiguration should be displayed.Format each line for a final configuration so that there is a single blank character between twoadjacent letters. Treat the empty square the same as a letter. For example, if the blank is an interiorposition, 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
TRGSJXDOKI
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.
题目大意就是说,根据所给指向移动格中空格所在位置,输出最后状态,然后就是输出时最后一次没有空行。(好久之前写的,一直木整理。。。)
#include <iostream> #include<cstdio> #include<cstring> using namespace std; char mazz[6][6]; int x,y; int judge(int x,int y) { if(x<0||x>=5||y<0||y>=5) return 0; return 1; } int change(int sx,int sy) { if(judge(x+sx,y+sy)) { mazz[x][y]=mazz[x+sx][y+sy]; mazz[x+sx][y+sy]=' '; x+=sx; y+=sy; return 0; } return 1; } int main() { char c; int cnt=0; while(gets(mazz[0])) { int flag=0; if(strcmp(mazz[0],"Z")==0) return 0; for(int i=1;i<5;++i) gets(mazz[i]); for(int i=0;i<5;++i) { for(int j=0;j<5;++j) { if(mazz[i][j]==' '||mazz[i][j]=='\0') x=i,y=j; } } while(c=getchar()) { if(c=='0') break; if(c=='A') { if(change(-1,0)) flag=1; } else if(c=='B') { if(change(1,0)) flag=1; } else if(c=='L') { if(change(0,-1)) flag=1; } else if(c=='R') { if(change(0,1)) flag=1; } } if(cnt) printf("\n"); printf("Puzzle #%d:\n",++cnt); if(flag) printf("This puzzle has no final configuration.\n"); else { for(int i=0;i<5;++i) { for(int j=0;j<5;++j) { if(!j) printf("%c",mazz[i][j]); else printf(" %c",mazz[i][j]); } printf("\n"); } } getchar(); } return 0; }
相关文章推荐
- css--动画
- hive中使用rcfile
- 《机器学习实战》学习笔记之朴素贝叶斯(Naive Bayes)
- 半小时学会上传本地项目到github
- RMQ算法-poj3264
- C函数的调用过程 栈帧
- 找出数组{1,2,3,4,...N-1}中出现的唯一重复数
- android控件edittext-addTextChangedListener监听文本的变化
- php 引用和global
- VB.net操控網頁整理加實戰篇
- linux的远程唤醒
- Hello,World!
- 设计模式学习01—单例模式
- 清结算概念
- ajax页面动态加载弹出窗口
- 3450: Tyvj1952 Easy|概率与期望
- spring mvc 接受多对象的处置
- I NEED A OFFER!---hdu1203(01背包)
- 智能指针的原理及实现
- Codeforces 599 C Day at the Beach