3520. Step out of maze tju

來源:互聯網
上載者:User

/*
 這是暑假集訓的第二場練習賽,這個題目給我第一印象就是DFS
 可是DFS恰是我的弱項,於是一直卡了好久,DFS到底沒有用的路徑沒有return
 第二場比賽就只做了一道悲劇收場
 漸漸開始明白很多題目都有相似之處,比如這個就跟hdu1026很像,可是我一開始就沒有
 想到BFS,如果以開始就想到BFS的話,我想我肯定可以做出來的
 第二天敲的時候還是有很多細節的地方需要注意的,居然PE 3次之多
 我相信通過暑假的學習我一定會更加成熟
*/
#include <iostream>
#include <cstdio>
#include <queue>
#include <stack>
using namespace std;

int map[105][105];
bool hash[105][105];
int dir[4][2] = { {0, 1}, {1, 0}, {-1, 0}, {0, -1} };
int n, m;
bool flag;
int pt[10000];
int len;

struct node
{
 int x;
 int y;
 int pre;
 int di;
}path[10000];
void BFS(int x, int y)
{
 queue<node> Q;
 node P, N;
 int t = 0;
 memset(hash, false, sizeof(hash));
 /*
 for(int i = 0; i < 4; i++)
 {
  P.x = x + dir[i][0];
  P.y = y + dir[i][1];
  P.pre = t;
  P.di = i;
  if(map[P.x][P.y] == 0)
  {
   hash[P.x][P.y] = true;
   Q.push(P);
  }
 }
 */
 P.x = x;
 P.y = y;
 P.pre = -1;
 Q.push(P);
 while(!Q.empty())
 {
  N = Q.front();
  path[t] = N;
  if(N.x < 1 || N.x > n || N.y < 1 || N.y > m)
  {
   stack <int> s;
   //int tt = N.pre;
   int d = N.di;
   s.push(d);
   int tt = N.pre;
   while(tt != 0)
   {
    s.push(d);
    tt = path[tt].pre;
    d = path[tt].di;
   }
   d = s.top();
   s.pop();
   if(d == 0)
    cout<<"R";
   else if(d == 1)
    cout<<"D";
   else if(d == 2)
    cout<<"U";
   else if(d == 3)
    cout<<"L";
   while(!s.empty())
   {
    d = s.top();
    s.pop();
    if(d == 0)
     cout<<" R";
    else if(d == 1)
     cout<<" D";
    else if(d == 2)
     cout<<" U";
    else if(d == 3)
     cout<<" L";
   }
   cout<<endl;
   return ;

  }

  Q.pop();
  for(int i = 0; i < 4; i++)
  {
   P.x = N.x + dir[i][0];
   P.y = N.y + dir[i][1];
   P.di = i;
   P.pre = t;
   if(!hash[P.x][P.y] && map[P.x][P.y] == 0)
   {
    Q.push(P);
    hash[P.x][P.y] = true;
   }
  }
  t++;
 }

}

void dfs(int x, int y, int di)
{
 
 if(x < 1 || x > n || y < 1 || y > m)
 {
  flag = true;
  return ;
 }
 
 for(int i = 0; i < 4; i++)
 {
  int x1 = x + dir[i][0];
  int y1 = y + dir[i][1];
  di = i;
  if(flag)
   return ;//比賽的時候這裡忘記了加return ,輸出了多餘的路徑,一直改不出來
  if(map[x1][y1] == 0)
  {
   map[x][y] = 1;
   dfs(x1, y1, di);
   if(flag)
   {
    pt[len++] = di;
    /*
    if(di == 0)
     cout<<"R ";
    else if(di == 1)
     cout<<"D ";
    else if(di == 2)
     cout<<"U ";
    else if(di == 3)
     cout<<"L ";
     */
   }
   map[x][y] = 0;
   
  }
 }
 
 
}

int main()
{
 int t;
 cin>>t;
 while(t--)
 {
  cin>>n>>m;
  memset(map, 0, sizeof(map));
  for(int i = 1; i <= n; i++)
  for(int j = 1; j <= m; j++)
   cin>>map[i][j];
  int x, y;
  cin>>x>>y;
  flag = false;
  len = 0;
  dfs(x, y, 0);
  if(pt[len - 1] == 0)
   cout<<"R";
  else if(pt[len - 1] == 1)
   cout<<"D";
  else if(pt[len -1 ] == 2)
   cout<<"U";
  else if(pt[len -1 ] == 3)
   cout<<"L";
  for(int i = len - 2; i >= 0; i--)
  {
   if(pt[i] == 0)
    cout<<" R";
   else if(pt[i] == 1)
    cout<<" D";
   else if(pt[i] == 2)
    cout<<" U";
   else if(pt[i] == 3)
    cout<<" L";
  }
  cout<<endl;
   
  //BFS(x, y);
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.