Tempter of the Bone(HDU 深搜)

来源:互联网 发布:mac 输入法 emoji表情 编辑:程序博客网 时间:2024/04/30 06:46

题目链接

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <string>#include <cmath>#include <queue>using namespace std;int n, m, t;int flag;int sx, sy, dx, dy;char maze[8][8];int vis[8][8];int mv[4][2] = {{0, 1},{1, 0},{-1, 0}, {0, -1}};void dfs(int x, int y, int time){    if(x == dx && y == dy)    {        if(time == t)          flag = 1;    }    else    {        if(flag == 1)            return;        int i;        for(i = 0; i < 4; i++)        {            int tx = x + mv[i][0];            int ty = y + mv[i][1];            if(tx >= 0 && tx < n && ty >= 0 && ty < m && !vis[tx][ty] && (maze[tx][ty] == '.' || maze[tx][ty] == 'D'))            {                vis[tx][ty] = 1;                dfs(tx, ty, time+1);                vis[tx][ty] = 0;            }        }    }}int main(){    int i, j;    while(cin >> n >> m >> t)    {        if(!n && !m && !t)            break;            flag = 0;        for(i = 0; i < n; i++)        {            cin >> maze[i];            for(j = 0; j < m; j++)            {                if(maze[i][j] == 'S')                {                    sx = i; sy = j;                }                if(maze[i][j] == 'D')                {                    dx = i; dy = j;                }            }        }        memset(vis, 0, sizeof(vis));        vis[sx][sy] = 1;        dfs(sx, sy, 0);        if(flag == 1)            cout << "YES" << endl;        else            cout << "NO" << endl;    }    return 0;}


0 0