题目描述:算法竞赛入门经典习题3-5
题目思路:模拟题
#include <stdio.h> #include <string.h> #define maxn 55 char gird[maxn][maxn] ; char cmd[maxn] ; int blank_x,blank_y ; int main(int argc, char *argv[]) { int flag = 1; for(int i=0;i<5;i++) gets(gird[i]) ; for(int i=0;i<5;i++) for(int j=0;j<5;j++) if(gird[i][j]==' '){ blank_x = i; blank_y = j; } while(gets(cmd)){ int len = strlen(cmd) ; for(int i=0;i<len;i++){ if(cmd[len-1] == '0' || cmd[i] == 'A' || cmd[i] == 'R' || cmd[i] == 'B' || cmd[i] == 'L' ){ if(cmd[i] == 'A' && blank_x != 0) { gird[blank_x][blank_y] = gird[blank_x-1][blank_y]; gird[--blank_x][blank_y] = ' '; } if(cmd[i] == 'R' && blank_y != 4) { gird[blank_x][blank_y] = gird[blank_x][blank_y+1]; gird[blank_x][++blank_y] = ' '; } if(cmd[i] == 'B' && blank_x != 4) { gird[blank_x][blank_y] = gird[blank_x+1][ blank_y]; gird[++blank_x][ blank_y] = ' '; } if(cmd[i] == 'L' && blank_y != 0) { gird[blank_x][blank_y] = gird[blank_x][ blank_y-1]; gird[blank_x][--blank_y] = ' '; } } else flag = 0; } if(flag) { for(int i = 0; i < 5; i++) { printf("%c %c %c %c %c ", gird[i][0], gird[i][1], gird[i][2], gird[i][3], gird[i][4]); } } else printf("This puzzle has no final configuration. "); } return 0; }