分析:一道比较难的爆搜题.首先要把9个块的信息存下来,记录每个块上下左右位置的颜色,然后记录每一排每一列能否操作,之后就是bfs了。在bfs的时候用一个数记录状态,第i位表示原来的第i个块现在在哪个位置,我们可以通过这个状态来解码得到信息,也可以来判重,只是数组开不下,需要用一个map。然后就是如何判断是否连通.首先把所有块还原成一张图,然后我们可以数每个颜色连通的数量,看看是不是等于36就可以了,这里用dfs来判断.

学习了一种比较强的记录状态的方式:记录相对位置,既可以判重,也可以还原图.

#include <cstdio>
#include <map>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; int canx[], cany[], zhuangtai[];
int can[], vis[][], a[][];
map <long long, int> flag; struct node
{
int col[];
}e[]; struct node2
{
long long stu;
int dist;
}; long long hashh(int a, int b, int c, int d, int e, int f, int g, int h, int i)
{
return (((((((a * + b) * + c) * + d) * + e) * + f) * + g) * + h) * + i;
} int dfs(int col, int x, int y, int last)
{
//printf("%d %d %d %d\n", col, x, y, last);
int ret = ;
if (vis[x][y] || (a[x][y] != col && a[x][y] != ) || (a[x][y] == && last == ))
return ret;
vis[x][y] = ;
if (col == a[x][y])
ret++;
if (x > )
ret += dfs(col, x - , y, a[x][y]);
if (x < )
ret += dfs(col, x + , y, a[x][y]);
if (y > )
ret += dfs(col, x, y - , a[x][y]);
if (y < )
ret += dfs(col, x, y + , a[x][y]);
return ret;
} bool check(long long stu)
{
//printf("%lld\n", stu);
int res = ;
memset(can, , sizeof(can));
memset(a, , sizeof(a));
for (int i = ; i >= ; i--)
for (int j = ; j >= ; j--)
{
int x = stu % ;
a[i * ][j * + ] = e[x].col[];
a[i * + ][j * + ] = e[x].col[];
a[i * + ][j * ] = e[x].col[];
a[i * + ][j * + ] = e[x].col[];
stu /= ;
}
for (int i = ; i <= ; i++)
{
for (int j = ; j <= ; j++)
printf("%d ", a[i][j]);
printf("\n");
}
printf("\n");
for (int i = ; i < ; i++)
for (int j = ; j < ; j++)
for (int k = ; k <= ; k++)
if (!can[k] && a[i][j] == k)
{
memset(vis, , sizeof(vis));
res += dfs(k, i, j, );
can[k] = ;
}
if (res == )
return true;
return false;
} void bfs()
{
queue <node2> q;
node2 temp;
temp.stu = ;
temp.dist = ;
q.push(temp);
flag[] = ;
while (!q.empty())
{
node2 u = q.front();
q.pop();
long long stu = u.stu;
int dist = u.dist;
//printf("%lld %d\n", stu, dist);
if (check(stu))
{
printf("%d\n", dist);
return;
}
//printf("flag\n");
for (int i = ; i >= ; i--)
{
zhuangtai[i] = stu % ;
stu /= ;
}
if (!canx[])
{
long long nstu = hashh(zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[]);
//printf("%lld\n", nstu);
if (!flag[nstu])
{
node2 temp;
temp.stu = nstu;
temp.dist = dist + ;
q.push(temp);
}
nstu = hashh(zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[]);
//printf("%lld\n", nstu);
if (!flag[nstu])
{
node2 temp;
temp.stu = nstu;
temp.dist = dist + ;
q.push(temp);
}
}
if (!canx[])
{
long long nstu = hashh(zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[]);
if (!flag[nstu])
{
node2 temp;
temp.stu = nstu;
temp.dist = dist + ;
q.push(temp);
}
nstu = hashh(zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[]);
if (!flag[nstu])
{
node2 temp;
temp.stu = nstu;
temp.dist = dist + ;
q.push(temp);
}
} if (!canx[])
{
long long nstu = hashh(zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[]);
if (!flag[nstu])
{
node2 temp;
temp.stu = nstu;
temp.dist = dist + ;
q.push(temp);
}
nstu = hashh(zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[]);
if (!flag[nstu])
{
node2 temp;
temp.stu = nstu;
temp.dist = dist + ;
q.push(temp);
}
} if (!cany[])
{
long long nstu = hashh(zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[]);
if (!flag[nstu])
{
node2 temp;
temp.stu = nstu;
temp.dist = dist + ;
q.push(temp);
}
nstu = hashh(zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[]);
if (!flag[nstu])
{
node2 temp;
temp.stu = nstu;
temp.dist = dist + ;
q.push(temp);
}
} if (!cany[])
{
long long nstu = hashh(zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[]);
if (!flag[nstu])
{
node2 temp;
temp.stu = nstu;
temp.dist = dist + ;
q.push(temp);
}
nstu = hashh(zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[]);
if (!flag[nstu])
{
node2 temp;
temp.stu = nstu;
temp.dist = dist + ;
q.push(temp);
}
} if (!cany[])
{
long long nstu = hashh(zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[]);
if (!flag[nstu])
{
node2 temp;
temp.stu = nstu;
temp.dist = dist + ;
q.push(temp);
}
nstu = hashh(zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[], zhuangtai[]);
if (!flag[nstu])
{
node2 temp;
temp.stu = nstu;
temp.dist = dist + ;
q.push(temp);
}
}
}
} void init()
{
for (int i = ; i < ; i++)
{
int t;
for (int j = ; j < ; j++)
{
char ch;
cin >> ch;
if (ch == 'R')
e[i].col[j] = ;
if (ch == 'G')
e[i].col[j] = ;
if (ch == 'B')
e[i].col[j] = ;
if (ch == 'O')
e[i].col[j] = ;
}
cin >> t;
if (t == )
{
canx[i / ] = ;
cany[i % ] = ;
}
}
} int main()
{
init();
bfs(); return ;
}

