Puzzle
2015-10-15 17:07
399 查看
Uva227
大概意思就是空格的移动,如果不合法就输出This puzzle has no final configuration.否则
输出最终的移动的答案,注意输出格式,最后多一个换行也是错的。
大概意思就是空格的移动,如果不合法就输出This puzzle has no final configuration.否则
输出最终的移动的答案,注意输出格式,最后多一个换行也是错的。
#include <stdio.h> #include <string.h> const int maxn = 7, N = 105; char str[maxn][maxn], ch ; int dx[5] = { 0, -1, 1, 0, 0 }, dy[5] = { 0, 0, 0, -1, 1 }; int check ( int x, int y ) //检查是否出界 { return x < 0 || x >= 5 || y < 0 || y >= 5; } template < class T > void swap ( T &a, T &b ) //模板交换 { T t = a; a = b; b = t; } void print ( int n ) //打印 { for ( int i = 0; i < n; i ++ ) { for ( int j = 0; j < n; j ++ ) printf ( j == 0 ? "%c" : " %c", str[i][j] ); printf ( "\n" ); } } int main ( ) { int x, y, flag, cas = 0; //freopen ( "in0.in", "r", stdin ); while ( 1 ) { flag = 0; fgets ( str[0], maxn, stdin ); if ( str[0][0] == 'Z' ) //当字符为Z时退出 break ; for ( int i = 1; i < 5; i ++ ) fgets ( str[i], maxn, stdin ); for ( int i = 0; i < 5; i ++ ) for ( int j = 0; j < 5; j ++ ) { if ( str[i][j] == ' ' ) { x = i; y = j; } } int n = 0; while ( ~ scanf ( "%c", &ch ) ) { if ( ch == '0' ) break ; n ++; } ch = '\0'; //赋结束标志 getchar ( ); //吃掉\n /*printf ( "%d\n", n ); for ( int i = 0; i <= n; i ++ ) printf ( "1.%s\n", ch[i] );*/ for ( int i = 0; i <= n; i ++ ) { //printf ( "5.%c\n", ch[i][j] ); int pos = 0; switch ( ch[i] ) { case 'A' : pos = 1; break ; case 'B' : pos = 2; break ; case 'L' : pos = 3; break ; case 'R' : pos = 4; break ; } int nx = x+dx[pos]; int ny = y+dy[pos]; //printf ( "%d %d\n", nx, ny ); if ( check ( nx, ny ) ) { flag = 1; break ; } swap ( str[x][y], str[nx][ny] ); x = nx; y = ny; } if ( cas ++ ) //*注意空格不能放在下面打印,那样会多一个换行,会出错 printf ( "\n" ); printf ( "Puzzle #%d:\n", cas ); if ( flag ) printf ( "This puzzle has no final configuration.\n" ); else print ( 5 ); } return 0; }
相关文章推荐
- js 判断各种数据类型
- 自制无线共享工具C++源代码
- eclipse为方法添加注释的快捷键是什么
- Qt 中使用智能指针
- RSA算法原理
- 黑马程序员——高新技术--反射
- PHP 字符串和十六进制互转
- 在Maven仓库中添加Oracle JDBC驱动
- 时间戳服务
- linux下OCI基础:配置Instant Client连oracle数据库
- Appium 服务关键字
- jsp中的basePath,获取应用的路径
- Linux命令大全
- Android 5.x新特性之利用CardView制造温和的阴影效果
- Zookeeper学习4_Paxos算法2-算法过程
- json数据转化格式
- maven项目使用jetty跑
- JDK自带VisualVM监控tomcat 内存、CPU
- [145]Binary Tree Postorder Traversal
- 公务员的工作性质