hdu2102 (dfs)搜尋水題,hdu2102dfs

來源:互聯網
上載者:User

hdu2102 (dfs)搜尋水題,hdu2102dfs

題意就不多說了   肯見很多人都是用bfsA的  然後我就果斷用dfs做了       和其他深搜都一樣就是注意下裡面的一些剪枝  不然很容易就逾時



#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;


char map[5][15][15];
int mark[5][15][15],t,n,m,flash;
int dir[4][2]={0,1,0,-1,1,0,-1,0};
int dfs(int x,int y,int z,int tt)
{
int i,xx,yy,zz;
if(map[x][y][z]=='P')
{
flash=1;
return 0;
}
else if(map[x][y][z]=='#')
{
if(x==0) xx=1;
else xx=0;
yy=y;
zz=z;
if(mark[xx][yy][zz]==0&&map[xx][yy][zz]!='*')
{
mark[xx][yy][zz]=1;
dfs(xx,yy,zz,tt);
mark[xx][yy][zz]=0;
}
}
else
{
for(i=0;i<4;i++)
{
xx=x;
yy=y+dir[i][0];
zz=z+dir[i][1];
if(yy<0||yy>=n||zz<0||zz>=m) continue;
if(tt+1>t) continue;
if(mark[xx][yy][zz]==0&&map[xx][yy][zz]!='*')
{
mark[xx][yy][zz]=1;
dfs(xx,yy,zz,tt+1);
mark[xx][yy][zz]=0;
}
}
}
return 0;
}
int main()
{
int T,i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&t);
for(i=0;i<n;i++)
scanf("%s",map[0][i]);
for(i=0;i<n;i++)
scanf("%s",map[1][i]);
map[0][0][0]='.';
memset(mark,0,sizeof(mark));
mark[0][0][0]=1;
flash=0;
dfs(0,0,0,0);
if(flash) printf("YES\n");
else printf("NO\n");
}
return 0;
}

聯繫我們

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