博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF:The Fault in Our Cubes(DFS)
阅读量:6801 次
发布时间:2019-06-26

本文共 4274 字,大约阅读时间需要 14 分钟。

A. The Fault in Our Cubes
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Rula is the Human Resources Manager at Mixed Dimensions Inc. She has some very delightful puzzles for the employees to enjoy their free time at the office. One of these puzzles is the Worm Puzzle.

The puzzle consists of 27 cubic pieces of several types. The pieces are connected by a rope that runs through holes in the pieces, and allows the pieces to rotate. In order to solve the puzzle, you need to build a 3x3x3 cube.

Each piece has one of the following three types:

  • Type E represents an end cube, it has a hole at one face only. This piece is always connected to only one other piece.
  • Type I represents a cube which has two holes on opposite faces, the rope goes straight into one hole and out through the other.
  • Type L represents a cube which has two holes in two adjacent faces, the rope enters through one face, makes a 90 degree turn, and exits through the other face.

Well, Hasan has cut the rope of the puzzle by mistake, of course he didn’t want to tell Rula about it, so he bought a new rope and linked the pieces together to make a new Worm Puzzle, but he wasn't sure whether he linked them in their original order or not.

Now Rula has been trying to solve the puzzle for two days, but with no success, although she used to solve it in 30 seconds. She started to suspect that something might be wrong with the puzzle.

She asked one of the engineers at Mixed Dimensions to verify whether the puzzle is solvable or not. Unfortunately, that engineer was Hasan, and of course, the question is difficult for him to answer.

Given the type of each piece, and the order in which Hasan linked the pieces together, can you help him by finding whether the given puzzle is solvable or not?

Input

The input consists of a string of 27 letters, each letter will be either an EI, or L, which represents the type of that piece.

The first and last letters are always E.

Output

Print YES if the given puzzle is solvable, otherwise print NO.

Examples
input
EILILLLLLLILILLLLLLILILLLLE
output
YES
input
EILLLILLILLLILILLLLILILILIE
output
YES
input
EILLILLLILLILLLILLLILLLLILE
output
NO
思路:暴力dfs即可。

# include 
# include
# include
using namespace std;int fx[6] = {1, -1, 0, 0, 0, 0};int fy[6] = {0, 0, 1, -1, 0, 0};int fz[6] = {0, 0, 0, 0, 1, -1};char s[30];int vis[4][4][4];bool judge(int x, int y, int z){ if(x<0||x>2||y<0||y>2||z<0||z>2||vis[x][y][z]) return false; return true;}bool dfs(int x ,int y, int z, int icount, int pos){ char c = s[icount]; int px = x + fx[pos]; int py = y + fy[pos]; int pz = z + fz[pos]; if(icount == 27) return judge(px, py, pz); if(!judge(px, py, pz)) return false; if(c == 'I') { vis[px][py][pz] = 1; if(dfs(px, py, pz, icount+1, pos)) return true; else { vis[px][py][pz] = 0; return false; } } else if(c == 'L') { vis[px][py][pz] = 1; for(int i=0; i<6; ++i) { if(i != pos) { if(dfs(px, py, pz, icount+1, i)) return true; } } vis[px][py][pz] = 0; return false; }}int main(){ while(~scanf("%s",s+1)) { memset(vis, 0, sizeof(vis)); bool flag = false; for(int i=2; i<27; ++i) if(s[i]=='E') { flag = true; break; } if(flag) { puts("NO"); continue; } 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] = 1; if(dfs(i, j, k, 2, 0)) { flag = true; break; } vis[i][j][k] = 0; } if(flag) break; } if(flag) break; } if(flag) puts("YES"); else puts("NO"); } return 0;}

转载于:https://www.cnblogs.com/junior19/p/6729974.html

你可能感兴趣的文章
宏定义冲突
查看>>
cobbler-自动化部署
查看>>
我的友情链接
查看>>
tracepath
查看>>
java多线程基础复习
查看>>
我的友情链接
查看>>
iOS:使用minimumScaleFactor控制字体大小自适应
查看>>
Android Zxing条码扫描自定义控件(附代码)
查看>>
Netty学习笔记之Netty之初印象(一)
查看>>
centos7上安装knock
查看>>
Google 镜像站搜集
查看>>
Python 分布式进程间通讯
查看>>
用RMAN 备份异机恢复 迁移数据(一)dave
查看>>
Redis参数一览
查看>>
数据库日常运维中的几个操作建议
查看>>
Java基础-String类常用方法
查看>>
轻量级移动设备即时通讯技术MobileIMSDK的常见问题解答
查看>>
管理H3C交换机
查看>>
Java_Index
查看>>
thymeleaf th:href 多个参数传递格式
查看>>