您的位置:首页 > 其它

HDU1728 逃离迷宫

2017-02-16 23:37 253 查看
the question is here http://acm.hdu.edu.cn/showproblem.php?pid=1728

I work so hard to find a way to cut edges.I failed at last.So I learn that from other people.I realize that cuting-edges is not a easy work,but requiring hard work and learn.And If I give up before learn nothing.All the effort is gone.So this question need to book the nubers of turns that the girl made and cut depend on different sutution.

#include <bits/stdc++.h>
#define inf 0x3fffffff
using namespace std;
const int MAXN=105;
char m[MAXN][MAXN];
int turn[MAXN][MAXN];
int flag=0;
int dx[4]= {-1,0,1,0};
int dy[4]= {0,1,0,-1};
int N,M,k;
int tx,ty;
/*
1
5 5
...**
*.**.
.....
.....
*....
2 1 1 1 3

*/
void dfs(int x,int y,int dir)
{
if(x==tx&&y==ty)
{
if(turn[x][y]<=k)
{
flag=1;
}
return;
}
if(turn[x][y]>k) return;

if(turn[x][y]==k && x!=tx && y!=ty) return;

for(int i=0; i<4; i++)
{
int xx=x+dx[i];
int yy=y+dy[i];

if(xx<0||xx>=N||yy<0||yy>=M) continue;

if(m[xx][yy]=='*'||turn[xx][yy]<turn[x][y]) continue;

if(dir!=-1&&dir!=i&&turn[xx][yy]<turn[x][y]+1) continue;

turn[xx][yy]=turn[x][y];

if(dir!=-1&&i!=dir)
{
turn[xx][yy]+=1;
}
dfs(xx,yy,i);
if(flag==1)
{
return;
}

}
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>N>>M;
for(int i=0; i<N; i++)
{
scanf("%s",m[i]);
}
int sx,sy;
cin>>k>>sy>>sx>>ty>>tx;
sx--;sy--;tx--;ty--;
for(int i=0; i<N; i++)
{
for(int j=0;j<M; j++)
{
turn[i][j]=inf;
}
}
flag=0;
turn[sx][sy]=0;
dfs(sx,sy,-1);
if(flag==1)
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: