UVa227 Posted on 2018-03-12 UVa227 一道我目前为止写过的最长代码的题,小小纪念一下,思路很简单,就是模拟,但是他的输出格式,我不是很明白,是复制网友代码然后发现的规律。代码很简单,应该可以看懂,orz 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105#include<iostream>#include<stdio.h>using namespace std;char s[6][6];struct blank{ int row;//行 int col;//列}blank;int input(){ int flag2 = 0; for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) { s[i][j] = getchar(); if (s[i][j] == 'Z') { flag2 = 1; break; } if (s[i][j] == ' ') //指向空白格 { blank.col = j; blank.row = i; } if (j == 5)getchar(); } if (flag2 == 1)break; } return flag2;}int move(){ char move; int flag = 0; while(cin>>move) { if (move == '0')break; switch (move) { case('A'): if (blank.row != 1) { s[blank.row][blank.col] = s[blank.row-1][blank.col]; s[--blank.row][blank.col] = ' '; } else flag = 1; break;//上 case('B'): if (blank.row != 5) { s[blank.row][blank.col] = s[blank.row+1][blank.col]; s[++blank.row][blank.col] = ' '; } else flag = 1; break;//下 case('R'): if (blank.col != 5) { s[blank.row][blank.col] = s[blank.row][blank.col+1]; s[blank.row][++blank.col] = ' '; } else flag = 1; break;//右 case('L'): if (blank.col != 1) { s[blank.row][blank.col] = s[blank.row][blank.col-1]; s[blank.row][--blank.col] = ' '; } else flag = 1; break;//左 } } return flag;}void output(int count){ if (count > 1)cout << endl; printf("Puzzle #%d:\n", count); for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) { if (j == 5)cout << s[i][j] << endl; else cout << s[i][j] << " "; } }}int main(){ int count = 1; for (;;) { int t1,t2; t1 = input(); if (t1)break; t2=move(); if (t2==0)output(count++); else { if (count > 1)cout << endl; printf("Puzzle #%d:\n", count++); cout << "This puzzle has no final configuration." << endl; } getchar(); } return 0;}