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分别最 ...
随机推荐
- java子类继承关系
1.java的类按照继承关系的树形结构所有的类其根节点都是object类,一个类有两种重要的成员,一是变量 .二是方法.子类继承不能继承父类中被声明为private的变量和方法. public cla ...
- css中border画三角形
<!doctype html><html lang="en"> <head> <meta charset="UTF-8&quo ...
- 广工赛-hdu6470矩阵快速幂
递推时把(n+1)^3拆开 构造矩阵即可 #include<bits/stdc++.h> using namespace std; #define ll long long #define ...
- 模块(import语句,from...import语句,_name_属性)
1, 什么是模块? 模块就是一系列功能的集合体 模块分为四个通用的类别: 1), 使用python编写的.py文件(*****) 2), 已被编译为共享库或DLL的C或C++扩展 3), 把一系列模块 ...
- Variable binding depth exceeds max-specpdl-size
(setq max-specpdl-size 5) ; default is 1000, reduce the backtrace level (setq debug-on-error t) ; no ...
- ecilpse运行Servlet程序是找不到路径的原因
当工作空间路径有空格时,空格会被转成%20,将导致路径无法识别,于是就找不到路径了.
- 51Nod 1298 圆与三角形(计算几何)
1298 圆与三角形 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出圆的圆心和半径,以及三角形的三个顶点,问圆同三角形是否相交.相交输出"Yes&quo ...
- C# 位移运算
一:“<<”和“>>”运算符用于执行移位运算,分别称为左移位运算符和右移位运算符.对于X<<N和X>>N形式的运算,含义是将X向左或向右移动N位,得到的 ...
- Git 分支 - 分支的新建与合并
转载自:https://git-scm.com/book/zh/v1/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB ...
- 小甲鱼python基础教程飞机大战源码及素材
百度了半天小甲鱼python飞机大战的源码和素材,搜出一堆不知道是什么玩意儿的玩意儿. 最终还是自己对着视频一行行代码敲出来. 需要的同学点下面的链接自取. 下载