A. The Fault in Our Cubes 暴力dfs

Posted stupid_one

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}
View Code

 

以上是关于A. The Fault in Our Cubes 暴力dfs的主要内容,如果未能解决你的问题,请参考以下文章

UVA 10733 - The Colored Cubes(Ploya)

A. The Child and Homework

__twice,the postman refused to deliver our letters unless we chained our dog up.

Packers And Movers Delhi : Charges Are The Real Value Of Our Work

安装Kubernetes报错:The connection to the server localhost:8080 was refused

Yii2 Lesson - 01 Installing the framework and Creating Our First Application