时间限制: 1 Sec 内存限制: 128 MB
题目描述
Flow Free is a puzzle that is played on a 2D grid of cells, with some cells marked as endpoints of certain colors and the rest of cells being blank. To solve the puzzle, you have to connect each pair of colored endpoints with a path, following these rules:
there is a path connecting two points with the same color, and that path also has that color
all cells in the grid are used and each cell belongs to exactly one path (of the same color as the endpoints of the path)
The rules imply that different colored paths cannot intersect.
The path is defined as a connected series of segments where each segment connects two neighbouring cells. Two cells are neighbours if they share a side (so all segments are either horizontal or vertical). By these definitions and rules above, each colored cell will be an endpoint of exactly one segment and each blank cell will be an endpoint of exactly two segments.
In this problem we will consider only the 4×4 puzzle, with 3 or 4 pairs of colored endpoints given.
Your task is to determine if a given puzzle is solvable or not.
输入
The input consists of 4 lines, each line containing 4 characters. Each character is from the set {R, G,B, Y,W}whereW denotes the blank cells and the other characters denote endpoints with the specified color. You are guaranteed that there will be exactly 3 or 4 pairs of colored cells. If there are 3 colors in the grid, Y will be omitted.
输出
On a single line output either “solvable” or “not solvable” (without the quotes).
样例输入
RGBW
WWWW
RGBY
YWWW
样例输出
solvable

开始读错了题,没注意要连上所有的块。
暴力枚举每个W块的颜色,每种情况下从起点广搜终点,不用vis数组剪枝而采用记录路径(状压)可以保证搜到每种路径,搜到终点时判断是否走过了所有相同颜色的格子。
第二天仔细想想,还是写的太麻烦了,实际上直接深搜就好了…

#define IN_LB() freopen("F:\\in.txt","r",stdin)
#define IN_PC() freopen("C:\\Users\\hz\\Desktop\\in.txt","r",stdin)
#include <bits/stdc++.h> using namespace std;
const int dirx[] = {-1,0,1,0};
const int diry[] = {0,1,0,-1};
const char col[] ="RGBY";
char mapp[5][5]; struct node {
int x,y,route,step;
node() {}
node(int x,int y,int route,int step):x(x),y(y),route(route),step(step) {}
} cR[2],cG[2],cB[2],cY[2]; int cntR,cntG,cntB,cntY;
vector <int> v; int xytoInd(int x,int y){
return x*4+y;
} bool bfs(char color,node st,node ed,int cnum) {
queue<node> q;
q.push(st);
while(!q.empty()) {
int x = q.front().x,y=q.front().y,route = q.front().route,step = q.front().step;
q.pop();
if(x==ed.x&&y==ed.y&&cnum+1==step)return true;
for(int i=0; i<4; i++) {
int xx = x+dirx[i],yy=y+diry[i];
if(xx>=0&&xx<4&&yy>=0&&yy<4&&mapp[xx][yy]==color&&((route&(1<<xytoInd(xx,yy)))==0)) {
q.push(node(xx,yy,route|(1<<xytoInd(xx,yy)),step+1));
}
}
}
return false;
} int main() {
// IN_PC();
for(int i=0; i<4; i++) {
scanf("%s",mapp[i]);
}
for(int i=0; i<4; i++) {
for(int j=0; j<4; j++) {
if(mapp[i][j]=='W') {
v.push_back(i*4+j);
}
if(mapp[i][j]=='R') {
cR[cntR++] = node(i,j,1<<xytoInd(i,j),0);
}
if(mapp[i][j]=='G') {
cG[cntG++] = node(i,j,1<<xytoInd(i,j),0);
}
if(mapp[i][j]=='B') {
cB[cntB++] = node(i,j,1<<xytoInd(i,j),0);
}
if(mapp[i][j]=='Y') {
cY[cntY++] = node(i,j,1<<xytoInd(i,j),0);
}
}
}
int num = v.size();
int flag = 0;
if(num==8) {
int sumsta = pow(4,8);
for(int i=0; i<sumsta; i++) {
int c = i,cn[4]={0};
for(int j=0; j<8; j++) {
int x = v[j]/4,y = v[j]%4;
mapp[x][y] = col[c%4];
cn[c%4]++;
c/=4;
}
if(bfs('R',cR[0],cR[1],cn[0])
&&bfs('G',cG[0],cG[1],cn[1])
&&bfs('B',cB[0],cB[1],cn[2])
&&bfs('Y',cY[0],cY[1],cn[3])) {
flag = 1;
break;
}
}
} else if(num==10) {
int sumsta = pow(3,10);
for(int i=0; i<sumsta; i++) {
int c = i,cn[3]={0};
for(int j=0; j<10; j++) {
int x = v[j]/4,y = v[j]%4;
mapp[x][y] = col[c%3];
cn[c%3]++;
c/=3;
}
if(bfs('R',cR[0],cR[1],cn[0])
&&bfs('G',cG[0],cG[1],cn[1])
&&bfs('B',cB[0],cB[1],cn[2])) {
flag = 1;
break;
}
}
}
if(flag)printf("solvable\n");
else printf("not solvable\n");
return 0;
}

