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 模 ...
随机推荐
- MSP430:PWM产生
#define PWM BIT6 // Description: This program generates one PWM output on ...
- PCB genesis SET取中心点--算法实现
最新ICS工厂有一项incam脚本新需求,这里介绍5种解决方法解决 需求如下图所示:绿色所圈处是是需求出的中心点(图形间距一致归为一类并计算中心点坐标) 前题条件:1.一个SET里面可能有多个CAM, ...
- codevs1154能量项链(环形dp,区间dp)
1154 能量项链 2006年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在Mars星球上 ...
- [Swift通天遁地]八、媒体与动画-(12)CoreText框架中的字体的FontMetrics布局信息
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- TypeScript `unknown` 类型
unknown 字面理解和 any 其实没差,任何类型都可赋值给它,但有一点, Anything is assignable to unknown, but unknown isn't assigna ...
- Java并发编程系列之Semaphore详解
简单介绍 我们以饭店为例,假设饭店只有三个座位,一开始三个座位都是空的.这时如果同时来了三个客人,服务员人允许他们进去用餐,然后对外说暂无座位.后来的客人必须在门口等待,直到有客人离开.这时,如果有一 ...
- ACM_Repeating Characters
Repeating Characters Time Limit: 2000/1000ms (Java/Others) Problem Description: For this problem, yo ...
- 题解报告:hdu 1846 Brave Game(巴什博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1846 Problem Description 十年前读大学的时候,中国每年都要从国外引进一些电影大片, ...
- 为什么选择Android Studio 而是 Eclipse
Android Studio 现在的版本已经比较稳定了,刚出来时也是各种BUG,自己用了下,摸索了一天,感觉挺好的. 优点之一:代码提示和搜索功能非常强大,非常智能. 1).自定义theme有个名字叫 ...
- Python--10、线程
线程 每个进程里都有一个控制线程,进程不是一个执行单位,线程是执行单位,进程是资源单位(资源隔离).进程下可以开多个线程,多线程共享进程内的资源.创建线程的速度比创建进程的速度快,因为创建线程不需要再 ...