(HDU1010)Tempter of the Bone

来源:互联网 发布:四知先生翻译 编辑:程序博客网 时间:2024/06/06 01:00

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1010

#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<cstdlib>#include<sstream>#include<set>#include<algorithm>#include<queue>#include<stack>#include<vector>#include<ctime>#include<fstream>#include<iomanip>using namespace std;#define M 105int n,m,t,si,sj,ei,ej,c,wall;char ma[M][M];int dir[4][2]={{0, 1}, {0, -1}, {1, 0}, {-1, 0}};void DFS(int x, int y, int time){    if (x<=0||x>n||y<=0||y>m) return;    if (c) return;    if (x == ei && y == ej && time == t)    {        if(time == t)            c = 1;        return;    }    int temp = (t - time) - abs(x - ei) - abs(y - ej);    if(temp<0 || temp & 1)  return;    for (int i=0;i<4;i++)    {        int x1=x+dir[i][0];        int y1=y+dir[i][1];        if (ma[x1][y1]!='X')        {            ma[x1][y1]='X';            DFS(x1, y1,time+1);            ma[x1][y1]='.';        }    }    return;}int main(){    while (cin>>n>>m>>t)    {        if(n==0&&m==0&&t==0) break;        wall = 0;        for (int i = 1; i<=n; i++)        {            for (int j = 1; j<=m; j++)            {                cin>>ma[i][j];                if (ma[i][j] == 'S')                {                    si = i;                    sj = j;                }                if (ma[i][j] == 'D')                {                    ei = i;                    ej = j;                }                if (ma[i][j] == 'X')                    wall ++;            }        }        c = 0;        ma[si][sj] = 'X';        if (n * m - wall <= t)        {            cout<<"NO"<<endl;            continue;        }        DFS(si, sj, 0);        if (c)            cout<<"YES"<<endl;        else            cout<<"NO"<<endl;    }    return 0;}
0 0
原创粉丝点击