版权声明:本文为博主原创文章,未经博主同意不得转载。 https://blog.csdn.net/u011775691/article/details/28050277

#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <vector>
using namespace std;
map<string,int>mymap;
map<string,int>::iterator it; #define LEN 1111
bool visited[LEN];
//bool arc[LEN][LEN];
vector<int> arc[555555];
int degree[LEN];
int n,m; bool is_v_i(int v,int i)
{
vector<int>::iterator it=find(arc[v].begin(),arc[v].end(),i);
// for(it=arc[v].begin();it!=arc[v].end();i++)
// {
// if(it)
// }
if(it==arc[v].end())
return false;
else
return true;
} void dfs(int v) //深度优先遍历
{
visited[v]=true;
for(int i=1;i<=n;i++)
{
if(!visited[i] && is_v_i(v,i))
{
dfs(i);
}
}
} bool isConnected() //查看遍历后结果
{
for(int i=1;i<=n;i++)
{
if(!visited[i]){return false;}
}
return true;
} bool isCircuit() //通过度数是否为偶数推断欧拉回路
{
int oddnum=0;
for(int i=1;i<=n;i++)
{
if(degree[i]%2)
{
oddnum++;
if(oddnum>2)
return false;
}
}
return true;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("D:/1.txt","r",stdin);
//freopen("D:/2.txt","w",stdout);
#endif
int que=1;
string s1,s2;
while(cin>>s1>>s2)
{
int p,q;
it=mymap.find(s1);
if(it==mymap.end())
{
mymap[s1]=que++;
p=que-1;
}
else
{
p=it->second;
}
it=mymap.find(s2);
if(it==mymap.end())
{
mymap[s2]=que++;
q=que-1;
}
else
{
q=it->second;
}
degree[p]++;degree[q]++;//没方向的
//arc[p][q]=arc[q][p]=true;//arc[p][q] ,p,q是否连通
arc[p].push_back(q);
arc[q].push_back(p);
}
n=que-1;
dfs(1);
if(!isConnected())
{
cout<<"Impossible"<<'\n';
}
else{
if(isCircuit())
cout<<"Possible"<<'\n';
else
cout<<"Impossible"<<'\n';
}
return 0;
}

POJ3984 迷宫问题【水BFS】的更多相关文章

  1. Poj3984 迷宫问题 (BFS + 路径还原)

    Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, ...

  2. POJ-3984 迷宫问题(BFS找最短路径并保存)

    问题: 定义一个二维数组:  int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, ...

  3. POJ-3984 迷宫问题 (BFS)

    题意:有一个\(5\)X\(5\)的\(01\)图,输出从左上角走到右下角的最短路径. 题解:基础的bfs,这里困难的是如何输出这个最短路径,我们可以用一个结构体来存点和路径,我们每次向外去拓展的时候 ...

  4. POJ.3894 迷宫问题 (BFS+记录路径)

    POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...

  5. POJ-3984.迷宫问题(BFS + 路径输出)

    昨天中午做的这道题,结果蛙了一整天,就因为一行代码困住了,今天算是见识到自己有多菜了.流泪.jpg 本题大意:给一个5 * 5的迷宫,1表示墙壁,0表示通路,从左上角走到右下角并输出路径. 本题思路: ...

  6. POJ3984 迷宫问题 —— BFS

    题目链接:http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  7. POJ3984 迷宫问题 BFS

    看题传送门:http://poj.org/problem?id=3984 BFS水一发 明天帮学弟挑电脑顺便去玩.接下来几天好好看数据结构.嗯哼. 这题标准的BFS应用,唯一需要注意的是需要输出中间的 ...

  8. poj3984 迷宫问题(简单的输出路径的bfs)

    题目链接 http://poj.org/problem?id=3984 中文题题意不解释了 反正就是简单的结构体套结构体存一下路径就行了 #include <iostream> #incl ...

  9. (简单) POJ 3984 迷宫问题,BFS。

    Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, ...

随机推荐

  1. Android-Handler消息机制实现原理

    一.消息机制流程简介 在应用启动的时候,会执行程序的入口函数main(),main()里面会创建一个Looper对象,然后通过这个Looper对象开启一个死循环,这个循环的工作是,不断的从消息队列Me ...

  2. 详解UIView的frame、bounds和center属性

    From: http://ios.wpjam.com/2011/08/29/uiview-frame-bounds-center/ 1.概要 翻开ios官方开发文档,赫然发现上面对这三个属性的解释如下 ...

  3. 深入理解OAuth2.0 XSS CSRF CORS 原理

    基于Token的WEB后台认证机制 http://www.cnblogs.com/xiekeli/p/5607107.html 深入理解OAuth2.0协议http://blog.csdn.net/s ...

  4. mac 查看文件编码及转换文件编码

    方法是用vim , vim 打开相应文件, :set fileencoding即可显示文件编码格式 将文件编码转换为utf-8 :set fileencoding=utf-8

  5. 2017.2.12 开涛shiro教程-第七章-与Web集成

    2017.2.9 开涛shiro教程-第七章-与Web集成(一) 原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. ...

  6. .NET Core R2安装及示例教程

    .NET Core R2安装及示例教程 Install for Windows - Visual Studio 2015 1 Download Visual Studio 2015 Make sure ...

  7. 【Python】继承

    子类的方法__init__() 创建子类的实例时,Python首先需要完成的任务是给父类所有属性赋值,为此,子类的方法__init__()需要父类施以援手. class Car(): '''模拟汽车' ...

  8. IOS Audio开发集合

    打算每天抽出一点时间学习音频方面的知识,在此做下汇总: 1. 多媒体层预览  根据结构,明确学习内容.

  9. CSS环绕球体的旋转文字-3D效果

    代码地址如下:http://www.demodashi.com/demo/12482.html 项目文件结构截图 只需要一个html文件既可: 项目截图: 代码实现原理: 该示例的实现过程很简单,主要 ...

  10. 使用struts2完成ckeditor和图片上传

    代码地址如下:http://www.demodashi.com/demo/12427.html 使用struts2完成ckeditor和ckeditor图片上传 ckeditor版本ckeditor_ ...