UVa227_puzzle

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVa227_puzzle相关的知识,希望对你有一定的参考价值。

 1 //按要求来就可以了
  #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 using namespace std; 5 const int M = 5, N = 5, maxn = 1010; 6 7 void output(char (*p)[N]) 8 { 9 for (int i2 = 0; i2 < M; i2++) 10 { 11 printf ("%c %c %c %c %c\n", p[i2][0], p[i2][1],p[i2][2], p[i2][3], p[i2][4]); 12 } 13 } 14 15 int main(void) 16 { 17 char p[M][N]= {0}, c[maxn] = {0}; 18 int count = 0; 19 while (cin.getline(p[0],M+1)) 20 { 21 if (strcmp(p[0],"Z") == 0) 22 return 0; 23 cin.getline(p[1],M+1); //这样比循环的效率高. 24 cin.getline(p[2],M+1); 25 cin.getline(p[3],M+1); 26 cin.getline(p[4],M+1); 27 int b_x = 0, b_y = 0; 28 for (int i = 0; i < M; i++) 29 for (int j = 0; j < N; j++) 30 { 31 if (p[i][j] == ) { 32 b_x = i; b_y = j; //b_x,b_y代表空白位置 33 break; 34 } 35 } 36 int k = 0; 37 while (cin >> c[k]) 38 { 39 if (c[k] != 0) 40 k++; 41 else 42 break; 43 } 44 c[k] = 0; //因为每次在k的位置数是不定的,k不定!!所以要将第k位初始化等下面条件 45 //控制到c[i]!=0 46 int flag = 1, x = b_x, y = b_y; 47 for (int i = 0; c[i]; i++) 48 { 49 switch(c[i]) 50 { 51 case A : x = x-1; y = b_y;break; 52 case B : x = x+1; y = b_y;break; 53 case L : x = b_x; y = y-1; break; 54 case R : x = b_x; y = y+1; break; 55 default : flag = 0; break; 56 } 57 if (x < 0 || x > 4 || y < 0 || y > 4) 58 { 59 flag = 0; break ; 60 } 61 else { 62 p[b_x][b_y] = p[x][y]; 63 p[x][y] = ; 64 b_x = x; b_y = y; //移动的位置变成了空白位置!!注意!! 65 } 66 } 67 if (count) 68 printf("\n"); 69 printf ("Puzzle #%d:\n", ++count); 70 if (flag) 71 { 72 output(p); 73 } 74 else { 75 printf ("This puzzle has no final configuration.\n"); 76 } 77 getchar(); //cin.ignore(1,‘\n‘); 吃掉cin >> c[k]后面的‘\n‘ 78 } 79 return 0; 80 }

 

以上是关于UVa227_puzzle的主要内容,如果未能解决你的问题,请参考以下文章

uva 227 Puzzle

Puzzle UVa227(分析和问题解决)

uva 227 Puzzle (UVA - 227)

UVa - 227 - Puzzle

Uva 227-Puzzle 解题报告

习题3-5 Puzzle UVA - 227