A. The Fault in Our Cubes 暴力dfs

http://codeforces.com/gym/101257/problem/A

把它固定在(0,0, 0)到(2, 2, 2)上,每次都暴力dfs检查,不会超时的,因为规定在这个空间上,一不行,就会早早退出。

这样写起来比较好写。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 100 + 2;
bool vis[4][4][4];
char str[maxn];
int fx[6] = {0, 1, 0, -1, 0, 0};
int fy[6] = {1, 0, -1, 0, 0, 0};
int fz[6] = {0, 0, 0, 0, -1, 1};
bool check(int x, int y, int z) {
    if (x < 0 || y < 0 || z < 0) return false;
    if (x > 2 || y > 2 || z > 2) return false;
    if (vis[x][y][z]) return false;
    return true;
}
int op[22];
bool dfs(int cur, int x, int y, int z, int dir) {
    int tx = fx[dir] + x, ty = fy[dir] + y, tz = fz[dir] + z;
    if (cur == 27) {
        if (check(tx, ty, tz)) return true;
        else return false;
    }
    if (!check(tx, ty, tz)) return false;
    if (str[cur] == ‘I‘) {
        vis[tx][ty][tz] = true;
        if (dfs(cur + 1, tx, ty, tz, dir)) {
            return true;
        } else {
            vis[tx][ty][tz] = false;
            return false;
        }
    } else {
        vis[tx][ty][tz] = true;
        for (int i = 0; i < 6; ++i) {
            if (dir == i || op[dir] == i) continue;
            if (dfs(cur + 1, tx, ty, tz, i)) {
                return true;
            }
        }
        vis[tx][ty][tz] = false;
        return false;
    }
}
void work() {
    op[0] = 2;
    op[2] = 0;
    op[1] = 3;
    op[3] = 1;
    op[4] = 5;
    op[5] = 4;
    scanf("%s", str + 1);
    for (int i = 2; i <= 26; ++i) {
        if (str[i] == ‘E‘) {
            cout << "NO" << endl;
            return;
        }
    }
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            for (int k = 0; k < 3; ++k) {
                vis[i][j][k] = true;
                if (dfs(2, i, j, k, 0)) {
                    cout << "YES" << endl;
                    return;
                }
                vis[i][j][k] = false;
            }
        }
    }
    cout << "NO" << endl;
}

int main() {
#ifdef local
    freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
    work();
    return 0;
}

时间: 2024-08-02 02:50:07

A. The Fault in Our Cubes 暴力dfs的相关文章

hdu 5024 Wang Xifeng&#39;s Little Plot【暴力dfs,剪枝】

2014年广州网络赛的C题,也是水题.要你在一个地图中找出一条最长的路,这条路要保证最多只能有一个拐角,且只能为90度 我们直接用深搜,枚举每个起点,每个方向进行dfs,再加上剪枝. 但是如果直接写的话,那一定会特别麻烦,再加上方向这一属性也是我们需要考虑的方面,我们将从别的地方到当前点的方向编一个号:往右为0,如下图顺时针顺序编号 (往右下方向为1,往下为2......以此类推) 我们知道了当前点的坐标,来到当前点的方向,以及到目前有没有拐弯,这几个属性之后,就可以记忆化搜索了,用一个四维数组

Codeforces Round #359 (Div. 2) C. Robbers&#39; watch (暴力DFS)

题目链接:http://codeforces.com/problemset/problem/686/C 给你n和m,问你有多少对(a, b) 满足0<=a <n 且 0 <=b < m 且a的7进制和n-1的7进制位数相同 且b的7进制和m-1的7进制位数相同,还有a和b的7进制上的每位上的数各不相同. 看懂题目,就很简单了,先判断a和b的7进制位数是否超过7,不超过的话就dfs暴力枚举计算就可以了. 1 //#pragma comment(linker, "/STACK

hihoCoder 1185 连通性&#183;三(Tarjan缩点+暴力DFS)

#1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出去,就拜托小Hi和小Ho忙帮放牧. 约翰家一共有N个草场,每个草场有容量为W[i]的牧草,N个草场之间有M条单向的路径. 小Hi和小Ho需要将牛羊群赶到草场上,当他们吃完一个草场牧草后,继续前往其他草场.当没有可以到达的草场或是能够到达的草场都已经被吃光了之后,小hi和小Ho就把牛羊群赶回家. 一开

Strange Country II 暴力dfs

这题点的个数(<=50)有限, 所以可以纯暴力DFS去搜索 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <fstream> #include <cstring> #include <cmath> #include <stack> #

CodeForces 887B Cubes for Masha (暴力 || DFS)

Absent-minded Masha got set of n cubes for her birthday. At each of 6 faces of each cube, there is exactly one digit from 0 to 9. Masha became interested what is the largest natural x such she can make using her new cubes all integers from 1 to x. To

UVaLive 3401 Colored Cubes (暴力)

题意:给定n个立方体,让你重新涂尽量少的面,使得所有立方体都相同. 析:暴力求出每一种姿态,然后枚举每一种立方体的姿态,求出最少值. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostrea

ZOJ Seven-Segment Display 暴力dfs + 剪枝

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3954 0 = on     1 = off A seven segment code of permutation p is a set of seven segment code derived from the standard code by rearranging the bits into the order indicated by p. For exampl

UVa 818Cutting Chains (暴力dfs+位运算+二进制法)

题意:有 n 个圆环,其中有一些已经扣在一起了,现在要打开尽量少的环,使所有的环可以组成一条链. 析:刚开始看的时候,确实是不会啊....现在有点思路,但是还是差一点,方法也不够好,最后还是参考了网上的题解,大神们的代码就是不一样, 但还是看了好久才看懂.首先是用二进制法进行暴力,因为 n 最大才是15,不会超时的,然后就是在暴力时判断打开这些环时,剩下的是不是还存在环, 如果存在那么不是不行的,然后再判断是不是有的环有两个分支以上,因为一个环如果成链那么最多只有两个分支,所以多于两个的也是不对

HDU 5113 Black And White(暴力dfs+减枝)

题目大意:给你一个n×m的矩阵,然后给你k种颜色,每种颜色有x种,所有的个数加起来恰好为n×m个.问你让你对这个矩阵进行染色问你,能不能把所有的小方格都染色,而且相邻两个颜色不同. 思路:一开始想的是构造,先按照个数进行排序,枚举每一个位置,贪心的策略先放多的,如果可以全部放下就输出YES,以及存贮的方案,否则输出NO,但是有bug,一直不对... 正解:dfs暴力枚举每一个点,裸的话需要25!,显然会超时,需要先排个序用构造的策略,让多的先放这样可以减枝.然后再dfs就可以了. Black A