题目链接:点我

第一次不太清楚怎么判重,现在懂了,等下次再做

 /*
*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(略难)的更多相关文章

  1. HDU 4531 bfs/康拓展开

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4531 吉哥系列故事——乾坤大挪移 Time Limit: 2000/1000 MS (Java/Othe ...

  2. hdu 5040 BFS 多维化处理图

    http://acm.hdu.edu.cn/showproblem.php?pid=5040 跟这一题http://blog.csdn.net/u011026968/article/details/3 ...

  3. C - 小明系列故事――捉迷藏 HDU - 4528 bfs +状压 旅游-- 最短路+状压

    C - 小明系列故事――捉迷藏 HDU - 4528 这个题目看了一下题解,感觉没有很难,应该是可以自己敲出来的,感觉自己好蠢... 这个是一个bfs 用bfs就很好写了,首先可以预处理出大明和二明能 ...

  4. HDU 2822 (BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...

  5. HDU 1180 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...

  6. HDU 2531 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...

  7. HDU 5025 (BFS+记忆化状压搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5025 题目大意: 迷宫中孙悟空救唐僧,可以走回头路.必须收集完钥匙,且必须按顺序收集.迷宫中还有蛇, ...

  8. HDU 1429 (BFS+记忆化状压搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1429 题目大意:最短时间内出迷宫,可以走回头路,迷宫内有不同的门,对应不同的钥匙. 解题思路: 要是 ...

  9. HDU 1026 (BFS搜索+优先队列+记录方案)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...

随机推荐

  1. 戴尔游匣5577安装 ubuntu/mint

    这里以mint为例. 做好usb启动盘后, 启动到读秒的时候按上下方向键唤出如下选项: 在第二个选项里按 tab 键(上图界面有提示) 进去后有如下界面: 在后面加上参数: nouveau.modes ...

  2. js原生选择class DOM元素

    document.querySelector(".demo"); querySelector() 方法返回匹配指定选择器的第一个元素.如果需要返回所有的元素,使用 querySel ...

  3. 【逆向知识】PE ASLR

    1.知识点 微软从windows vista/windows server 2008(kernel version 6.0)开始采用ASLR技术,主要目的是为了防止缓冲区溢出 ASLR技术会使PE文件 ...

  4. Permutation Index I & II

    Given a permutation which contains no repeated number, find its index in all the permutations of the ...

  5. HDU 6215 2017Brute Force Sorting 青岛网络赛 队列加链表模拟

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6215 题意:给你长度为n的数组,定义已经排列过的串为:相邻两项a[i],a[i+1],满足a[i]&l ...

  6. Scrapy:运行爬虫程序的方式

    Windows 10家庭中文版,Python 3.6.4,Scrapy 1.5.0, 在创建了爬虫程序后,就可以运行爬虫程序了.Scrapy中介绍了几种运行爬虫程序的方式,列举如下: -命令行工具之s ...

  7. python基础--xml和configparse模块

    1)XML模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多. 下面是xml的遍历查询删除修改和生成 # -*- coding:utf-8 -*- __author__ = 's ...

  8. CF614A 【Link/Cut Tree】

    题意:求出所有w^i使得l<=w^i<=r 输入为一行,有三个数,分别是l,r,w.意义如题目所描述 输出为一行,输出所有满足条件的数字,每两个数字中间有一个空格 如果没有满足条件的数字则 ...

  9. C语言-删除注释

    C语言中的注释,不嵌套,一律使用/*   */的形式.不过双引号中的/*  */则不能算是注释. 以下是正确的代码 /* * ===================================== ...

  10. mysqlsla 安装

    tar -zxvf mysqlsla-2.03.tar.gz cd mysqlsla-2.03 perl Makefile.PLmake && make install BEGIN f ...