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分别最 ...
随机推荐
- dbcp连接池出现的问题java.lang.AbstractMethodError: com.mysql.jdbc.Connection.isValid(I)Z
解决方案:mysql-connector 版本为 5.0.4 ,那么对应的 dbcp 和 pool 版本应该为 1.4 和 1.6 . 5.0.4 不应该使用 2.0 及以上版本的 dbcp 和 ...
- Allegro PCB Design GXL (legacy) 从dxf文件中导入板框
Allegro PCB Design GXL (legacy) version 16.6-2015 新建brd文件,并设置好相应的参数之后,点击菜单:File > Import > DXF ...
- txt文档去重复内容
@echo off for /f "delims=" %%i in ('type "%1"') do (if not defined %%i set %%i=A ...
- ORA-12705
1. 分析 ORA-12705是一个与nls 环境或者文件相关的错误,按照Oracle 官方的提示,要么是环境变量配置错误,要么是通过alter session 命令调整了错误的nls参数值,要么是n ...
- STL容器之优先队列
STL容器之优先队列 优先级队列,以前刷题的时候用的比较熟,现在竟然我只能记得它的关键字是priority_queue(太伤了).在一些定义了权重的地方这个数据结构是很有用的. 先回顾队列的定义:队列 ...
- thinkphp5验证码使用
simple 控制器中 /** * 生成验证码 * @param viod */ public function verify() { $captcha = new \think\captcha\Ca ...
- python之string模块常量:数字,26个字母,标点符号,空白
In [8]: import string In [9]: dir(string) In [10]: string.ascii_letters Out[10]: 'abcdefghijklmnopqr ...
- Javascript \x 反斜杠x 16进制 编解码
js 里 \x 开头的通常是16进制编码的数据,下面代码实现编解码: 解码 function decode(str){ return str.replace(/\\x(\w{2})/g,functio ...
- Vue爬坑之路
1.关闭eslint严格语法检查
- mysql配置完半同步复制之后报错[ERROR] The server quit without updating PID file
修改配置,MySQL启动报:[ERROR] The server quit without updating PID file [root@localhost mysql]# /etc/init.d/ ...