• 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
    ***************************/
    


  • 相关阅读:
    html数据属性 data-*
    切片,索引,基本数据类型
    计算机是什么
    使用CSS3和jQuery可伸缩的搜索条
    一个按钮判断两次事件,切换图标
    Javascript(jQuery)中绑定页面上所有按钮点击事件的几种方式
    jq向上无缝滚动
    js避免全局污染
    闭包:让外部函数能访问函数内的变量,让局部变量长期贮存在内存中
    position绝对剧中
  • 原文地址:https://www.cnblogs.com/jiangu66/p/2996691.html
Copyright © 2020-2023  润新知