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 ...
随机推荐
- 移动 Web 开发技巧
1.使用click会出现绑定点击区域闪一下的情况,解决:给该元素一个样式如下 -webkit-tap-highlight-color: rgba(0,0,0,0); 2.用iphone或ipad浏览很 ...
- Android中ViewPager+Fragment取消(禁止)预加载延迟加载(懒加载)问题解决方案
转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/53205878本文出自[DylanAndroid的博客] Android中Vie ...
- POJ 3067 Japan(经典树状数组)
基础一维树状数组 题意:左边一排 1-n 的城市,右边一排 1-m 的城市,都从上到下依次对应.接着给你一些城市对,表示城市这两个城市相连,最后问你一共有多少个交叉,其中处于城市处的交叉不算并且每个 ...
- MVC LINQ to SQL: Basic Concepts and Features
http://www.codeproject.com/Articles/215712/LINQ-to-SQL-Basic-Concepts-and-Features
- DNS常用命令
ipconfig/ifconfig 显示网络信息 nslookup host 查询域名对应的ip同时也显示了网关地址 cat /etc/resolv.conf 查看本地dns服务器地 ...
- rman归档删除
rman: delete [all] input 数据库oracle 11g 全备脚本如下:rman target / <<EOFrun {allocate channel t1 typ ...
- NumPy 学习(2): 数组的操作
1. 简单一维数组的操作 一维数组的操作类似于python自身的list类型. In [14]: arr = np.arange(10) In [15]: arr Out[15]: array([0, ...
- C#获取IP地址
public string GetUserIP() { string _userIP; if(Request.ServerVariables["HTTP_VIA ...
- [转]shell 变量替换
转自:http://blog.csdn.net/xuhongning/article/details/6191515 1,参数替换: 不含有“:”的,只要定义了,就生效,不管是否为空 含有“:”的,即 ...
- 【转】推荐UML插件AmaterasUML
基于Green UML在使用过程中的问题(对于大工程,点击生成类图后不响应),自己只能再次寻找其他的插件.在无意中,发现AmaterasUML. 官方网站:http://amateras.source ...