hdu Hike on a Graph
此题是道bfs搜索的题目。bfs的精髓就是找到下一步的所有可能然后存储起来,有点暴力的感觉,这题就是每步中 所有的可能都入队,然后一一 判断。这道题的题意是 :
给你一幅完全图,再给你三个盘,目的是把这三个盘移动到一个点上,输出最少步数!盘移动的时候有要求,比如移第一个盘,把1盘移动到2这个位置,(1,2)这个点有颜色标记,另外两个盘比如说在3,4两点,那么1盘能移动到2这个点的条件是(1,2)这个点的颜色要与(3,4)这点的颜色相同!!
#include"iostream"
#include"stdio.h"
#include"algorithm"
#include"string.h"
#include"cmath"
#include"string"
#include"queue"
#define mx 105
using namespace std;
int n,p1,p2,p3;
struct node
{
int p[];
int step;
};
char g[mx][mx];
bool vis[mx][mx][mx];//标记某种情况是否已出现过 void Set(node a)
{
vis[a.p[]][a.p[]][a.p[]]=false;
} bool judge(node a) //判断此情况是否已出现过
{
return vis[a.p[]][a.p[]][a.p[]];
} bool End(node a) //判断是否完成操作
{
if(a.p[]==a.p[]&&a.p[]==a.p[]) return true;
return false;
} void bfs()
{
queue<node>q;
while(!q.empty()) q.pop();
node cur,next;
cur.p[]=p1;cur.p[]=p2;cur.p[]=p3;cur.step=;
q.push(cur);
Set(cur);
int i;
while(!q.empty())
{
cur=q.front();
q.pop();
if(End(cur))
{
cout<<cur.step<<endl;
return;
}
for(i=;i<=n;i++)
{
next=cur;
next.p[]=i;
next.step++;
if(judge(next)&&g[cur.p[]][i]==g[cur.p[]][cur.p[]])
{
Set(next);
q.push(next);
}
}
for(i=;i<=n;i++)
{
next=cur;
next.p[]=i;
next.step++;
if(judge(next)&&g[cur.p[]][i]==g[cur.p[]][cur.p[]])
{
Set(next);
q.push(next);
}
}
for(i=;i<=n;i++)
{
next=cur;
next.p[]=i;
next.step++;
if(judge(next)&&g[cur.p[]][i]==g[cur.p[]][cur.p[]])
{
Set(next);
q.push(next);
}
}
}
cout<<"impossible"<<endl;
}
int main()
{
int i,j;
while(cin>>n,n)
{
memset(vis,true,sizeof(vis));
cin>>p1>>p2>>p3;
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
{
cin>>g[i][j];//用于单个字符的输入,不会读入空格,以前一直误以为会连空格一起读入
}
}
bfs();
}
return ;
}
hdu Hike on a Graph的更多相关文章
- [ACM_搜索] ZOJ 1103 || POJ 2415 Hike on a Graph (带条件移动3盘子到同一位置的最少步数 广搜)
Description "Hike on a Graph" is a game that is played on a board on which an undirected g ...
- POJ-2415 Hike on a Graph (BFS)
Description "Hike on a Graph" is a game that is played on a board on which an undirected g ...
- HDU 6343.Problem L. Graph Theory Homework-数学 (2018 Multi-University Training Contest 4 1012)
6343.Problem L. Graph Theory Homework 官方题解: 一篇写的很好的博客: HDU 6343 - Problem L. Graph Theory Homework - ...
- HDU 5876:Sparse Graph(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description In graph theory, t ...
- HDU 6343 - Problem L. Graph Theory Homework - [(伪装成图论题的)简单数学题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6343 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- HDU 5631 Rikka with Graph 暴力 并查集
Rikka with Graph 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5631 Description As we know, Rikka ...
- HDU 5422 Rikka with Graph
Rikka with Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- hdu 5424 Rikka with Graph II (BestCoder Round #53 (div.2))(哈密顿通路判断)
http://acm.hdu.edu.cn/showproblem.php?pid=5424 哈密顿通路:联通的图,访问每个顶点的路径且只访问一次 n个点n条边 n个顶点有n - 1条边,最后一条边的 ...
- hdu 3435 A new Graph Game
http://acm.hdu.edu.cn/showproblem.php?pid=3435 #include <cstdio> #include <iostream> #in ...
随机推荐
- 【MySQL 安装过程2】MySQL安装到 最后一部 未响应 的解决方案
首先我们应该做的 是在控制面板将MySQL 卸载.再进行以下的操作: 1.在开始菜单下,点击运行,输入regedit,进入注册表编辑器目录下 2.在注册表编辑器里system下找到controlset ...
- nginx日志中文变成类型\xE9\xA6\x96\xE9\xA1\xB5-\xE6\x8E\xA8\xE8\x8D\x90的东西
感谢 http://my.oschina.net/leejun2005/blog/106791 代码如下: public class App { public static String str2He ...
- JMeter中的关联-正则表达式提取(2)
JMeter获取正则表达式中的提取的所有关联值的解决方法: 需求如下: { : ", : "results": : [ : : { : : : "total_e ...
- APK瘦身实践
首发地址:http://www.jayfeng.com/2015/12/29/APK%E7%98%A6%E8%BA%AB%E5%AE%9E%E8%B7%B5/ 因为推广的需要,公司需要把APK的大小再 ...
- 《DSP using MATLAB》示例Example5.1
终于看到第5章了,继续努力,加油!!! 代码: xn = [0, 1, 2, 3]; N =4; Xk = dfs(xn,N) 用到的dfs函数: function [Xk] = dfs(xn,N) ...
- [技术学习]js继承
今天又看了一遍js的面向对象方面的知识,重点看了继承相关内容,已经记不得看了第几次这个内容,终于觉得自己好像懂了,特记录下来过程. js面向对象继承分为两大类,主要分为对象继承和非对象继承(拷贝继承) ...
- 矩阵快速幂 ZOJ 3497 Mistwald
题目传送门 题意:看似给了一个迷宫,每个点能传送到4个地方,问在P时间能否到达终点 分析:其实是一个有向图,可以用邻接矩阵存图,连乘P次看是否能从1到n*m,和floyd的传递背包思想一样 #incl ...
- ASP.NET Core EF Sample
Install EF Install-Package Microsoft.EntityFrameworkCore.SqlServer Install-Package Microsoft.EntityF ...
- Drawing Arc Using ArcSegment in XAML
We can use the Arc XAML element to draw arcs in XAML. Besides drawing arcs using the Arc element, we ...
- soapui中文操作手册(八)----Web服务的功能测试案例
现在,让我们来看看在一个TestCase的功能测试. 展开 Simple TestSuite并双击Simple Login and Logout w. Properties Steps. 正如你所看到 ...