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[3];
    int step;
};
char g[mx][mx];
bool vis[mx][mx][mx];//标记某种情况是否已出现过

void Set(node a)
{
    vis[a.p[0]][a.p[1]][a.p[2]]=false;
}

bool judge(node a) //判断此情况是否已出现过
{
    return vis[a.p[0]][a.p[1]][a.p[2]];
}

bool End(node a) //判断是否完成操作
{
    if(a.p[0]==a.p[1]&&a.p[1]==a.p[2]) return true;
    return false;
}

void bfs()
{
    queue<node>q;
    while(!q.empty()) q.pop();
    node cur,next;
    cur.p[0]=p1;cur.p[1]=p2;cur.p[2]=p3;cur.step=0;
    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=1;i<=n;i++)
        {
            next=cur;
            next.p[0]=i;
            next.step++;
            if(judge(next)&&g[cur.p[0]][i]==g[cur.p[1]][cur.p[2]])
            {
                Set(next);
                q.push(next);
            }
        }
          for(i=1;i<=n;i++)
        {
            next=cur;
            next.p[1]=i;
            next.step++;
            if(judge(next)&&g[cur.p[1]][i]==g[cur.p[0]][cur.p[2]])
            {
                Set(next);
                q.push(next);
            }
        }
          for(i=1;i<=n;i++)
        {
            next=cur;
            next.p[2]=i;
            next.step++;
            if(judge(next)&&g[cur.p[2]][i]==g[cur.p[1]][cur.p[0]])
            {
                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=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                cin>>g[i][j];//用于单个字符的输入,不会读入空格,以前一直误以为会连空格一起读入
            }
        }
        bfs();
    }
    return 0;
}

时间: 2024-11-13 15:39:38

hdu Hike on a Graph的相关文章

hdu 1853 Cyclic Tour &amp;&amp; hdu 3435 A new Graph Game(简单KM算法)

Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Total Submission(s): 1478    Accepted Submission(s): 750 Problem Description There are N cities in our country, and M one-way roads connecting them. Now L

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 graph is drawn. The graph is complete and has all loops, i.e. for any two locations there is exactly one arrow between them. The arrows are coloured. There

HDU 5876:Sparse Graph(BFS)

http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinct vertices of H are adjacent if and only if they are not adjacent in G

HDU 5422:Rikka with Graph

Rikka with Graph Accepts: 353 Submissions: 1174 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的: 勇太有一张nn个点mm条边的无向图,每一条边的长度都是1.现在他想再在这张图上连上一条连接两个不同顶点边,使得1号点到nn号点的最短路尽可能的短

hdu 5422 Rikka with Graph(简单题)

Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: Yuta has a non-direct graph with n vertices and m edges. The length of each edge is 1.

hdu 5424 Rikka with Graph II 哈密顿通路

Rikka with Graph II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 367    Accepted Submission(s): 90 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situatio

HDU 6090 Rikka with Graph —— 2017 Multi-University Training 5

Rikka with Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is

hdu 6090 Rikka with Graph

Rikka with Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 425    Accepted Submission(s): 270 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation,

HDU 5424——Rikka with Graph II——————【哈密顿路径】

Rikka with Graph II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1051    Accepted Submission(s): 266 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situati