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 ...
随机推荐
- EL表达式详解及应用实例
1. EL是JSP内置的表达式语言! * jsp2.0开始,不让再使用java脚本,而是使用el表达式和动态标签来替代java脚本! * EL替代的是<%= ... %>,也就是说,EL只 ...
- 【转】清理Kylin的中间存储数据(HDFS & HBase Tables)
http://blog.csdn.net/jiangshouzhuang/article/details/51290399 Kylin在创建cube过程中会在HDFS上生成中间数据.另外,当我们对cu ...
- 《DSP using MATLAB》示例Example5.2
代码: L = 5; N = 20; k = [-N/2:N/2]; % square wave parameters xn = [ones(1,L), zeros(1,N-L)]; % Sq wav ...
- const int *
5.Please choose the right statement about constusage: A.const int a;//const interger B.int const a;/ ...
- HDU4511 小明系列故事——女友的考验(AC自动机 + DP)
题目大概说有平面有n个点,从1点出发走到n点,每一步只能走到序号比当前更大的点且走的序列不能包含给定的m个序列中的任何一个,问1走到n的最短路. 用m个序列建个AC自动机,后缀包含整个序列的结点标记一 ...
- node.js链接mysql
node.js连接数据库有很多种,比如:mongoose,oracle,mysql...,我自己玩就选了一个我很熟悉的轻量级的mysql数据库尝试了一把,感觉不错. 首先要把mysql客户端安装好,官 ...
- .net平台下C#socket通信(中)
上篇.net平台下C#socket通信(上)介绍了socket通信的基本原理及最基本的通信方式.本文在此基础上就socket通信时经常遇到的问题做一个简单总结,都是项目中的一些小问题,拿来此处便于下次 ...
- js 四舍五入保留二位小数
1. 最笨的办法....... [我就怎么干的.........] function get() { var s = 22.127456 + ""; var str = s.sub ...
- 使用GDB 修改MySQL参数不重启
link:http://blog.chinaunix.net/uid-20785090-id-4016315.html mysql很多参数都需要重启才能生效,有时候条件不允许,可以使用gdb作为最后的 ...
- iOS中为网站添加图标到主屏幕
1 <link rel="apple-touch-icon-precomposed" href="icon.png"/> 2 <link re ...