UVa 227 Puzzle
来源:互联网 发布:2016最近网络最火的dj 编辑:程序博客网 时间:2024/06/05 23:52
Problem Description
有一个5*5的网格,其中恰好有一个格子是空的,其他格子各有一个字母。一共有4种指令:A,B,L,R,分别表示把空格上,下,左,右,的相邻字母移到空格种。输入初始网格和指令序列(以数字0结束),输入指令执行完毕后的网格。如该有非法指令,应输出“This puzzle has no final configuration.”。
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.
代码:
#include<bits/stdc++.h>using namespace std;int main(){ char s[10][10]; char c; int i, j, cas = 1; memset(s, 0, sizeof(s));//初始化 while(gets(s[0]))//输入网格第一行 { if(s[0][0] == 'Z') break;//退出循环 for(i = 1; i < 5; i++)//接下来四行 { gets(s[i]); } int x, y, flag = 0, ll = 0; for(i = 0; i < 5; i++) { for(j = 0; j < 5; j++) { if(s[i][j] == ' ' || s[i][j] == '\0')//可能空格在最后面 { s[i][j] = ' ';//如果是最后面让它为空格 ll = 1; x = i; y = j;//标记下标 while(1) { scanf("%c", &c); if(c == 'A') { if(x - 1 >= 0 && x - 1 < 5)//满足指令 { s[x][y] = s[x - 1][y]; x = x - 1;//更新空格的下标 s[x][y] = ' '; } else { flag = 1; break; } } else if(c == 'B') { if(x + 1 >= 0 && x + 1 < 5)//满足指令 { s[x][y] = s[x + 1][y]; x = x + 1;//更新空格的下标 s[x][y] = ' '; } else { flag = 1; break; } } else if(c == 'L') { if(y - 1 >= 0 && y - 1 < 5)//满足指令 { s[x][y] = s[x][y - 1]; y = y - 1;//更新空格的下标 s[x][y] = ' '; } else { flag = 1; break; } } else if(c == 'R') { if(y + 1 >= 0 && y + 1 < 5)//满足指令 { s[x][y] = s[x][y + 1]; y = y + 1;//更新空格的下标 s[x][y] = ' '; } else { flag = 1; break; } } else if(c == '0') break; } while(c != '0') { scanf("%c", &c); } } if(ll) break;//表示已经找到空格 } if(ll) break; } getchar(); if(cas != 1) printf("\n"); printf("Puzzle #%d:\n", cas++); if(flag) printf("This puzzle has no final configuration.\n");//非法指令 else{//不非法输出结果 for(i = 0; i < 5; i++) { for(j = 0; j < 5; j++) { if(j) printf(" "); printf("%c", s[i][j]); } printf("\n"); } } memset(s, 0, sizeof(s));//初始化 } return 0;}
0 0
- UVa 227 - Puzzle
- UVa 227 - Puzzle
- Uva-227-Puzzle
- UVa 227 Puzzle
- UVA - 227 Puzzle
- Uva-227 - Puzzle-AC
- UVa 227 - Puzzle
- UVa-227 - Puzzle
- 【Uva 227】 Puzzle
- UVa 227 - Puzzle
- UVa 227 Puzzle
- UVA - 227 Puzzle
- Uva - 227 - Puzzle
- UVA - 227 Puzzle
- UVa 227 Puzzle
- UVA 227 Puzzle
- UVa 227 - Puzzle
- UVa 227 Puzzle
- 有一种绝望叫做无限PE hzau 1207 Candies (华中农业大学第五届程序设计大赛网络同步赛 I题)
- 算法导论 练习题 10.2-8
- Unity Editor 基础篇(十二):更改Hierarchy面板物体显示
- vim utf-8 bom设置的问题
- Detecting Near-Duplicates for Web Crawling - simhash与重复信息识别
- UVa 227 Puzzle
- 今年二十七八岁
- 图的邻接表存储
- linux下软件中心安装qtcreator出现问题
- 名企笔试:好未来2017秋招笔试:(倒置字符串)2017-03-07 算法爱好者
- Scala快速开始
- 事务
- SQL Server DBA工作内容详解
- python 数据清洗之数据合并、转换、过滤、排序