码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ 2110 DFS

时间:2015-08-21 12:41:27      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:

狗要出门,且正好在T秒

就是DFS + 剪枝, 联系一下剪枝技巧

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int step[][2] = {0,1,0,-1,1,0,-1,0};
const int maxn = 10 + 7;

char Map[maxn][maxn];
int m,n,t;
int aimx,aimy;
int bex,bey;

bool DFS(int x,int y,int tt)
{
    if(x == 0 || x == m+1 || y == 0 || y == n+1) return false;
    if(x == aimx && y == aimy && tt == t) return true;
    int Dis = ((t - tt) - abs(x-aimx) - abs(y -aimy));
    if(Dis % 2 || Dis < 0)  return false;
    for(int i = 0; i <4; ++i)
    {
        if(Map[x+step[i][0]][y+step[i][1]] != X)
        {
            Map[x+step[i][0]][y+step[i][1]] = X;
            //cout << " X : " << x << "  Y : " << y << endl;
            if(DFS(x+step[i][0], y+step[i][1], tt+1)) return true;
            Map[x+step[i][0]][y+step[i][1]] = .;
        }
    }
    return false;
}

int main()
{
    while(cin >> m >> n >> t && (n + m + t))
    {
        for(int i = 0; i < maxn; ++i)
            for(int j = 0; j < maxn; ++j) Map[i][j] = X;
        int cnt = 0;
        for(int i = 1; i <= m; ++i)
        {
            scanf("%s",Map[i] + 1);
            for(int j = 1; j <= n; ++j) if(Map[i][j] == S) bex = i, bey = j;
            else if(Map[i][j] == D) aimx = i, aimy = j;
            else if(Map[i][j] == X) cnt++;
        }
        if(m*n - cnt < t) {
            printf("NO\n");
            continue;
        }
        Map[bex][bey] = X;
        if(DFS(bex,bey,0)) {
            printf("YES\n");
        } else printf("NO\n");
    }
    return 0;
}

ZOJ 2110 DFS

标签:

原文地址:http://www.cnblogs.com/aoxuets/p/4747210.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!