此题是道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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 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 - ...

  4. HDU 5876:Sparse Graph(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description   In graph theory, t ...

  5. 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 ...

  6. HDU 5631 Rikka with Graph 暴力 并查集

    Rikka with Graph 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5631 Description As we know, Rikka ...

  7. HDU 5422 Rikka with Graph

    Rikka with Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  8. 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条边,最后一条边的 ...

  9. hdu 3435 A new Graph Game

    http://acm.hdu.edu.cn/showproblem.php?pid=3435 #include <cstdio> #include <iostream> #in ...

随机推荐

  1. 两种方法获取shadow ssdt

    ULONG GetShadowSsdtCurrentAddresses( PSSDT_ADDRESS   AddressInfo, PULONG          Length ) { PSYSTEM ...

  2. 【T_SQL】基础 续+

    十.模糊查询 1.LIKE --查询时,字段中的内容并不一定与查询内容完全匹配,只要字段中含有这些内容. SELECT StuName AS 姓名 FROM Stuinfo WHERE stuname ...

  3. android 获取文件夹、文件的大小 以B、KB、MB、GB 为单位

    android 获取文件夹.文件的大小 以B.KB.MB.GB 为单位   public class FileSizeUtil { public static final int SIZETYPE_B ...

  4. Loadrunner在场景中添加多个负载机报错:Action.c(38): Error -26488: Could not obtain information about submitted解决方法

    Error -26488: Could not obtain information about submitted file "E:\.jpg": _stat32 rc=-1, ...

  5. C#函数与SQL储存过程

    一点点小认识作为memo,求指正. C#的函数与SQL的储存过程有很多的相似性, 它们都是一段封闭的代码块,来提高代码的重用性,虽然现在复制粘贴很方便,但是我们在写多个函数的时候频繁的复制粘贴相同的内 ...

  6. 8、显示程序占用内存多少.txt

    方法一: 要加单元 PsAPI procedure TForm1.tmr1Timer(Sender: TObject); begin edt1.Text:= format('memory use: % ...

  7. SlidesJS的使用

    项目中对slideshow要求要有触屏滑动换图功能,就想到了SlidesJS这个Jquery插件 例排,先把静态html写好 <div id="cm_slides"> ...

  8. ArcGIS 最短路径计算

    using System;using ESRI.ArcGIS.Carto;using ESRI.ArcGIS.Geometry;using ESRI.ArcGIS.Geodatabase;using ...

  9. android项目 在签名打包遇到的问题

    我在签名打包前,build success  ,可以把程序安装在手机上 然后签名打包的时候,build fail  ,原因采用了release 版本,因此这个时候在gradule build 添加下面 ...

  10. Financial Management[POJ1004]

    Financial Management Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 179458   Accepted: ...