UVA 227 Puzzle - 输入输出
题目:
acm.hust.edu.cn/vjudge/roblem/viewProblem.action?id=19191
这道题本身难度不大,但输入输出时需要特别小心,一不留神就会出问题。
对于输入,如果要读入一行时:
没有空白字符,则直接使用scanf和%s即可;
有空白字符,使用gets,但要小心溢出;fgets一直不能正常工作,有待研究(gets会将缓冲区多余的\n扔掉,fgets会保留在字符串中);
对于要读入单个字符时:
使用scanf和“ %c”进行舍弃空白字符(注意空格),并且最后需要getchar来吃掉最后的回车;
scanf和“%c”会读入空白字符,和getchar相同。
只忽略回车,不忽略空格时,可将getchar放在以c==‘\n'为循环条件的do while中。
我心中的理想代码如下:
#include<stdio.h>
#include<string.h>
const int LEN=;
const int MAX=;
const int y[]={,,,-};
const int x[]={-,,,};
char map[LEN][LEN];
int tra[];
bool legal(int pos){
return <=pos&&pos<LEN;
}
void Pmap(){
for(int cow=;cow<LEN;cow++){
printf("%c",map[cow][]);
for(int col=;col<LEN;col++)
printf(" %c",map[cow][col]);
printf("\n");
}
}
int main(){
tra['A']=;
tra['B']=;
tra['R']=;
tra['L']=; bool first=true;
int Case=;
//freopen("in","r",stdin);
//freopen("out","w",stdout);
int bx,by;
while(gets(map[])){
if(map[][]=='Z')break;
for(int col=;col<LEN;col++)
gets(map[col]);
for(int i=;i<LEN;i++)
for(int j=;j<LEN;j++)
if(map[i][j]==' '){
bx=i;by=j;
}
bool ok=true;
char c;
while(scanf(" %c",&c),c!=''){
if(!ok)continue;
int nx=bx+x[tra[c]],ny=by+y[tra[c]];
if(!legal(nx)||!legal(ny)){
ok=false;
continue;
}
map[bx][by]=map[nx][ny];
map[nx][ny]=' ';
bx=nx;by=ny;
}
getchar();
if(first)
first=false;
else
printf("\n");
printf("Puzzle #%d:\n",++Case);
if(ok)
Pmap();
else
printf("This puzzle has no final configuration.\n");
}
return ;
}
UVA 227 Puzzle - 输入输出的更多相关文章
- uva 227 Puzzle (UVA - 227)
感慨 这个题实在是一个大水题(虽然说是世界决赛真题),但是它给出的输入输出数据,标示着老子世界决赛真题虽然题目很水但是数据就能卡死你...一直pe pe直到今天上午AC...无比感慨...就是因为最后 ...
- uva 227 Puzzle
Puzzle A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained ...
- UVA 227 Puzzle(基础字符串处理)
题目链接: https://cn.vjudge.net/problem/UVA-227 /* 问题 输入一个5*5的方格,其中有一些字母填充,还有一个空白位置,输入一连串 的指令,如果指令合法,能够得 ...
- 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 277 Puzzle
题意:输入5x5的字符串,输入操作,要求输出完成操作后的字符串. 注意:①输入的操作执行可能会越界,如果越界则按题目要求输出不能完成的语句. ②除了最后一次的输出外,其他输出均要在后面空一行. ③操作 ...
- UVA 227 周期串
题意: 给一个字符串,寻找最短的循环节 如abcabcabcabc以3为周期,也按6和12为周期. 分析: 因为循环节肯定是相等的,所以枚举串长度的所有约数的循环节再判断是否相等即可. 我的方法是枚举 ...
- Uva227.Puzzle
题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 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 ...
随机推荐
- hdu4355 三分
F - 三分 Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:32768KB 64bit I ...
- mysql grant all privileges on
遇到了 SQLException: access denied for @'localhost' (using password: no) 解决办法 grant all privileges on * ...
- linux for循环
一定要记得写后面的分号:http://www.runoob.com/linux/linux-shell-variable.html 这个页面的课程的循环教程是有问题的 for color in yel ...
- php 与 jquery中$.post()与attr()方法的简单实例 amaze modal 模态窗口
$(selector).attr(attribute,value)$.post() 在jqery中有这样一个方法,$.post()下面就这个方法做一个简单的实例: jQuery.post( url, ...
- php性能效率优化
[size=5][color=Red]php性能效率优化[/color][/size] 最近在公司一边自学一边写PHP程序,由于公司对程序的运行效率要求很高,而自己又是个新手,一开始就注意程序的效率很 ...
- IOS 项目问题总结
把自己项目中遇到的问题总结一下,供大家参考,希望大家多多提出意见!! 在Xcode 6.2中遇到Your build settings specify a provisioning profile w ...
- Android 开发中关于layoutinflater
Inflater英文意思是膨胀,在Android中应该是扩展的意思吧. LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout ...
- 用continue语句的时候,要千万小心内存泄漏,当然还有return和break也是
疑惑了大半年的内存泄漏,居然是因为这个原因- 有空学学QT的指针使用,可以使得代码更简洁.更不容易内存泄漏-
- 通过Excel来集中管理资源文件
在支持双语或多语种项目中,常常需要编辑多个文件来添加资源项,感觉比较繁琐,所以想做一个可以集中管理资源文件的工具.借助Excel,使用Excel来记录,并且通过Excel可以进行分页分模块来规划 ...
- yumdebug