zoj2110Tempter of the Bone

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

int d[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0} };
int N, M, T;
char map1[10][10];
int flag[10][10];
int sx, sy, ex, ey;
bool res;///represent the result
int wall;

void input_map() {
    char tmp;///吃掉回车符。
    for(int i = 1; i <= N; i++) {
        for(int j = 1; j <= M; j++) {
            scanf("%c", &map1[i][j]);
            if(map1[i][j] == 'S') { sx = i; sy = j; }
            if(map1[i][j] == 'D') { ex = i, ey = j; }
            if(map1[i][j] == 'X') { wall++; }
        }
        scanf("%c", &tmp);
    }
}

void dfs(int x, int y, int t) {
  //  cout << x << ' ' << y << ' ' << t << endl;
    if(map1[x][y] == 'X') {
        return;
    }
    else  if(x == ex && y == ey&& t == T) {
        res = true; return;
    }
    int temp = (T-t) - fabs(x-ex) - fabs(y-ey);
    if(temp < 0 || temp%2) { return; }      //剪枝
    int nx, ny;
    map1[x][y] = 'D';///mark
    flag[x][y] = 1;
    for(int i = 0; i < 4; i++) {
        nx = x + d[i][0];
        ny = y + d[i][1];
        if(map1[nx][ny] != 'X' && flag[nx][ny] == 0 ) {///是否可行且访问过。
            if(nx >= 1 && nx <= N && ny >= 1 && ny <= M) {///是否越界
                if(res) { return ;}//剪枝
                dfs(nx, ny, t+1);
            }
        }
    }
    map1[x][y] = '.';
    flag[x][y] = 0;
    return ;
}


int main() {
    while(scanf("%d%d%d", &N, &M, &T) != EOF) {
        if(!N && !M && !T) { break; }
        res = false;///init false;
        memset(flag, 0, sizeof(flag));
        wall = 0;///init;
        char tmp;
        scanf("%c", &tmp);///吃掉回车符。
        input_map();
        if(N*M - wall <= T) {
            cout << "NO" << endl;
            continue;
        }
        dfs(sx, sy, 0);
        if(res) {
            cout << "YES" << endl;
        }
        else {
            cout << "NO" << endl;
        }
    }
    return 0;
}


/************************
3 4 5
S...
.X.X
...D

4 4 8
.X.X
..S.
....
DX.X

4 4 5
S.X.
..X.
..XD
....
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1110
***************************/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值