【暴力枚举&BFS】Flow Free @RMRC2017/upcexam5124的更多相关文章

  1. CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)

    题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ...

  2. 2014牡丹江网络赛ZOJPretty Poem(暴力枚举)

    /* 将给定的一个字符串分解成ABABA 或者 ABABCAB的形式! 思路:暴力枚举A, B, C串! */ 1 #include<iostream> #include<cstri ...

  3. HNU 12886 Cracking the Safe(暴力枚举)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...

  4. 51nod 1116 K进制下的大数 (暴力枚举)

    题目链接 题意:中文题. 题解:暴力枚举. #include <iostream> #include <cstring> using namespace std; ; ; ch ...

  5. Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举

    题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...

  6. bzoj 1028 暴力枚举判断

    昨天梦到这道题了,所以一定要A掉(其实梦到了3道,有两道记不清了) 暴力枚举等的是哪张牌,将是哪张牌,然后贪心的判断就行了. 对于一个状态判断是否为胡牌,1-n扫一遍,然后对于每个牌,先mod 3, ...

  7. POJ-3187 Backward Digit Sums (暴力枚举)

    http://poj.org/problem?id=3187 给定一个个数n和sum,让你求原始序列,如果有多个输出字典序最小的. 暴力枚举题,枚举生成的每一个全排列,符合即退出. dfs版: #in ...

  8. hihoCoder #1179 : 永恒游戏 (暴力枚举)

    题意: 给出一个有n个点的无向图,每个点上有石头数个,现在的游戏规则是,设置某个点A的度数为d,如果A点的石子数大于等于d,则可以从A点给每个邻接点发一个石子.如果游戏可以玩10万次以上,输出INF, ...

  9. CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)

    问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...

随机推荐

  1. mysql把查询结果集插入到表理

    把表B的内容插入到表A INSERT INTO 1111_0 SELECT*FROM report_0 把查询结果集插入到表中 insert into A(a,b,c) select from B(a ...

  2. python之logging

    1.简单使用 # CRITICAL, ERROR, WARNING, INFO, DEBUG) cewid import logging logging.basicConfig(level=loggi ...

  3. mysql配置完半同步复制之后报错[ERROR] The server quit without updating PID file

    修改配置,MySQL启动报:[ERROR] The server quit without updating PID file [root@localhost mysql]# /etc/init.d/ ...

  4. 部署Tomcat及nginx负载均衡

    Web应用服务器的选择   (1)IBM的WebSphere及Oracle的WebLogic 性能高,但价格也高   (2)Tomcat 性价比高 Tomcat服务器是一个免费的开放源代码的Web应用 ...

  5. CodeForces 516A Drazil and Factorial 动态规划

    原文链接http://www.cnblogs.com/zhouzhendong/p/8990592.html 题目传送门 - CodeForces 516A 题意 对于一个正整数$x$,$f(x)=x ...

  6. List接口相对于Collection接口的特有方法

    [添加功能] 1 void add(int index,Object element); // 在指定位置添加一个元素. [获取功能] 1 Object get(int index); // 获取指定 ...

  7. B2B、B2C、C2C、O2O

    B2B:企业对企业 B2B (也有写成 BTB)是指企业对企业之间的营销关系,它将企业内部网,通过 B2B 网站与客户紧密结合起来,通过网络的快速反应,为客户提供更好的服务,从而促进企业的业务发展(B ...

  8. Visual Studio 2019及其注册码

    Visual Studio 2019 更快地进行代码编写.更智能地执行操作.使用同类最佳IDE 创建未来.     下载Visual Studio         使用从初始设计到最终部署的完整工具集 ...

  9. BUG总是存在的

    遇到了一个Bug 前段时间,添加功能的时候,在其他页面的Html中(django)的python调用{{}}中不小心按多了一个空格. 这导致这个值在读取的时候,读取多了一个空格:split的时候,多分 ...

  10. python数据结构之选择排序

    选择排序(select_sort)是一个基础排序,它主要通过查找已给序列中的元素的最大或者最小元素,然后将其放在序列的起始位置或者结束位置,并通过多次这样的循环完成对已知序列的排序,在我们对n个元素进 ...