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 模 ...
随机推荐
- thinkphp调试手段
使用ThinkPHP应该掌握的调试手段经常看到有人问到findAll的返回数据类型是什么之类的问题,以及出错了不知道什么原因的情况,其实还是没有熟悉ThinkPHP内置的调试手段和方法,抛开IDE本身 ...
- zhw大神线段树姿势
; i<; i++) tree[i][]=tree[i][]=i; ; i>=; i--) tree[i][]=tree[i+i][], tree[i][]=tree[i+i+][]; v ...
- Java内存泄漏及对象引用的4种类型
转自: http://www.cnblogs.com/qq78292959/archive/2011/07/25/2116123.html 总结: 引用分类: 强引用,弱引用,软引用,虚引用.虚引用必 ...
- python框架之Flask基础篇(三)-------- 模版的操作
1.flask特有的变量和函数: 变量:g.session.request.config 函数:url_for().get_flashed_messages()这个函数注意了啊,记住这是个函数,别忘了 ...
- Python--10、生产者消费者模型
生产者消费者模型(★) 平衡生产线程和消费线程的工作能力来提高程序的整体处理数据的速度.程序中有两类角色:生产数据.消费数据实现方式:生产->队列->消费. 通过一个容器来解决生产者和消费 ...
- fcc 响应式框架Bootstrap 练习2
text-primary 属性值使标题直接变成了红色,text-center使标题直接居中 <h2 class="text-primary text-center"> ...
- android学习之路资料集合
版权声明:本文为 stormzhang 原创文章,可以随意转载,但必须在明确位置注明出处!!! 这篇博客背后的故事 一路走来很不容易,刚好知乎上被人邀请回答如何自学android编程, 就借这个机会在 ...
- XML——读与写
XML写入 private static void writeXml() { using (XmlTextWriter xml = new XmlTextWriter(@"C:\Users\ ...
- Tcl之looping
1 While loop while test body The while command evaluates test as an expression. If test is true, the ...
- ubuntu下sudo命令不能使用问题
不知道从什么时候开始,ctrl+alt+F1进入命令行之后,登录成功.使用sudo命令,不能使用....被坑了很久. 解决方法: 出现 [sudo ] username !!! 之后,在输入一遍 密码 ...