poj 2386 poj1562 poj1979 图的遍历 八个方向和四个方向 深搜

2024-02-13 17:32

本文主要是介绍poj 2386 poj1562 poj1979 图的遍历 八个方向和四个方向 深搜,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

三个题基本上就是一样的思路,学到了怎么遍历四个方向和八个方向

count初始化 poj1562

#include<stdio.h>
#include<string.h>
int w,h;
char a[105][105];void mi_gong(int x,int y)
{a[x][y]='*';int tempx;int tempy;for(int k=-1;k<=1;k++){for(int p=-1;p<=1;p++){tempx=x+k;tempy=y+p;if(tempx>=0&&tempy>=0&&tempx<h&&tempy<w&&a[tempx][tempy]=='@')mi_gong(tempx,tempy);}}return ;
}int main()
{while(scanf("%d%d",&h,&w),h||w){int count=0;//memset(a,'*',sizeof(a));getchar();      for(int i=0;i<h;i++){scanf("%s",a[i]);getchar();}for(int i=0;i<h;i++){for(int j=0;j<w;j++){if(a[i][j]=='@'){mi_gong(i,j);count++;}}}printf("%d\n",count);}return 0;
}


poj2386
#include<stdio.h>int w,h;
char a[105][105];void mi_gong(int x,int y)
{a[x][y]='.';int tempx;int tempy;for(int k=-1;k<=1;k++){for(int p=-1;p<=1;p++){tempx=x+k;tempy=y+p;if(tempx>=0&&tempy>=0&&tempx<h&&tempy<w&&a[tempx][tempy]=='W')mi_gong(tempx,tempy);}}return ;
}int main()
{int count=0;scanf("%d%d",&h,&w);getchar();for(int i=0;i<h;i++){scanf("%s",a[i]);getchar();}for(int i=0;i<h;i++){for(int j=0;j<w;j++){if(a[i][j]=='W'){mi_gong(i,j);count++;}}}printf("%d",count);return 0;
}


poj1979
#include<stdio.h>
#include<iostream>
using namespace std;int w,h;
char a[21][21];int mi_gong(int x,int y)
{if(x<0||x>=h||y<0||y>=w)return 0;if(a[x][y]=='#')return 0;a[x][y]='#';return (1+mi_gong(x-1,y)+mi_gong(x,y-1)+mi_gong(x+1,y)+mi_gong(x,y+1));
}int main()
{int kw,kh;while(scanf("%d%d",&w,&h),w||h){getchar();for(int i=0;i<h;i++){scanf("%s",a[i]);getchar();}for(int i=0;i<h;i++){for(int j=0;j<w;j++){if(a[i][j]=='@'){printf("%d\n",mi_gong(i,j));break;}}}}return 0;
}


这篇关于poj 2386 poj1562 poj1979 图的遍历 八个方向和四个方向 深搜的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/706165

相关文章

MySQL存储过程之循环遍历查询的结果集详解

《MySQL存储过程之循环遍历查询的结果集详解》:本文主要介绍MySQL存储过程之循环遍历查询的结果集,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言1. 表结构2. 存储过程3. 关于存储过程的SQL补充总结前言近来碰到这样一个问题:在生产上导入的数据发现

python进行while遍历的常见错误解析

《python进行while遍历的常见错误解析》在Python中选择合适的遍历方式需要综合考虑可读性、性能和具体需求,本文就来和大家讲解一下python中while遍历常见错误以及所有遍历方法的优缺点... 目录一、超出数组范围问题分析错误复现解决方法关键区别二、continue使用问题分析正确写法关键点三

Java遍历HashMap的6种常见方式

《Java遍历HashMap的6种常见方式》这篇文章主要给大家介绍了关于Java遍历HashMap的6种常见方式,方法包括使用keySet()、entrySet()、forEach()、迭代器以及分别... 目录1,使用 keySet() 遍历键,再通过键获取值2,使用 entrySet() 遍历键值对3,

C++中使用vector存储并遍历数据的基本步骤

《C++中使用vector存储并遍历数据的基本步骤》C++标准模板库(STL)提供了多种容器类型,包括顺序容器、关联容器、无序关联容器和容器适配器,每种容器都有其特定的用途和特性,:本文主要介绍C... 目录(1)容器及简要描述‌php顺序容器‌‌关联容器‌‌无序关联容器‌(基于哈希表):‌容器适配器‌:(

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

poj 1287 Networking(prim or kruscal最小生成树)

题意给你点与点间距离,求最小生成树。 注意点是,两点之间可能有不同的路,输入的时候选择最小的,和之前有道最短路WA的题目类似。 prim代码: #include<stdio.h>const int MaxN = 51;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int P;int prim(){bool vis[MaxN];