POJ3984 迷宫问题【水BFS】
版权声明:本文为博主原创文章,未经博主同意不得转载。 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】的更多相关文章
- 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, ...
- 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, ...
- POJ-3984 迷宫问题 (BFS)
题意:有一个\(5\)X\(5\)的\(01\)图,输出从左上角走到右下角的最短路径. 题解:基础的bfs,这里困难的是如何输出这个最短路径,我们可以用一个结构体来存点和路径,我们每次向外去拓展的时候 ...
- POJ.3894 迷宫问题 (BFS+记录路径)
POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...
- POJ-3984.迷宫问题(BFS + 路径输出)
昨天中午做的这道题,结果蛙了一整天,就因为一行代码困住了,今天算是见识到自己有多菜了.流泪.jpg 本题大意:给一个5 * 5的迷宫,1表示墙壁,0表示通路,从左上角走到右下角并输出路径. 本题思路: ...
- POJ3984 迷宫问题 —— BFS
题目链接:http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ3984 迷宫问题 BFS
看题传送门:http://poj.org/problem?id=3984 BFS水一发 明天帮学弟挑电脑顺便去玩.接下来几天好好看数据结构.嗯哼. 这题标准的BFS应用,唯一需要注意的是需要输出中间的 ...
- poj3984 迷宫问题(简单的输出路径的bfs)
题目链接 http://poj.org/problem?id=3984 中文题题意不解释了 反正就是简单的结构体套结构体存一下路径就行了 #include <iostream> #incl ...
- (简单) 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, ...
随机推荐
- Fresco,Facbook强大的图片加载框架
项目git地址:https://github.com/facebook/fresco Fresco是 facebook推出的一款强大的图片加载的框架:主要有Image Pipeline和Drawees ...
- struts2中迭代的使用方法
struts2 Html代码 <s:iterator value="resultList" id="user" status="st&qu ...
- MongoDB下载安装測试及使用
1.下载安装 64位:mongodb-win32-x86_64-enterprise-windows-64-2.6.4-signed.msi 余数为1的 db.collection.find({ &q ...
- 对国外某hotel的内网域简单渗透
Penetration Testing不单单是一个博客,更热衷于技术分享的平台. 本文将讲述对国外某一hotel的渗透测试,让更多的人安全意识得到提高,有攻才有防,防得在好,也有疏忽的地方,这就是为啥 ...
- {}在javascript与(python,java)中的含义区别
{}在javascript中是对象,其访问属性的方法为 a.name,a['name'],参见http://www.itxueyuan.org/view/6332.html {}在python,jav ...
- 移动端去掉按钮button默认样式
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- #測试相关#Getting “junit.framework.AssertionFailedError: Forked Java VM exited abnormally” Exception
编写Ant脚本进行持续測试的时候.出现了junit.framework.AssertionFailedError: Forked Java VM exited abnormally的报错,以此为key ...
- 【Python】学习笔记十五:循环对象
循环对象 所谓的循环对象,包含有一个next()方法(python3中为__next__() ),这个方法的目的就是进行到下一个结果,而在结束一系列结果之后,举出StopIteration错误 当一个 ...
- [转]如何用C++实现一个LRU Cache
[转自http://hawstein.com/posts/lru-cache-impl.html] LRU是Least Recently Used的缩写,意思是最近最少使用,它是一种Cache替换算法 ...
- 使用chrome调试前端线上代码
家都知道在前端开发过程中,为加快网站静态资源加载速度都会对js/css等静态资源进行压缩合并再部署到生产环境,而在实际开发过程中开发人员一般都是在开发环境进行源码文件开发调试的,当部署平台或部署人员将 ...