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

***************************/


你可能感兴趣的:(one)