noip模拟赛 c的更多相关文章

  1. NOIP模拟赛20161022

    NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...

  2. contesthunter暑假NOIP模拟赛第一场题解

    contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...

  3. NOIP模拟赛 by hzwer

    2015年10月04日NOIP模拟赛 by hzwer    (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...

  4. 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程

    数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...

  5. 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...

  6. 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...

  7. 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...

  8. CH Round #58 - OrzCC杯noip模拟赛day2

    A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...

  9. CH Round #52 - Thinking Bear #1 (NOIP模拟赛)

    A.拆地毯 题目:http://www.contesthunter.org/contest/CH%20Round%20%2352%20-%20Thinking%20Bear%20%231%20(NOI ...

  10. CH Round #49 - Streaming #4 (NOIP模拟赛Day2)

    A.二叉树的的根 题目:http://www.contesthunter.org/contest/CH%20Round%20%2349%20-%20Streaming%20%234%20(NOIP 模 ...

随机推荐

  1. leetcode快排相关

    leetcode:75颜色分类(3way).215数组中的第K个最大元素(normal) 3way private static void quick3waySort(int[] arr, int l ...

  2. 使用免费SSL证书让网站支持HTTPS访问

    参考掘金的文章,掘金的文章最详细. https://juejin.im/post/5a31cbf76fb9a0450b6664ee 先检查是否存在 EPEL 源: # 进入目录检查是否存在 EPEL ...

  3. C语言作用于修饰符

    之前就遇到了坑,莫名其妙报错.   总结下: extern   声明在其他文件里 static     仅当前文件可见

  4. Codeforces 612D 前缀和处理区间问题

    传送门:http://codeforces.com/problemset/problem/612/D (转载请注明出处谢谢) 题意: 给出数字n和k,n表示接下来将输入n个在x轴上的闭区间[li,ri ...

  5. 阿里邮箱绑定Foxmail失败的解决办法

    收件服务器地址: POP 服务器地址:pop3.mxhichina.com 端口110,SSL 加密端口995 或 IMAP 服务器地址:imap.mxhichina.com 端口143,SSL 加密 ...

  6. JSP所需要掌握的部分

    JSP基本语法 指令 <%@ 指令%> JSP指令是JSP的引擎 主要的两种指令是page和include(taglib) <%@ page import="java.ut ...

  7. [转]sed常用命令总结

    转自:http://blog.chinaunix.net/uid-26963748-id-3249732.html 一.Sed简介 Sed:Stream Editor  流式编辑器 又称行编辑器,每次 ...

  8. Web开发中跨域的几种解决方案

    同domain(或ip),同端口,同协议视为同一个域,一个域内的脚本仅仅具有本域内的权限,可以理解为本域脚本只能读写本域内的资源,而无法访问其它域的资源.这种安全限制称为同源策略. 出于安全考虑,HT ...

  9. node的api

    一. 1.url: 绝对URI http://user:pass@www.example.com:80/dir/index.html?uid=1#ch1 协议 登录信息 服务器地址 端口 文件路径 查 ...

  10. csf 课件转化为wmv正常格式

    1. 下载csf文件到本地:如下图 2.从下面百度网盘下载到本地: https://pan.baidu.com/s/1BBbgq  n85a 3.安装并出现下面图标,点击打开 4. 运行如下图 5.  ...