noip模拟赛 c

分析:一道比较难的爆搜题.首先要把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的更多相关文章
- NOIP模拟赛20161022
NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...
- contesthunter暑假NOIP模拟赛第一场题解
contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...
- NOIP模拟赛 by hzwer
2015年10月04日NOIP模拟赛 by hzwer (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...
- 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程
数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...
- 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...
- 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...
- 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...
- CH Round #58 - OrzCC杯noip模拟赛day2
A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...
- CH Round #52 - Thinking Bear #1 (NOIP模拟赛)
A.拆地毯 题目:http://www.contesthunter.org/contest/CH%20Round%20%2352%20-%20Thinking%20Bear%20%231%20(NOI ...
- CH Round #49 - Streaming #4 (NOIP模拟赛Day2)
A.二叉树的的根 题目:http://www.contesthunter.org/contest/CH%20Round%20%2349%20-%20Streaming%20%234%20(NOIP 模 ...
随机推荐
- Visual Studio AI
Visual Studio 完全AI手册 - 从0开始配置环境 https://www.cnblogs.com/ms-uap/p/9123033.html 零.前提条件 一台能联网的电脑,使用win1 ...
- [CF391E2]Three Trees
https://zybuluo.com/ysner/note/1246822 题面 有三棵树,建两条边让他们相连,最大化所有点对距离之和. \(40pts\ n\leq1000\) \(100pts\ ...
- [App Store Connect帮助]三、管理 App 和版本(3)查找 App
在“我的 App”中,使用工具栏控件快速查找 App. 搜索 App: 在搜索栏中输入文本. 您可以输入 App 的名称.Apple ID(App 标识符).套装 ID,或 SKU.套装 ID 和 S ...
- hadoop一主一从部署(1)
一.安装前说明 主机IP:192.168.132.128 从机IP:192.168.132.129 1. 所有的安装包我放在了/root/这个目录下,你要根据自己情况去修改,这点必须注意 2. 采用的 ...
- Servlet访问路径的两种方式、Servlet生命周期特点、计算服务启动后的访问次数、Get请求、Post请求
Servlet访问路径的两种方式: 1:注解 即在Servlet里写一个@WebServlet @WebServlet("/myServlet") 2:配置web.xml < ...
- sql server 数据库跨服务器备份,复制监视器——快照代理,复制流程
在做数据库跨服务器复制时,查看复制监视器的快照代理,可以看到复制流程,具体如下: 初始化 连接发布服务器 设置服务器数据库兼容性级别 更新索引的统计信息 在生成快照时锁定已发布的表 复制快照数据(每个 ...
- Apache服务器防范DoS
Apache服务器对拒绝服务攻击的防范主要通过软件Apache DoS Evasive Maneuvers Module 来实现.它是一款mod_access的替代软件,可以对抗DoS攻击.该软件可 ...
- 从JavaScript的单线程执行说起
先看一段代码: 1 2 3 4 5 setTimeout(function(){ alert("a"); }, 0); while(1); alert("b&qu ...
- MyEclipse中VSS的使用详解
本文系转载,原文地址http://hi.baidu.com/yi88cheng/blog/item/13dd862f765e6b5c4fc226e5.html
- 自己写一个爬虫 copider
copider 模仿scrapy的一些写法,当然我这个是单进程的,不是异步的 1.目录 copider/copider.py #coding=utf-8 ''' Created on 2015年10月 ...