题意:输入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的更多相关文章

  1. uva 227 Puzzle

     Puzzle  A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained ...

  2. UVA 227 Puzzle - 输入输出

    题目: acm.hust.edu.cn/vjudge/roblem/viewProblem.action?id=19191 这道题本身难度不大,但输入输出时需要特别小心,一不留神就会出问题. 对于输入 ...

  3. UVA 227 Puzzle(基础字符串处理)

    题目链接: https://cn.vjudge.net/problem/UVA-227 /* 问题 输入一个5*5的方格,其中有一些字母填充,还有一个空白位置,输入一连串 的指令,如果指令合法,能够得 ...

  4. uva 227 Puzzle (UVA - 227)

    感慨 这个题实在是一个大水题(虽然说是世界决赛真题),但是它给出的输入输出数据,标示着老子世界决赛真题虽然题目很水但是数据就能卡死你...一直pe pe直到今天上午AC...无比感慨...就是因为最后 ...

  5. UVA_Digit Puzzle UVA 12107

    If you hide some digits in an integer equation, you create a digit puzzle. The figure below shows tw ...

  6. uva live 12846 A Daisy Puzzle Game

    假设下一个状态有必败.那么此时状态一定是必胜,否则此时状态一定是必败 状压DP #include<iostream> #include<map> #include<str ...

  7. UVA 12849 Mother’s Jam Puzzle( 高斯消元 )

    题目: http://uva.onlinejudge.org/external/128/12849.pdf #include <bits/stdc++.h> using namespace ...

  8. Puzzle UVA - 227 PE代码求大佬指点

    ​ A children's puzzle that was popular 30 years ago consisted of a 5×5 frame which contained 24 smal ...

  9. UVA - 12107 Digit Puzzle(数字谜)(IDA*)

    题意:给出一个数字谜,要求修改尽量少的数,使修改后的数字谜只有唯一解.空格和数字可以随意替换,但不能增删,数字谜中所有涉及的数必须是没有前导零的正数.输入数字谜一定形如a*b=c,其中a.b.c分别最 ...

随机推荐

  1. spring boot 整合 shiro

    shrio官网:https://shiro.apache.org/ Apache Shiro是一个功能强大且易于使用的Java安全框架,可执行身份验证,授权,加密和会话管理.借助Shiro易于理解的A ...

  2. Penetration testing _internal & wireless Penetration Testing

    第一部分 渗透测试步骤 ---参考资料  Ethical Hacking: The Value of Controlled Penetration Tests  下载地址  链接:https://pa ...

  3. C#概念总结(三)

    1.定义结构体 定义了结构体,必须使用了stuct语句,struct定义了一个带有多个成员的的新数据类型.C# 的结构不同于C的.具有一下等特点: 结构可以有方法.字段.索引.属性.运算方法和事件.结 ...

  4. python网络爬虫笔记(二)

    一.函数调用的默认设置 1.def enroll(name,grnder,age=4,city='Shanghai'): print (''name:',name) print (''gender', ...

  5. netty 学习(1)

    Netty使用:通过BootStrap来启动.而BootStrap主要分为两类:1.面向连接(TCP)的(ClientBootStrap和ServerBootStrap);2. 非面向连接(UDP)的 ...

  6. .NET成年了,然后呢?

    作者|Lex Li 编辑|郭蕾 这可能是唯一一篇系统回顾 .NET 发展的文章..NET 的成年礼到了,你会送它什么? 2014 年 11 月 12 日,美国纽约曼哈顿,多云,气温适宜.微软公司执行副 ...

  7. css3三角形冒泡泡图形制作

    图一: 图二: <!DOCTYPE html> <html> <head> <title>css 三角形</title> <style ...

  8. Directory 类

    Directory 类 该类公开,主要用于创建.移动和枚举通过目录和子目录的静态方法.此类不能被继承.       命名空间: System.IO;       程序集: mscorlib(在 msc ...

  9. 【AtCoder】ARC077

    C - pushpush 如果是按下标说的话 如果是偶数个 那么是 \(N,N - 2,N - 4...1,3,5...N - 1\) 如果是奇数个 \(N,N - 2,N - 4...2,4,6.. ...

  10. 【转】android:paddingLeft与android:layout_marginLeft的区别

    http://www.blogjava.net/anchor110/articles/342206.html 当按钮分别设置以上两个属性时,得到的效果是不一样的. android:paddingLef ...