⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1103 hike on a graph.txt

📁 浙江大学ACM练习题 1103 提交通过代码 上一个包失误未付原码,十分抱歉!
💻 TXT
字号:
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;

class pt
{
public:
 int i, k, j, c;
};

queue<pt> pq, tq, ttq;
int n, p1, p2, p3, b[50][50][50], ans;
char a[50][50];


void init()
{
 p1 --, p2 --, p3 --;
 memset(b, 0, sizeof(b));
 ans = -1;
 pq = tq;
 b[p1][p2][p3] = 1;
 pt tpt;
 tpt.i = p1, tpt.k = p2, tpt.j = p3, tpt.c = 0;
 pq.push(tpt);
}

void bfs()
{
 while(!pq.empty())
 {
  pt tpt, ttpt;
  int i, k, j;
  tpt = pq.front();
  if(tpt.i == tpt.k && tpt.k == tpt.j)
  {
   ans = tpt.c;
   break;
  }
  for(i = 0; i < n; i ++)
  {
   if(a[tpt.j][tpt.k] == a[tpt.i][i])
   {
    k = tpt.k, j = tpt.j;
    if(!b[i][k][j])
    {
     b[i][k][j] = 1;
     ttpt.i = i, ttpt.j = j, ttpt.k = k, ttpt.c = tpt.c + 1;
     pq.push(ttpt);
    }
   }
  }
  for(k = 0; k < n; k ++)
  {
   if(a[tpt.j][tpt.i] == a[tpt.k][k])
   {
    i = tpt.i, j = tpt.j;
    if(!b[i][k][j])
    {
     b[i][k][j] = 1;
     ttpt.i = i, ttpt.j = j, ttpt.k = k, ttpt.c = tpt.c + 1;
     pq.push(ttpt);
    }
   }
  }
  for(j = 0; j < n; j ++)
  {
   if(a[tpt.i][tpt.k] == a[tpt.j][j])
   {
    k = tpt.k, i = tpt.i;
    if(!b[i][k][j])
    {
     b[i][k][j] = 1;
     ttpt.i = i, ttpt.j = j, ttpt.k = k, ttpt.c = tpt.c + 1;
     pq.push(ttpt);
    }
   }
  }
  pq.pop();
 }
}
   

int main()
{
 while(cin >> n && n)
 {
  cin >> p1 >> p2 >> p3;
  int i, k;
  for(i = 0; i < n; i ++)
  {
   for(k = 0; k < n; k ++)
   {
    cin >> a[i][k];
   }
  }
  init();
  bfs();
  if(ans != -1)
   printf("%d\n", ans);
  else
   printf("impossible\n");
 }
 return 0;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -