算法竞赛第三章 习题3-5 谜题

xiaoxiao2021-02-28  35

#include<iostream> using namespace std; //建立5*5表格 char biao[5][5]; int main() {  //填充表格  初始位置 x行 y列 cin不读空格 原来傻冒用cin  int x , y ;  char p; int t=0;  while (cin.get(p))  {   if (t < 25)   {    biao[t / 5][t % 5] = p;    if (biao[t / 5][t % 5] == ' ')    {     x = t / 5; y = t % 5;    }   }   else break;   t++;  }  cout << x <<" "<< y;  //输入指令,以0结束  char c;  while (cin >> c)  {   //如果为0,结束并输出,其他位置交换值并改变末位置   if (c == '0')   {    for (int i = 0; i <5 ; i++)    {     for (int k = 0; k < 5; k++)     {      cout << biao[i][k]<<" ";     }     cout << endl;    }   }   //如果不为零   else   {    //往上    if (c == 'A')    {     if (x - 1 < 0) cout << "This puzzle has no final configuration";     else     {      biao[x][y] = biao[x - 1][y];      biao[x - 1][y] = ' ';      x--;     }    }    //往下    if (c == 'B')    {     if (x + 1 >4) cout << "This puzzle has no final configuration";     else     {      biao[x][y] = biao[x + 1][y];      biao[x + 1][y] = ' ';      x++;     }    }    //往左    if (c == 'L')    {     if (y-1<0) cout << "This puzzle has no final configuration";     else     {      biao[x][y] = biao[x][y-1];      biao[x][y-1] = ' ';      y--;     }    }    //往右    if (c == 'R')    {     if (y +1>4) cout << "This puzzle has no final configuration";     else     {      biao[x][y] = biao[x][y + 1];      biao[x][y + 1] = ' ';      y++;     }    }   }    }  system("pause");  return 0; }
转载请注明原文地址: https://www.6miu.com/read-2631969.html

最新回复(0)