hdu 4531 bfs(略难)
题目链接:点我
第一次不太清楚怎么判重,现在懂了,等下次再做
/*
*HDU 4531
*BFS
*注意判重
*/ #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <iostream>
using namespace std; char str[][][];
char g[][][]; map<int,int>mp;
queue<int>q;
bool move[];//是否可以移动,0~5对应1~3行,1~3列 //并查集判断连通
int F[];
int find(int x)
{
if(F[x]==-)return x;
return F[x]=find(F[x]);
}
void bing(int x,int y)
{
int t1=find(x);
int t2=find(y);
if(t1!=t2)F[t1]=t2;
}
//并查集判断是不是连通
bool judge(int state)//判断状态state是不是连通
{
char temp[];
int t[];
sprintf(temp,"%09d",state);
for(int i=;i<;i++)t[i]=temp[i]-'';
for(int i=;i<;i++)
for(int j=;j<;j++)
{
int p=t[*i+j];
int x=p/;
int y=p%;
strcpy(g[i][j],str[x][y]);
}
memset(F,-,sizeof(F));
for(int i=;i<;i++)
for(int j=;j<;j++)
{
if(g[i][j][]==g[i][j][])bing(*i+*j,*i+*j+);
if(g[i][j][]==g[i][j][])bing(*i+*j,*i+*j+);
if(g[i][j][]==g[i][j][])bing(*i+*j+,*i+*j+);
if(g[i][j][]==g[i][j][])bing(*i+*j+,*i+*j+);
}
for(int i=;i<;i++)
{
if(g[i][][]==g[i][][])bing(*i+,*i++);
if(g[i][][]==g[i][][])bing(*i++,*i++);
}
for(int j=;j<;j++)
{
if(g[][j][]==g[][j][])bing(*j+,+*j+);
if(g[][j][]==g[][j][])bing(+*j+,+*j+);
}
int R=-,G=-,B=-,O=-;
for(int i=;i<;i++)
for(int j=;j<;j++)
for(int k=;k<;k++)
{
int t1=find(*i+*j+k);
if(g[i][j][k]=='R')
{
if(R==-)R=t1;
else if(t1!=R)return false;
}
else if(g[i][j][k]=='G')
{
if(G==-)G=t1;
else if(t1!=G)return false;
}
else if(g[i][j][k]=='B')
{
if(B==-)B=t1;
else if(t1!=B)return false;
}
else
{
if(O==-)O=t1;
else if(t1!=O)return false;
}
}
return true;
}
int bfs()
{
mp.clear();
while(!q.empty())q.pop();
int tmp,now;
char ss1[],ss2[];
tmp=; //初始是012345678
mp[tmp]=;
q.push(tmp);
while(!q.empty())
{
tmp=q.front();
q.pop();
if(judge(tmp))return mp[tmp];
sprintf(ss1,"%09d",tmp); for(int i=;i<;i++)
for(int j=;j<;j++)
{
int t=ss1[*i+j]-'';
strcpy(g[i][j],str[t/][t%]);
}
//第一行的左右移动
if(move[])
{
strcpy(ss2,ss1);
ss2[]=ss1[];
ss2[]=ss1[];
ss2[]=ss1[];
now=;
for(int i=;i<;i++)
{
now*=;
now+=ss2[i]-'';
}
if(mp.find(now)==mp.end())
{
mp[now]=mp[tmp]+;
q.push(now);
} ss2[]=ss1[];
ss2[]=ss1[];
ss2[]=ss1[];
now=;
for(int i=;i<;i++)
{
now*=;
now+=ss2[i]-'';
}
if(mp.find(now)==mp.end())
{
mp[now]=mp[tmp]+;
q.push(now);
} } //第二行的左右移动
if(move[])
{
strcpy(ss2,ss1);
ss2[]=ss1[];
ss2[]=ss1[];
ss2[]=ss1[];
now=;
for(int i=;i<;i++)
{
now*=;
now+=ss2[i]-'';
}
if(mp.find(now)==mp.end())
{
mp[now]=mp[tmp]+;
q.push(now);
}
ss2[]=ss1[];
ss2[]=ss1[];
ss2[]=ss1[];
now=;
for(int i=;i<;i++)
{
now*=;
now+=ss2[i]-'';
}
if(mp.find(now)==mp.end())
{
mp[now]=mp[tmp]+;
q.push(now);
}
} //第三行的左右移动
if(move[])
{
strcpy(ss2,ss1);
ss2[]=ss1[];
ss2[]=ss1[];
ss2[]=ss1[];
now=;
for(int i=;i<;i++)
{
now*=;
now+=ss2[i]-'';
}
if(mp.find(now)==mp.end())
{
mp[now]=mp[tmp]+;
q.push(now);
} ss2[]=ss1[];
ss2[]=ss1[];
ss2[]=ss1[];
now=;
for(int i=;i<;i++)
{
now*=;
now+=ss2[i]-'';
}
if(mp.find(now)==mp.end())
{
mp[now]=mp[tmp]+;
q.push(now);
} } //第一列的左右移动
if(move[])
{
strcpy(ss2,ss1);
ss2[]=ss1[];
ss2[]=ss1[];
ss2[]=ss1[];
now=;
for(int i=;i<;i++)
{
now*=;
now+=ss2[i]-'';
}
if(mp.find(now)==mp.end())
{
mp[now]=mp[tmp]+;
q.push(now);
} ss2[]=ss1[];
ss2[]=ss1[];
ss2[]=ss1[];
now=;
for(int i=;i<;i++)
{
now*=;
now+=ss2[i]-'';
}
if(mp.find(now)==mp.end())
{
mp[now]=mp[tmp]+;
q.push(now);
} } //第二列的左右移动
if(move[])
{
strcpy(ss2,ss1);
ss2[]=ss1[];
ss2[]=ss1[];
ss2[]=ss1[];
now=;
for(int i=;i<;i++)
{
now*=;
now+=ss2[i]-'';
}
if(mp.find(now)==mp.end())
{
mp[now]=mp[tmp]+;
q.push(now);
} ss2[]=ss1[];
ss2[]=ss1[];
ss2[]=ss1[];
now=;
for(int i=;i<;i++)
{
now*=;
now+=ss2[i]-'';
}
if(mp.find(now)==mp.end())
{
mp[now]=mp[tmp]+;
q.push(now);
} } //第三列的左右移动
if(move[])
{
strcpy(ss2,ss1);
ss2[]=ss1[];
ss2[]=ss1[];
ss2[]=ss1[];
now=;
for(int i=;i<;i++)
{
now*=;
now+=ss2[i]-'';
}
if(mp.find(now)==mp.end())
{
mp[now]=mp[tmp]+;
q.push(now);
} ss2[]=ss1[];
ss2[]=ss1[];
ss2[]=ss1[];
now=;
for(int i=;i<;i++)
{
now*=;
now+=ss2[i]-'';
}
if(mp.find(now)==mp.end())
{
mp[now]=mp[tmp]+;
q.push(now);
}
} }
return -;
} int main()
{
int T;
scanf("%d",&T);
int iCase=;
while(T--)
{
iCase++;
printf("Case #%d: ",iCase);
for(int i=;i<;i++)move[i]=true;
for(int i=;i<;i++)
for(int j=;j<;j++)
{
scanf("%s",&str[i][j]);
if(str[i][j][]=='')//所在的列和行不能移动
{
move[i]=false;
move[+j]=false;
}
}
printf("%d\n",bfs());
}
return ;
}
hdu 4531 bfs(略难)的更多相关文章
- HDU 4531 bfs/康拓展开
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4531 吉哥系列故事——乾坤大挪移 Time Limit: 2000/1000 MS (Java/Othe ...
- hdu 5040 BFS 多维化处理图
http://acm.hdu.edu.cn/showproblem.php?pid=5040 跟这一题http://blog.csdn.net/u011026968/article/details/3 ...
- C - 小明系列故事――捉迷藏 HDU - 4528 bfs +状压 旅游-- 最短路+状压
C - 小明系列故事――捉迷藏 HDU - 4528 这个题目看了一下题解,感觉没有很难,应该是可以自己敲出来的,感觉自己好蠢... 这个是一个bfs 用bfs就很好写了,首先可以预处理出大明和二明能 ...
- HDU 2822 (BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...
- HDU 1180 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...
- HDU 2531 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...
- HDU 5025 (BFS+记忆化状压搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5025 题目大意: 迷宫中孙悟空救唐僧,可以走回头路.必须收集完钥匙,且必须按顺序收集.迷宫中还有蛇, ...
- HDU 1429 (BFS+记忆化状压搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1429 题目大意:最短时间内出迷宫,可以走回头路,迷宫内有不同的门,对应不同的钥匙. 解题思路: 要是 ...
- HDU 1026 (BFS搜索+优先队列+记录方案)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...
随机推荐
- MVC中检测到有潜在危险的 Request.Form 值
在做mvc项目时,当使用xhedit or.ueditor编辑器时,点击提交时,编辑器中的内容会带有html标签提交给服务器,这时就是会报错,出现如下内容: “/”应用程序中的服务器错误. 从客户端( ...
- csv导入mysql提示错误[Error Code] 1290 - The MySQL server is running with the --secure-file-priv option 解决方法【转】
解决方法: 1.进入mysql查看secure_file_prive的值 mysql>SHOW VARIABLES LIKE "secure_file_priv"; secu ...
- 使用Cache缓存
存放位置:服务器内存,用于频繁访问且不轻易更改的内容缓存. string CacheKey = "CT1"; //检索指定项, object objModel = Cache.Ge ...
- MAVLink v1.0详解——结构
本文针对 MAVLink v1.0版本,协议版本:3. MAVLink是为微型飞行器MAV(Micro Air Vehicle)设计的(LGPL)开源的通讯协议.是无人飞行器和地面站(Ground C ...
- 使用os模块实现展示目录下的文件和文件夹
Windows 10家庭中文版,Python 3.6.4 今天学习了os模块,下面是使用它开发的一个展示目录下的文件和文件夹的函数,代码如下: import os # deep大于等于1的整数,默认为 ...
- Tomcat 上传war包后 会自动部署
- SQLSERVER 2008 编辑所有或者任意行
选中表 右键选择“编辑前200行”,然后选择左上角的 sql图标,然后在右侧的SQL语句去掉 top 200 然后执行查询 就可以编辑所有的行了,可以选择自己需要写SQL,然后查询编辑. 第二种方法: ...
- Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor
Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor */--> div ...
- python2.7
python2.7支持win32.win64 下载地址:http://pan.baidu.com/s/1dE39eQ9 初学,附一个牛人的python教程地址:http://www.liaoxuefe ...
- php单双引号嵌套解决方案
代码如下: <?php $path = "./"; function show_files($path){ //下面是单双引号嵌套解决方案 //echo "< ...