HDU1728 逃离迷宫

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值