分析:一道比较难的爆搜题.首先要把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. Robert 的军队

    题目描述 Winter is coming. Robert 是个昏庸的君主,整日沉迷于吃喝玩乐,终于,当寒冬降临,他不得不组 织军队来对抗敌人. 尽管如此,他仍然是个喜欢玩耍的人,还有点强迫症,他希望 ...

  2. AD10 库下载地址

    http://wiki.altium.com/display/ADOH/Download+Libraries 最新更新库地址: http://designcontent.live.altium.com ...

  3. 【WIP】客户端JavaScript DOM

    创建: 2017/10/12 初步完成: 2017/10/15   更新: 2017/10/14 标题加上[WIP],继续完成     [TODO] 补充暂略的, 搜[略]  DOM树  概要  基本 ...

  4. 互斥的数(hash)

    1553 互斥的数  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold     题目描述 Description 有这样的一个集合,集合中的元素个数由给定的N决定, ...

  5. [Swift通天遁地]二、表格表单-(8)快速实现表单的输入验证

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  6. js判断客户端是手机端还是PC端

    封装函数: function isPC() { var userAgentInfo = navigator.userAgent; var Agents = ["Android", ...

  7. $P5017 摆渡车$

    problem 毒瘤\(DP\) #ifdef Dubug #endif #include <bits/stdc++.h> using namespace std; typedef lon ...

  8. Django总结二

    - 创建app应用与ORM操作 - Django项目project - app(应用) -> 不同的功能放在不同的app中 - 命令 : - 创建app python manage.py sta ...

  9. gerrit项目配置

    1. 相关约定说明: 1.1 gerrit服务器ip地址:192.168.130.10 1.2 gerrit服务器端用户名:gerrit 1.3 gerrit用户端管理员:admin 1.4 ssh端 ...

  10. tomcat生成catalina.out文件

    生成catalina.out方法 导语:本文为Windows下生tomcat将控制台信息输出到catalina.out文件  且 保证能实时查看日志文件的方法. 一.创建catalina.out 1. ...