UVA 277 Puzzle
题意:输入5x5的字符串,输入操作,要求输出完成操作后的字符串。
注意:①输入的操作执行可能会越界,如果越界则按题目要求输出不能完成的语句。
②除了最后一次的输出外,其他输出均要在后面空一行。
③操作的最后一个换行符可能会占据str[0],需要用c来getchar()
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int noConfig=,x,y,findEmpty=,count=,cmdcount=;
//noConfig是指令不能执行(越界时),findEmpty是一边输入一边查找空字符的时候用,1为找到
//count是puzzle的计数,cmdcount是命令的计数
char str[][],cmd[];
//str是输入的字符串,cmd是操作指令
int judge(int exi,int exj)//判断越界
{
if(exi<||exi>=||exj<||exj>=)
{
return -;
}
return ;
} int swap(int i,int j,char cmd)//str[exi][exj]交换str[i][j]中的空字符
{
int exi,exj;
// printf(" i=%d j=%d cmd=%c\n",i,j,cmd);
switch (cmd)
{
case 'A':
exi=i-;
exj=j;
break;
case 'B':
exi=i+;
exj=j;
break;
case 'L':
exi=i;
exj=j-;
break;
case 'R':
exi=i;
exj=j+;
break;
}
// printf("exi=%d exj=%d\n",exi,exj);
//判断
if(judge(exi,exj)==-)
{
noConfig=;
return -;
}
//交换
str[y][x]=str[exi][exj];
// printf(" Y,X:str[%d][%d]=%c\n",y,x,str[y][x]);
str[exi][exj]=' ';
// printf(" EXI,EXJ:str[%d][%d]=%c\n",y,x,str[exi][exj]);
y=exi;
x=exj;
// printf("after change:\n");
// for(i=0;i<5;i++)
// {
// for(j=0;j<5;j++)
// {
// printf("%c",str[i][j]);
// }
// printf("\n");
// }
return ;
} int main()
{
char c;
int i,j,k;
while()
{
noConfig=;
cmdcount=;
memset(str,'\0',sizeof(str));
for(i=;i<;i++)
{
gets(str[i]);
if(str[][]=='Z')
{
return ;
}
// for(j=0;j<5;j++)//边输入边查找
// {
// if(str[i][j]==' '||str[i][j]=='\0')
// {
// x=j;
// y=i;
// findEmpty=1;
//// printf("Y=%d X=%d\n",y,x);
// }
// }
}
for(i=;i<;i++)
{
for(j=;j<;j++)
{
if(str[i][j]==' '||str[i][j]=='\0')
{
x=j;
y=i;
// printf("Y=%d X=%d\n",y,x);
}
}
}
count++;
while((c=getchar())!='')
{
cmd[cmdcount++]=c;
}
c=getchar();//用c取最后的换行符,不然str[0]会被换行符占据
for(i=;i<cmdcount;i++)
{
if(swap(y,x,cmd[i])==-)
break;
}
// fflush(stdin);写了这个就TIE
//输出结果
if(count!=)
printf("\n");//注意这里,除最后一个输出外,其他的输完后都空一行
if(noConfig==)//无结果
{
printf("Puzzle #%d:\n",count);
printf("This puzzle has no final configuration.\n");
}
else
{
printf("Puzzle #%d:\n",count);
//输出
for(i=;i<;i++)
{
for(j=;j<;j++)
{
if(j!=)
printf(" ");
printf("%c",str[i][j]);
}
printf("\n");
}
}
}
return ;
}
UVA 277 Puzzle的更多相关文章
- uva 227 Puzzle
Puzzle A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained ...
- UVA 227 Puzzle - 输入输出
题目: acm.hust.edu.cn/vjudge/roblem/viewProblem.action?id=19191 这道题本身难度不大,但输入输出时需要特别小心,一不留神就会出问题. 对于输入 ...
- UVA 227 Puzzle(基础字符串处理)
题目链接: https://cn.vjudge.net/problem/UVA-227 /* 问题 输入一个5*5的方格,其中有一些字母填充,还有一个空白位置,输入一连串 的指令,如果指令合法,能够得 ...
- uva 227 Puzzle (UVA - 227)
感慨 这个题实在是一个大水题(虽然说是世界决赛真题),但是它给出的输入输出数据,标示着老子世界决赛真题虽然题目很水但是数据就能卡死你...一直pe pe直到今天上午AC...无比感慨...就是因为最后 ...
- UVA_Digit Puzzle UVA 12107
If you hide some digits in an integer equation, you create a digit puzzle. The figure below shows tw ...
- uva live 12846 A Daisy Puzzle Game
假设下一个状态有必败.那么此时状态一定是必胜,否则此时状态一定是必败 状压DP #include<iostream> #include<map> #include<str ...
- UVA 12849 Mother’s Jam Puzzle( 高斯消元 )
题目: http://uva.onlinejudge.org/external/128/12849.pdf #include <bits/stdc++.h> using namespace ...
- Puzzle UVA - 227 PE代码求大佬指点
A children's puzzle that was popular 30 years ago consisted of a 5×5 frame which contained 24 smal ...
- UVA - 12107 Digit Puzzle(数字谜)(IDA*)
题意:给出一个数字谜,要求修改尽量少的数,使修改后的数字谜只有唯一解.空格和数字可以随意替换,但不能增删,数字谜中所有涉及的数必须是没有前导零的正数.输入数字谜一定形如a*b=c,其中a.b.c分别最 ...
随机推荐
- Dubbo常用配置文件分析及核心源码阅读(SPI.Extension)
1.多版本支持: 基于上篇博客的 快速启动 Dubbo 服务 的代码进行多版本支持的演示:基于原来的实现类GpHelloImpl ,我们需要新增一个新版本的实类:GpHelloImpl2 public ...
- N阶楼梯上楼问题
N阶楼梯上楼问题 时间限制: 1 Sec 内存限制: 32 MB 题目描述 样例输出 13 #include <stdio.h> int main() { int i, n; long ...
- Ajax增删改查-----------查
查询所有 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- dnsjava usage
linux dig 命令使用方法 https://www.imooc.com/article/26971?block_id=tuijian_wz https://jimwayne.blogspot.c ...
- Windows系统下安装运行Kafka
一.安装JAVA JDK 1.下载安装包 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151. ...
- 来一个使用sysbench测试cpu性能的简单脚本
#!/bin/bash for ((i=1; i<16; i++)); do sysbench cpu run --cpu-max-prime=10000 --threads=4 --time= ...
- ES标准
精确来说,ES1 ~ ES5 中的数字是 ECMA-262 标准的版本号(edition). 即:Standard ECMA-262, 1st Edition(其实第一个版本是没有版本号的)Stand ...
- 5336: [TJOI2018]party
题解: 比较水啦..dp套dp f[i][j][k]表示枚举了前i位,最大公共子序列匹配状态为j,noi匹配到了第k位 因为g[j]和g[j+1]最多差1 所以可以状压成j 然后内层再dp一下搞出下一 ...
- IIS7部署报错 500.22错误 检查到这集成托管模式下不使用的ASP.NET配置
公司的一个项目,环境为: .Net 4.0 + MVC3,部署在Windows Server 2003R2 IIS6.0上面运行正常, 迁移到新服务器Windows Server 2008R2,部署在 ...
- seafile增加邮件服务功能
这个很简单哈,直接上配置.此处我用的163邮箱 vim /opt/seafile/conf/seahub_settings.py ### 163邮箱配置测试 ### EMAIL_USE_SSL = F ...