您的位置:首页 > 其它

UVA - 227 Puzzle

2016-07-08 19:12 316 查看

UVA - 227 Puzzle

题目大意。给定一个5X5的格子,给定a~x的英文字母和一个空格,通过ablr移动位置

解题思路:二维数组 交换 元素

#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
char c;
int n,m;
bool t;
int s = 1;
c = getchar();
while(c != 'z' && c != 'Z'){
t = 1;
char a[5][5];
char b[100];
a[0][0] = c;
for (int i = 0;i < 5;i++){
for (int j = 0;j < 5; j++){
if (i == 0 && j == 0) continue;
a[i][j] = getchar();
}
getchar();
}
for (int i = 0;b[i-1] != '0';i++)
b[i] = getchar();
for (int i = 0;;i++){
for (int j = 0;j < 5;j++)
for (int k = 0; k < 5;k++)
if (a[j][k] == ' '){
n = j;
m = k;
}
if (b[i] == '0')break;
if (b[i] == 'a' || b[i] =='A'){
if (n - 1 < 0){t = 0;break;}
a
[m] = a[n-1][m];
a[n-1][m] = ' ';
}
if (b[i] == 'b' || b[i] == 'B'){
if (n + 1 > 4 ){t = 0;break;}
a
[m] = a[n+1][m];
a[n+1][m] = ' ' ;
}
if (b[i] == 'l' || b[i] == 'L'){
if (m - 1 < 0){t = 0;break;}
a
[m] = a
[m-1];
a
[m-1] = ' ';
}
if (b[i] == 'r' || b[i] == 'R'){
if (m + 1 > 4){t = 0;break;}
a
[m] = a
[m+1];
a
[m+1] = ' ';
}
}
cout<<"Puzzle #"<<s<<":"<<endl;
if (t != 0)
for (int i = 0; i < 5;i++){
for (int j = 0; j <4 ;j++){
putchar(a[i][j]);
cout<<' ';
}
putchar(a[i][4]);
cout<<endl;
}
else
cout<<"This puzzle has no final configuration."<<endl;

getchar();
s++;
c = getchar();
if (c != 'z' && c != 'Z')
cout<<endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: