[uva-352]The Seasonal War (dfs和bfs两种
来源:互联网 发布:发帖软件 编辑:程序博客网 时间:2024/05/20 22:28
求‘1’的连通块个数。
优化就是把搜过的1变成0。
没有内心戏
没有表情包
反正放假了
//uva 352 BFS by zhuhua//time limit: 3000 ms//AC time: 0 ms???#include <iostream>#include <cstdio>#include <queue>#include <utility>using namespace std;typedef pair<int,int> P;int dx[8]={1,1,0,-1,-1,-1,0,1};int dy[8]={0,1,1,1,0,-1,-1,-1};char Map[30][30];int n;void bfs(int x, int y){ queue<P>que; Map[x][y]='0'; que.push(P(x,y)); while(!que.empty()){ P p=que.front(); que.pop(); for(int i=0;i<8;i++){ int x2=p.first+dx[i]; int y2=p.second+dy[i]; if(x2>=1&&x2<=n&&y2>=1&&y2<=n&&Map[x2][y2]=='1'){//cout<<"test"<<x2<<' '<<y2<<endl; Map[x2][y2]='0'; que.push(P(x2,y2)); } } }}int main(){ int i,j,t,cnt; t=1; while(scanf("%d",&n)!=EOF) { //getchar(); for(i=1;i<=n;i++){ scanf(" %s",Map[i]+1); } cnt=0; for(i=1;i<=n;i++) for(j=1;j<=n;j++) { if(Map[i][j]=='1') { //cout<<i<<' '<<j<<endl; cnt++; bfs(i,j); } } printf("Image number %d contains %d war eagles.\n",t++,cnt); } return 0;}
//uva 352 DFS by zhuhua//time limit: 3000 ms//AC time: 0 ms???#include <iostream>#include <cstdio>#include <queue>#include <utility>using namespace std;typedef pair<int,int> P;int dx[8]={1,1,0,-1,-1,-1,0,1};int dy[8]={0,1,1,1,0,-1,-1,-1};char Map[30][30];int n;void dfs(int x, int y){ Map[x][y]='0'; for(int i=0;i<8;i++) { int x2=x+dx[i]; int y2=y+dy[i]; if(x2>=1&&x2<=n&&y2>=1&&y2<=n &&Map[x2][y2]=='1') { dfs(x2,y2); } }}int main(){ int i,j,t,cnt; t=1; while(scanf("%d",&n)!=EOF) { //getchar(); for(i=1;i<=n;i++){ scanf(" %s",Map[i]+1); } cnt=0; for(i=1;i<=n;i++) for(j=1;j<=n;j++) { if(Map[i][j]=='1') { //cout<<i<<' '<<j<<endl; cnt++; dfs(i,j); } } printf("Image number %d contains %d war eagles.\n",t++,cnt); } return 0;}
阅读全文
0 0
- [uva-352]The Seasonal War (dfs和bfs两种
- UVa 352 - The Seasonal War
- UVa 352 The Seasonal War
- UVA 352 The Seasonal War
- 352The Seasonal War
- uva352 The Seasonal War-python
- UVA - 10004 Bicoloring (dfs和bfs两种解法)
- [uva-572]Oil Deposits (BFS和DFS两种
- UVA - 572 Oil Deposits(DFS和BFS两种解法)
- POJ-1426-Find The Multiple (BFS +DFS)两种解法
- UVA 657 The die is cast ——bfs+dfs
- POJ 1426 DFS BFS两种写法
- XYZZY uva BFS+DFS
- 通过BFS 和 DFS两种方法找无向图的连通分量
- UVA-11882 bfs + dfs + 剪枝
- UVA10557XYZZY(DFS+BFS 与 SPFA 两种做法)
- POJ 3984 迷宫问题 BFS DFS两种解法
- Leetcode 133. Clone Graph用BFS、DFS两种解法
- Sublime Text自定义快捷键
- Java Number & Math 类
- Linux 安装JDK
- Mac常用快捷键一览
- 华为面经
- [uva-352]The Seasonal War (dfs和bfs两种
- 洛谷Oj-最短网络 Agri-Net-最小生成树(模板题)
- Java Character 类
- 显示调用析构函数被g++编译器优化掉不执行
- 修改Linux系统的主机名
- imx6学习笔记
- 2017.10.1 互不侵犯king 思考记录
- KD-tree的原理以及构建与查询操作的python实现
- redis安装make失败