HDU 1728 逃離迷宮,hdu1728逃離迷宮

來源:互聯網
上載者:User

HDU 1728 逃離迷宮,hdu1728逃離迷宮

bfs

搞清楚是轉彎而不是步數。所以需要一個方向一直走下去直到邊界或者牆。

還有就是注意題意。給出起點終點的 x,y 位置是交換的。 題目是下標1開始。注意。


#include<cstdio>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<stack>#include<iostream>#include<list>#include<set>#include<vector>#include<cmath>#define INF 0x7fffffff#define eps 1e-8#define LL long long#define PI 3.141592654#define CLR(a,b) memset(a,b,sizeof(a))#define FOR(i,a,n) for(int i= a;i< n ;i++)#define FOR0(i,a,b) for(int i=a;i>=b;i--)#define pb push_back#define mp make_pair#define ft first#define sd second#define sf scanf#define pf printf#define acfun std::ios::sync_with_stdio(false)#define SIZE 100+1using namespace std;char g[SIZE][SIZE];struct lx{    int x,y,t;    void init(int xx,int yy,int tt)    {        x=xx,y=yy,t=tt;    }}start,thend;int n,m;int xx[]= {0,0,-1,1};int yy[]= {-1,1,0,0};//2//5 5//.....//.....//.**.*//..*.*//.....//2 4 4 2 4void bfs(){    bool vis[SIZE][SIZE];    CLR(vis,0);    queue<lx>q;    vis[start.x][start.y]=1;    q.push(start);    while(!q.empty())    {        lx tmp=q.front();        q.pop();//        pf("%d %d t=%d\n",tmp.x,tmp.y,tmp.t);//        system("pause");        if(tmp.x==thend.x&&tmp.y==thend.y&&tmp.t<=thend.t)        {            puts("yes");            return;        }        if(tmp.t>thend.t)continue;        FOR(k,0,4)        {            lx now;            int x=tmp.x+xx[k];            int y=tmp.y+yy[k];            while(x>=0&&x<n&&y>=0&&y<m&&g[x][y]=='.')            {                if(!vis[x][y])                {                    now.init(x,y,tmp.t+1);                    vis[x][y]=1;                    q.push(now);                }                x+=xx[k];                y+=yy[k];            }        }    }    puts("no");}int main(){    int tt;    sf("%d",&tt);    while(tt--)    {        sf("%d%d",&n,&m);        char str[SIZE];        FOR(i,0,n)        sf("%s",g[i]);        int x,y,t;        sf("%d%d%d",&t,&y,&x);start.init(x-1,y-1,-1);        sf("%d%d",&y,&x);thend.init(x-1,y-1,t);        //pf("%d %d == %d %d\n",start.x,start.y,thend.x,thend.y);        bfs();    }}



hdu acm 1728題逃離迷宮怎用DFS解決

這題的思路是求出最少的轉彎數目,然後和最大的轉彎數目比一比,就可以得出答案了,像這種求最少的一般用BFS比較好做的,DFS是解決一些判斷定性的問題的。這題用DFS不好做。BFS可以輕鬆解決。

#include<iostream>
#include<queue>
#include<cstdio>
usingnamespace std;
char map[102][102];
int visit[102][102];
int bx,by,ex,ey,step,N,M;
int XX[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct node{int x,y,step,dir;};
void bfs()
{
queue<node>qu;
node t;
int k;
t.x=bx, t.y=by, t.step=0, t.dir=-1;
visit[bx][by]=0;
qu.push(t) ;
while(!qu.empty())
{
t=qu.front();
qu.pop();
for(k=0;k<4;k++)
{
node tt=t;
tt.x+=XX[k][0], tt.y+=XX[k][1];
if(tt.x<1||tt.x>M||tt.y<1||tt.y>N||map[tt.x][tt.y]=='*')
continue;
if(tt.dir!=k&&tt.dir!=-1)tt.step++;
if(tt.step>step)continue;//!!!
if(tt.x==ex&&tt.y==ey)
{
cout<<"yes\n";return;
}
if(visit[tt.x][tt.y]>=tt.step)
{ tt.dir=k;
visit[tt.x][tt.y]=tt.step;
qu.push(tt);
}
}
}
cout<<"no\n";
return ;
}
int main()
{
int CASE,i,j;
cin>>CASE;
while(CASE--)
{
cin>>M>>N;//M行 N列
for(i=1;i<=M;i++)
for(j=1;j<=N;j++)
{
cin>>map[i][j];
visit[i][j]=999;
}
cin>>step>>by>>bx>>ey>>ex;//x對應列
if(bx==ex&&by==ey)
{cout<<"yes\n&quo......餘下全文>>
 
hdu1728 逃離迷宮 我錯在什地方了?一直WA

首先,你定義的二維數組int dir[4][2]={0,1,0,-1,-1,0,1,0}有問題,格式應該是:
int dir[4][2]={ {0,1} , {0,-1} , {-1,0} , {1,0} } ;

其次,你定義的int y1這個變數與標頭檔中的某個函數產生衝突,改成y01或者其他就可以編譯通過了,但是你程式具體實現什麼我不是很清楚,只是幫你編譯通過了,具體的演算法我再看看。
 

聯繫我們

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