Asteroids! hdu 1240 終於AC

來源:互聯網
上載者:User

/*
很水的bfs,一開始bfs不會,用dfs錯了好多次,應該是演算法有問題吧,我覺得這個題目用dfs
應該可以的
*/
#include<iostream>//2389356 2010-04-27 11:23:31 Accepted 1240 0MS 308K 1495 B C++ 悔惜晟
#include<queue>
#include<cstring>
#include<cmath>
using namespace std;

char map[12][12][12];
bool hash[12][12][12];
bool flag;
int dir[6][3] = {{0, 0, 1}, {0, 1, 0}, {0, 0, -1}, {0, -1, 0}, {1, 0, 0}, {-1, 0, 0} };
int a, b, c;
int d, e, f;
int t;
int n;
struct node
{
 int x;
 int y;
 int z;
 int num;
};
queue<node> q;

int bfs(int si, int sj, int sk)
{
 node N, P;
 N.x = si;
 N.y = sj;
 N.z = sk;
 N.num = 0;
 int i;
 q.push(N);
 hash[si][sj][sk] = false;
 while(!q.empty())
 {
  N = q.front();
  if(N.x == f && N.y == e && N.z == d && N.num == t)
  {
   flag = true;
   cout<<n<<" "<<N.num<<endl;
   break;
  }
  if(N.num > t)
  {
   break;
  }
  q.pop();
  for(i = 0; i < 6; i++)
  {
   P.x = N.x + dir[i][0];
   P.y = N.y + dir[i][1];
   P.z = N.z + dir[i][2];
   
   if(P.x >= 0 && P.x < n && P.y >= 0 && P.y < n && P.z >= 0 && P.z < n && hash[P.x][P.y][P.z] && map[P.x][P.y][P.z] == 'O')
   {
    P.num = N.num + 1;
    q.push(P);
    hash[P.x][P.y][P.z] = false;
   }
  }
   
 }
 return 1;

}
int main()
{
 char str[12];
 int i, j ,k;
 while(cin>>str>>n)
 {
  for(i = 0; i < n; i++)
  for(j = 0; j < n; j++)
  for(k = 0; k < n; k++)
   cin>>map[i][j][k];
  cin>>a>>b>>c;
  cin>>d>>e>>f;
  cin>>str;
  t = (int)(fabs(1.0*a - d) + fabs(1.0*b - e) + fabs(1.0*c - f)); //省賽的時候這樣的CE過
  flag = false;
  memset(hash, true, sizeof(hash));//一開始這裡標記錯了,以後應該規定標記的,不然寫寫就會錯了
  bfs(c, b ,a);
  if(!flag)
   cout<<"NO ROUTE"<<endl;
 }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.