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, ...
随机推荐
- Spring MVC的各种参数绑定方式(请求参数用基础类型和包装类型的区别)(转)
1.基本数据类型(以int为例,其他类似): Controller代码: @RequestMapping("saysth.do") public void test(int cou ...
- 原生js获取元素的样式信息
工作中经常会需要获取DOM元素的样式,之前都是通过jquery的css()方法,现在总结一下通过原生js获取元素样式的方法. obj.style js var _width = obj.style.w ...
- AlphaGo GITHUB
AlphaGo GITHUB https://github.com/Rochester-NRT/AlphaGo
- CLI/C++中混合类的使用【转】
http://www.cppblog.com/mzty/archive/2007/12/24/39517.html CLI/C++中混合类的使用 一 混合类 所谓混合类是指CLI/C++中native ...
- String转Map的工具类
借鉴代码 public class StringToMapUtil { public static Map<String, String> getValue(String param) { ...
- 浅谈PHP与手机APP开发即API接口开发
API(Application Programming Interface,应用程序接口)架构,已经成为目前互联网产品开发中常见的软件架构模式,并且诞生很多专门API服务的公司,如:聚合数据(http ...
- 公司的mysql-installer-community-5.7.19.0安装注意
需要安装Microsoft Visual C++ 2013 Redistributable(x64) 和 Microsoft Visual C++ 2013 Redistributable(x86) ...
- 【Python】导入类
导入单个类 随着不断添加类,可能会使文件变得很长,那么此时,需要将类存储在模块中,然后在主程序导入类即可 book.py class Book(): '''模拟一本书''' def __init__( ...
- MySQL数据库的知识总结
1.Mysql数据库存储引擎 概念:存储引擎其实就是如何实现存储数据,如何为存储的数据建立索引以及如何更新,查询数据等技术实现的方法.MYSQL中的数据用各种不同的技术存储在文件 (内存)中,这些技术 ...
- VueJS事件处理器v-on
事件监听可以使用 v-on 指令. v-on:click表达式 HTML: <!DOCTYPE html> <html> <head> <meta chars ...