hdu 2598 Manipulating the Power Square 模拟水题
2013-06-07 23:58
375 查看
Manipulating the Power Square
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 186 Accepted Submission(s): 62
[align=left]Problem Description[/align]
Rosalina finds Super Mario puzzling over the Power Square and gives him another hint on how to unlock its power. ”Keep swapping 0 with one of its neighbors! I’ll tell you which neighbor,” Rosalina says to Mario. Rosalina then gives
Mario a sequence of directions on which neighbor to swap with 0.
[align=left]Input[/align]
Input is a description of of the Power Square, followed by a number of commands. The first line is the size of the Power Square n. You may assume n<=100. The second line contains the n2 values in the Power Square, separated by spaces.
Values start from the top left corner and move from left to right, moving down one row to the leftmost position when a row is filled.
Following the Power Square description are a number of commands, with each command on a separate line. Each command begins with the name of the command, followed by any additional command parameters.
There will no more than 100 commands.
[align=left]Output[/align]
The command ”SHOW” causes the current state of the Power Square to be displayed in n × n
form (each row of n values on a single line, separated by spaces), followed by a blank line.
The command ”MOVE” is followed by one more more moves: ”up”, ”down”, ”left”, or ”right”.
Each move is executed as follows:
– For ”up”, swap 0 with its neighbor above.
– For ”left”, swap 0 with its left neighbor.
– For ”right”, swap 0 with its right neighbor.
– For ”down”, swap 0 with its neighbor below.
If move attempts to swap 0 with a non-existent neighbor, then output ”FAILED” on a single line and stop attempting the remaining moves. If the move succeeds, ”MOVED” is output on a single line. In either case, the state of the Power Square is changed to reflect
the moves made.
[align=left]Sample Input[/align]
3
8 7 6 5 4 3 2 1 0
SHOW
MOVE up
SHOW
3
8 7 6 5 4 3 2 1 0
SHOW
MOVE up right
[align=left]Sample Output[/align]
8 7 6
5 4 3
2 1 0
MOVED
8 7 6
5 4 0
2 1 3
8 7 6
5 4 3
2 1 0
FAILED
[align=left]Source[/align]
HDU 2010-05 Programming Contest
[align=left]Recommend[/align]
lcy
题意:
输入 n 以及MOVE SHOW 等命令 输入n 则之后要输入一个n*n的矩阵 输入SHOW 要输出矩阵 输入MOVE 其后可以跟多个子命令(up down right left) 表示将0和其上下右左
进行交换 如果MOVE后的所有操作都可以完成 则输出MOVED 否则输出FAILED
思路: 直接模拟
#include<stdio.h> #include<string.h> char s[10000]; int map[111][111]; int main() { int n,i,j,flag=0,x,y,xx,yy,cg;//cg表示交换是否成功 flag表示输入是否依旧是up right down left while(scanf("%s",s)!=EOF) { if(strcmp(s,"MOVE")==0) { if(flag==1) { if(cg==1) printf("MOVED\n"); else printf("FAILED\n"); cg=1; flag=0; } } else if(strcmp(s,"SHOW")==0) { if(flag==1) { if(cg==1) printf("MOVED\n"); else printf("FAILED\n"); cg=1; flag=0; } for(i=1;i<=n;i++) { for(j=1;j<n;j++) printf("%d ",map[i][j]); printf("%d\n",map[i][j]); } printf("\n"); } else if(strcmp(s,"up")==0) { if(cg==0) continue; flag=1; xx=x-1;yy=y; if(xx<1||xx>n||yy<1||yy>n) cg=0; else { int temp; temp=map[x][y];map[x][y]=map[xx][yy];map[xx][yy]=temp; //temp=x; x=xx;xx=temp; temp=y; y=yy;yy=temp; x=xx;y=yy; } } else if(strcmp(s,"left")==0) { if(cg==0) continue; flag=1; xx=x;yy=y-1; if(xx<1||xx>n||yy<1||yy>n) cg=0; else { int temp; temp=map[x][y];map[x][y]=map[xx][yy];map[xx][yy]=temp; //temp=x; x=xx;xx=temp; temp=y; y=yy;yy=temp; x=xx;y=yy; } } else if(strcmp(s,"right")==0) { if(cg==0) continue; flag=1; xx=x;yy=y+1; if(xx<1||xx>n||yy<1||yy>n) cg=0; else { int temp; temp=map[x][y];map[x][y]=map[xx][yy];map[xx][yy]=temp; //temp=x; x=xx;xx=temp; temp=y; y=yy;yy=temp; x=xx;y=yy; } } else if(strcmp(s,"down")==0) { if(cg==0) continue; flag=1; xx=x+1;yy=y; if(xx<1||xx>n||yy<1||yy>n) cg=0; else { int temp; temp=map[x][y];map[x][y]=map[xx][yy];map[xx][yy]=temp; //temp=x; x=xx;xx=temp; temp=y; y=yy;yy=temp; x=xx;y=yy; } } else { if(flag==1) { if(cg==1) printf("MOVED\n"); else printf("FAILED\n"); } flag=0; n=0;cg=1; //printf("s=%s\n",s); for(i=0;s[i]!='\0';i++) n=n*10+s[i]-'0'; // printf("n=%d\n",n); for(i=1;i<=n;i++) for(j=1;j<=n;j++) { scanf("%d",&map[i][j]); if(map[i][j]==0) {x=i;y=j;} } } } if(flag==1)//如果最后一个命令是MOVE 要在所有输入结束后输出下面的结果 { if(cg==1) printf("MOVED\n"); else printf("FAILED\n"); } }
相关文章推荐
- hdu 5083 有坑+字符串模拟水题
- hdu 1491(Octorber 21st)(模拟水题)
- HDU 5186 zhx's submissions 模拟水题
- HDU-1035 Robot Motion 模拟问题(水题)
- HDU 1228 模拟水题
- HDU 5968 异或密码(模拟水题)
- HDU 1031 Design T-Shirt 模拟 水题
- HDU-1225 Football Score 模拟问题(水题)
- 单调队列1002 HDU 4122 Alice's mooncake shop 对于学习单调队列的同学不建议做,浪费时间的模拟水题
- hdu 4530 小Q系列故事——大笨钟 模拟水题
- HDU 3347 Calculate the expression By Assassin 模拟水题
- HDU 5099 Comparison of Android versions(模拟-水题)
- HDU 1056 HangOver 模拟 水题
- HDU 5095 Linearization of the kernel functions in SVM(模拟, 水题)
- HDU 3635 Dragon Balls 并查集水题 模拟
- hdu 5007 Post Robot【模拟】水题
- HDU-5835 Danganronpa(模拟水题)
- hdu 1117 Booklet Printing(模拟 分类 水题)
- hdu 4891 模拟水题
- HDU 1234 开门人和关门人(模拟水题)