ZOJ 2110 DFS

狗要出门,且正好在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;
}
时间: 2024-10-14 05:38:58

ZOJ 2110 DFS的相关文章

zoj 2110 Tempter of the Bone

// zoj 2110 #include <iostream>#include <string.h>#include <stdlib.h>using namespace std; char map[9][9]; //迷宫地图int n,m,t; //迷宫的大小,及迷宫的门会在第t秒开启 int di,dj; //(di,dj):门的位置 bool escape;int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};//控制行走方向 左 右

ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)

题意  一仅仅狗要逃离迷宫  能够往上下左右4个方向走  每走一步耗时1s  每一个格子仅仅能走一次且迷宫的门仅仅在t时刻打开一次  问狗是否有可能逃离这个迷宫 直接DFS  直道找到满足条件的路径  或者走全然部可能路径都不满足 注意剪枝  当前位置为(r,c)  终点为(ex,ey) 剩下的时间为lt  当前点到终点的直接距离为  d=(ex-r)+(ey-c)   若多走的时间rt=lt-d<0 或为奇数时  肯定是不可能的  能够自己在纸上画一下 每一个点仅仅能走一次的图  走弯路的话多

HDU 1010 &amp;&amp; ZOJ 2110 Tempter of the bone (DFS + 奇偶剪枝)

Problem Description: The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desp

zoj 2110 很好的dfs+奇偶剪枝

//我刚开始竟然用bfs做,不断的wa,bfs是用来求最短路的而这道题是求固定时间的 //剪纸奇偶剪枝加dfs #include<stdio.h> #include<queue> #include<math.h> #include<string.h> using namespace std; #define N 10 char ma[N][N]; struct node { int x,y,step; }ss,tt; int dis[4][2]={1,0,-

ZOJ 1204:(DFS)

输出一串数所能构成的所有等式,按照加数从大到小,算式从长到短排列 一开始因为无法按照要求输出纠结了好一阵~~最后看了大神的代码才发现只需要每次dfs规定长度就好了~ #include"cstdio" #include"cstring" #include"algorithm" #define MAXN 40 using namespace std; int num[MAXN],n,front,rear,ok; int stack[MAXN]; in

ZOJ 1002 DFS

Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small c

ZOJ 1008 Gnome Tetravex (DFS + 剪枝)

Gnome Tetravex 题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=8 题意:有N*N个方格,每个方格分为上下左右四个部分,每个部分填数字.现在要求重排方块,使得每两个有边相连的方块对应的数字相同. 思路:就是一个简单的搜索,我想了个剪枝,将上下左右四个方向上每个数字对应的是哪几个方块记录下来,但是这个剪枝并没有起很大的作用,还是T了.后来才发现,如果有很多个方块是相同的,会重复搜索,所以需要将相同的方块一起处

stack+DFS ZOJ 1004 Anagrams by Stack

题目传送门 1 /* 2 stack 容器的应用: 要求字典序升序输出,所以先搜索入栈的 3 然后逐个判断是否满足答案,若不满足,回溯继续搜索,输出所有符合的结果 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <stack> 9 #include <cmath> 10 #include <cstring> 11 #inc

dfs+剪枝 zoj 3631

X - Watashi's BG Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3631 Appoint description:  System Crawler  (2015-04-15) Description Watashi is the couch of ZJU-ICPC Team and he is very kind heart